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