From 9546f43cd63b0cd91e75a97a198cf94da9c867c8 Mon Sep 17 00:00:00 2001 From: Northern Man Date: Mon, 17 Jan 2022 17:16:43 -0500 Subject: [PATCH] Behaviour when MQTT and/or Wifi is not available (#1126) * Behaviour when MQTT and/or Wifi is not available I was looking into why all my OpenMQTTGateway devices were not responding after a recent power failure and am speculating that the ESP32 was able to restart faster than my Wifi and MQTT Server was able to restart and resume operations. And was thinking that a generic ESP.restart may be a reasonable approach with repeated Wifi and/or MQTT connection failures. As mentioned here https://community.openmqttgateway.com/t/behaviour-when-mqtt-and-or-wifi-is-not-available/1747 Added new variable maxRetryWatchDog, which is the maximum number of mqtt or wifi connection attempts before restarting gateway device. --- main/User_config.h | 1 + main/main.ino | 45 ++++++++++++++++++++++++++++++++++++--------- 2 files changed, 37 insertions(+), 9 deletions(-) diff --git a/main/User_config.h b/main/User_config.h index 5cb1ccd..80e4577 100644 --- a/main/User_config.h +++ b/main/User_config.h @@ -135,6 +135,7 @@ const byte mac[] = {0xDE, 0xED, 0xBA, 0xFE, 0x54, 0x95}; //W5100 ethernet shield //#define MDNS_SD //uncomment if you want to use mdns for discovering automatically your ip server, please note that MDNS with ESP32 can cause the BLE to not work #define maxConnectionRetry 10 //maximum MQTT connection attempts before going to wifimanager setup if never connected once #define maxConnectionRetryWifi 5 //maximum Wifi connection attempts with existing credential at start (used to bypass ESP32 issue on wifi connect) +#define maxRetryWatchDog 11 //maximum Wifi or mqtt re-connection attempts before restarting //set minimum quality of signal so it ignores AP's under that quality #define MinimumWifiSignalQuality 8 diff --git a/main/main.ino b/main/main.ino index 9c22bde..63e1f4e 100644 --- a/main/main.ino +++ b/main/main.ino @@ -317,7 +317,7 @@ void pub(const char* topicori, JsonObject& data) { # ifdef ZgatewayPilight String value = data["value"]; String protocol = data["protocol"]; - if (value != 0) { + if (value != "null" && value != 0) { topic = topic + "/" + protocol + "/" + value; } # else @@ -561,6 +561,9 @@ void connectMQTT() { delay(2000); digitalWrite(LED_ERROR, !LED_ERROR_ON); delay(5000); + if (failure_number_mqtt > maxRetryWatchDog) { + watchdogReboot(1); + } } } @@ -982,8 +985,19 @@ void setup_wifi() { Log.trace(F("." CR)); failure_number_ntwk++; # if defined(ESP32) && defined(ZgatewayBT) - if (failure_number_ntwk > maxConnectionRetryWifi && lowpowermode) - lowPowerESP32(); + if (lowpowermode) { + if (failure_number_ntwk > maxConnectionRetryWifi) { + lowPowerESP32(); + } + } else { + if (failure_number_ntwk > maxRetryWatchDog) { + watchdogReboot(2); + } + } +# else + if (failure_number_ntwk > maxRetryWatchDog) { + watchdogReboot(2); + } # endif } Log.notice(F("WiFi ok with manual config credentials" CR)); @@ -1203,12 +1217,8 @@ void setup_wifimanager(bool reset_settings) { if (!wifiManager.autoConnect(WifiManager_ssid, WifiManager_password)) { Log.warning(F("failed to connect and hit timeout" CR)); delay(3000); -//reset and try again -# if defined(ESP8266) - ESP.reset(); -# else - ESP.restart(); -# endif + //reset and try again + watchdogReboot(3); delay(5000); } digitalWrite(LED_ERROR, !LED_ERROR_ON); @@ -2117,3 +2127,20 @@ String toString(uint32_t input) { } # endif #endif + +/* + Reboot for repeated connection issues + Reason Codes + 1 - Repeated MQTT Connection Failure + 2 - Repeated WiFi Connection Failure + 3 - Failed WiFiManager configuration portal +*/ +void watchdogReboot(byte reason) { + Log.warning(F("Rebooting for reason code %d" CR), reason); +#if defined(ESP32) + ESP.restart(); +#elif defined(ESP8266) + ESP.reset(); +#else // Insert other architectures here +#endif +} \ No newline at end of file