more stable OTA uploads. disable UART and stop Mqtt during upload.

This commit is contained in:
proddy
2020-07-22 16:09:43 +02:00
parent 1c4033fb71
commit dbaf6fe4b4
3 changed files with 43 additions and 33 deletions

View File

@@ -1,46 +1,52 @@
#include <OTASettingsService.h>
OTASettingsService::OTASettingsService(AsyncWebServer* server, FS* fs, SecurityManager* securityManager) :
_httpEndpoint(OTASettings::read, OTASettings::update, this, server, OTA_SETTINGS_SERVICE_PATH, securityManager),
_fsPersistence(OTASettings::read, OTASettings::update, this, fs, OTA_SETTINGS_FILE),
_arduinoOTA(nullptr) {
OTASettingsService::OTASettingsService(AsyncWebServer * server, FS * fs, SecurityManager * securityManager)
: _httpEndpoint(OTASettings::read, OTASettings::update, this, server, OTA_SETTINGS_SERVICE_PATH, securityManager)
, _fsPersistence(OTASettings::read, OTASettings::update, this, fs, OTA_SETTINGS_FILE)
, _arduinoOTA(nullptr) {
#ifdef ESP32
WiFi.onEvent(std::bind(&OTASettingsService::onStationModeGotIP, this, std::placeholders::_1, std::placeholders::_2),
WiFiEvent_t::SYSTEM_EVENT_STA_GOT_IP);
WiFi.onEvent(std::bind(&OTASettingsService::onStationModeGotIP, this, std::placeholders::_1, std::placeholders::_2), WiFiEvent_t::SYSTEM_EVENT_STA_GOT_IP);
#elif defined(ESP8266)
_onStationModeGotIPHandler =
WiFi.onStationModeGotIP(std::bind(&OTASettingsService::onStationModeGotIP, this, std::placeholders::_1));
_onStationModeGotIPHandler = WiFi.onStationModeGotIP(std::bind(&OTASettingsService::onStationModeGotIP, this, std::placeholders::_1));
#endif
addUpdateHandler([&](const String& originId) { configureArduinoOTA(); }, false);
addUpdateHandler([&](const String & originId) { configureArduinoOTA(); }, false);
}
void OTASettingsService::begin() {
_fsPersistence.readFromFS();
configureArduinoOTA();
_fsPersistence.readFromFS();
configureArduinoOTA();
}
void OTASettingsService::loop() {
if (_state.enabled && _arduinoOTA) {
_arduinoOTA->handle();
}
if (_state.enabled && _arduinoOTA) {
_arduinoOTA->handle();
}
}
void OTASettingsService::configureArduinoOTA() {
if (_arduinoOTA) {
if (_arduinoOTA) {
#ifdef ESP32
_arduinoOTA->end();
_arduinoOTA->end();
#endif
delete _arduinoOTA;
_arduinoOTA = nullptr;
}
if (_state.enabled) {
// Serial.println(F("Starting OTA Update Service..."));
_arduinoOTA = new ArduinoOTAClass;
_arduinoOTA->setPort(_state.port);
_arduinoOTA->setPassword(_state.password.c_str());
/*
_arduinoOTA->onStart([]() { Serial.println(F("Starting")); });
_arduinoOTA->onEnd([]() { Serial.println(F("\r\nEnd")); });
delete _arduinoOTA;
_arduinoOTA = nullptr;
}
if (_state.enabled) {
// Serial.println(F("Starting OTA Update Service..."));
_arduinoOTA = new ArduinoOTAClass;
_arduinoOTA->setPort(_state.port);
_arduinoOTA->setPassword(_state.password.c_str());
_arduinoOTA->onStart([]() {
// Serial.println(F("Starting"));
emsesp::System::upload_status(true);
});
_arduinoOTA->onEnd([]() {
// Serial.println(F("\r\nEnd"));
emsesp::System::upload_status(false);
});
/*
_arduinoOTA->onProgress([](unsigned int progress, unsigned int total) {
Serial.printf_P(PSTR("Progress: %u%%\r\n"), (progress / (total / 100)));
});
@@ -58,16 +64,16 @@ void OTASettingsService::configureArduinoOTA() {
Serial.println(F("End Failed"));
});
*/
_arduinoOTA->begin();
}
_arduinoOTA->begin();
}
}
#ifdef ESP32
void OTASettingsService::onStationModeGotIP(WiFiEvent_t event, WiFiEventInfo_t info) {
configureArduinoOTA();
configureArduinoOTA();
}
#elif defined(ESP8266)
void OTASettingsService::onStationModeGotIP(const WiFiEventStationModeGotIP& event) {
configureArduinoOTA();
void OTASettingsService::onStationModeGotIP(const WiFiEventStationModeGotIP & event) {
configureArduinoOTA();
}
#endif