From 1f347582b0f3a19347c38d6e134d95eac41c3dc1 Mon Sep 17 00:00:00 2001 From: Kai Lauterbach Date: Tue, 10 Jan 2023 12:21:02 +0100 Subject: [PATCH] Improved wifi reconnect mechanism. More watchdog feed calls added. --- firmware/firmware.ino | 63 ++++++++++++++++++++++++++++++------------- 1 file changed, 44 insertions(+), 19 deletions(-) diff --git a/firmware/firmware.ino b/firmware/firmware.ino index 14d7dae..e412213 100644 --- a/firmware/firmware.ino +++ b/firmware/firmware.ino @@ -1,6 +1,8 @@ // Standard ESP8266 libs from project folder #include #include +#include + #include #include // WiFiClient @@ -78,6 +80,10 @@ void debug(String x) { void setup() { +#ifdef ENABLE_WATCHDOG + //wdt_disable(); +#endif + #if defined(DEBUG) || defined(SERIAL_FEATURE) Serial.begin(SERIAL_BAUD_RATE); #endif @@ -256,7 +262,6 @@ float readSensors(uint8_t s) { void wifiConnectionCheck() { -/* if ((wifi_check_interval_counter + WIFI_CHECK_INTERVAL_MS) > millis()) { // if check interval is not exceeded abort check return; @@ -269,29 +274,32 @@ void wifiConnectionCheck() { return; } - wifi_reconnect_cnt++; + // TODO Was muss hier getan werden? - debug("no wifi connection, try to reconnect " + String(wifi_reconnect_cnt)); +} - WiFi.disconnect(); - WiFi.mode(WIFI_OFF); - WiFi.mode(WIFI_STA); - -#ifdef WEBUPDATER_FEATURE - setWifiReconnectCnt(wifi_reconnect_cnt); -#endif +#ifdef DISABLE_WIFIMANAGER +void wifi_disconnected(WiFiEvent_t event) { if (wifi_reconnect_cnt >= 5) { - debug("\nReboot"); + + debug("\nReboot, to much reconnects to wifi done before"); ESP.restart(); + } else { + debug("no wifi connection, try to reconnect " + String(wifi_reconnect_cnt)); + + wifi_reconnect_cnt++; wifiConnect(); - //initWifiBasedSW(); +#ifdef WEBUPDATER_FEATURE + setWifiReconnectCnt(wifi_reconnect_cnt); +#endif } -*/ + } +#endif //*************************************************************************// @@ -322,7 +330,7 @@ void wifiConnect() { #endif } -#else // DISABLE_WIFIMANAGER +#else // DISABLE_WIFIMANAGER is defined if (!WiFi.config(local_IP, gateway, subnet)) { debug("Failed to set IP configuration"); @@ -330,6 +338,9 @@ void wifiConnect() { debug("Successful set IP configuration"); } + // bind the check function to the disconnected wifi event + WiFi.onEvent(wifi_disconnected, WIFI_EVENT_STAMODE_DISCONNECTED); + WiFi.begin(WIFI_SSID, WIFI_PASSWD); WiFi.setAutoReconnect(true); @@ -361,15 +372,15 @@ void criticalBatCheck() { void loop() { -#ifdef ENABLE_WATCHDOG - WDT_FEED(); -#endif - #ifdef BATTERY_POWERED delay(50); return; -#else +#else // not in BATTERY_POWERED mode + +#ifdef ENABLE_WATCHDOG + WDT_FEED(); +#endif // call fsm loop function _fsm_loop(); @@ -393,6 +404,9 @@ void _fsm_loop() update_webserver_cnt = millis(); doWebUpdater(); } +#ifdef ENABLE_WATCHDOG + WDT_FEED(); +#endif #endif #ifdef HTTP_CALL_SEND_JSON_DATA @@ -405,6 +419,9 @@ void _fsm_loop() http_call_send_json_data_cnt = millis(); http_call_send_json_data(); } +#ifdef ENABLE_WATCHDOG + WDT_FEED(); +#endif #endif switch (fsm_state) @@ -608,6 +625,10 @@ void _fsm_loop() // Send HTTP GET request int httpResponseCode = http.GET(); +#ifdef ENABLE_WATCHDOG + WDT_FEED(); +#endif + if (httpResponseCode > 0) { String response = http.getString(); debug("http response code: " + String(httpResponseCode) + " = " + response); @@ -747,6 +768,10 @@ void http_call_send_json_data() // Send HTTP GET request int httpResponseCode = http.GET(); +#ifdef ENABLE_WATCHDOG + WDT_FEED(); +#endif + if (httpResponseCode > 0) { String response = http.getString(); debug("http response code: " + String(httpResponseCode) + " = " + response);