diff --git a/Network.cpp b/Network.cpp index 6dbb7a4..31a4e87 100644 --- a/Network.cpp +++ b/Network.cpp @@ -37,6 +37,8 @@ void Network::setupDevice(const NetworkDeviceType hardware) void Network::initialize() { + _restartOnDisconnect = _preferences->getBool(preference_restart_on_disconnect); + if(_hostname == "") { _hostname = "nukihub"; @@ -102,6 +104,11 @@ int Network::update() if(!_device->isConnected()) { + if(_restartOnDisconnect && millis() > 60000) + { + ESP.restart(); + } + Serial.println(F("Network not connected. Trying reconnect.")); bool success = _device->reconnect(); Serial.println(success ? F("Reconnect successful") : F("Reconnect failed")); @@ -250,6 +257,12 @@ void Network::setMqttPresencePath(char *path) strcpy(_mqttPresencePrefix, path); } +void Network::disableAutoRestarts() +{ + _networkTimeout = 0; + _restartOnDisconnect = false; +} + bool Network::isMqttConnected() { return _mqttConnected; diff --git a/Network.h b/Network.h index 204c0b7..6e8c78f 100644 --- a/Network.h +++ b/Network.h @@ -21,6 +21,7 @@ public: void registerMqttReceiver(MqttReceiver* receiver); void reconfigureDevice(); void setMqttPresencePath(char* path); + void disableAutoRestarts(); // disable on OTA start void subscribe(const char* prefix, const char* path); void publishFloat(const char* prefix, const char* topic, const float value, const uint8_t precision = 2); @@ -60,6 +61,7 @@ private: int _networkTimeout = 0; std::vector _mqttReceivers; char* _presenceCsv = nullptr; + bool _restartOnDisconnect = false; unsigned long _lastConnectedTs = 0; }; diff --git a/Ota.cpp b/Ota.cpp index dd1fd33..b0a1a1b 100644 --- a/Ota.cpp +++ b/Ota.cpp @@ -33,3 +33,8 @@ void Ota::updateFirmware(uint8_t* buf, size_t size) } } } + +bool Ota::updateStarted() +{ + return _updateStarted; +} diff --git a/Ota.h b/Ota.h index eca4bff..5dd963b 100644 --- a/Ota.h +++ b/Ota.h @@ -9,6 +9,8 @@ class Ota public: void updateFirmware(uint8_t* buf, size_t size); + bool updateStarted(); + private: bool _updateStarted = false; esp_ota_handle_t otaHandler = 0; diff --git a/WebCfgServer.cpp b/WebCfgServer.cpp index 0836d99..f159283 100644 --- a/WebCfgServer.cpp +++ b/WebCfgServer.cpp @@ -792,6 +792,7 @@ void WebCfgServer::handleOtaUpload() } esp_task_wdt_init(30, false); + _network->disableAutoRestarts(); HTTPUpload& upload = _server.upload(); @@ -828,3 +829,8 @@ void WebCfgServer::sendFontsInterMinCss() // escaped by https://www.cescaper.com/ _server.send(200, "text/plain", intercss); } + +bool WebCfgServer::otaStarted() +{ + return _ota.updateStarted(); +} diff --git a/WebCfgServer.h b/WebCfgServer.h index 608867a..96bf955 100644 --- a/WebCfgServer.h +++ b/WebCfgServer.h @@ -27,6 +27,7 @@ public: void initialize(); void update(); + bool otaStarted(); private: @@ -75,7 +76,6 @@ private: char _credPassword[31] = {0}; bool _allowRestartToPortal = false; uint32_t _transferredSize = 0; - bool _otaStart = true; String _hostname; String _confirmCode = "----"; diff --git a/main.cpp b/main.cpp index 1b598f1..e05c5cd 100644 --- a/main.cpp +++ b/main.cpp @@ -94,6 +94,11 @@ void checkMillisTask(void *pvParameters) { delay(30000); + if(webCfgServer->otaStarted()) + { + return; + } + // millis() is about to overflow. Restart device to prevent problems with overflow if(millis() > restartTs) { diff --git a/networkDevices/W5500Device.cpp b/networkDevices/W5500Device.cpp index 53eaf6e..1b852cb 100644 --- a/networkDevices/W5500Device.cpp +++ b/networkDevices/W5500Device.cpp @@ -9,7 +9,6 @@ W5500Device::W5500Device(const String &hostname, Preferences* preferences) _preferences(preferences) { initializeMacAddress(_mac); - _restartOnDisconnect = _preferences->getBool(preference_restart_on_disconnect); Serial.print("MAC Adress: "); for(int i=0; i < 6; i++) @@ -47,10 +46,7 @@ void W5500Device::initialize() bool W5500Device::reconnect() { - if(_restartOnDisconnect && millis() > 60000) - { - ESP.restart(); - } + _hasDHCPAddress = false; diff --git a/networkDevices/W5500Device.h b/networkDevices/W5500Device.h index c27ff99..a0ade7e 100644 --- a/networkDevices/W5500Device.h +++ b/networkDevices/W5500Device.h @@ -31,7 +31,6 @@ private: int _maintainResult = 0; bool _hasDHCPAddress = false; - bool _restartOnDisconnect = false; byte _mac[6]; }; \ No newline at end of file