From 87c85eabe0b615d989bb6e26cc8f7d54cb0b4f68 Mon Sep 17 00:00:00 2001 From: 1technophile <1technophile@users.noreply.github.com> Date: Fri, 26 Jul 2019 10:55:17 +0200 Subject: [PATCH] if wifi is lost the ESP was blocked on a "MQTT connection loop" To avoid that check the status of wifi connection before trying to connect to mqtt Add of traces --- main/main.ino | 37 +++++++++++++++++++++---------------- 1 file changed, 21 insertions(+), 16 deletions(-) diff --git a/main/main.ino b/main/main.ino index 15d24f9..03ffc98 100644 --- a/main/main.ino +++ b/main/main.ino @@ -828,25 +828,30 @@ void loop() //MQTT client connexion management if (!client.connected()) { // not connected if (now - lastReconnectAttempt > 5000) { + trc("client disconnected"); lastReconnectAttempt = now; // Attempt to reconnect - if (reconnect()) { - lastReconnectAttempt = 0; - } else { - #if defined(ESPWifiManualSetup) - trc(F("restarting ESP")); - #ifdef ESP32 - ESP.restart(); + if (WiFi.status() == WL_CONNECTED){ + if (reconnect()) { + lastReconnectAttempt = 0; + } else { + #if defined(ESPWifiManualSetup) + trc(F("restarting ESP")); + #ifdef ESP32 + ESP.restart(); + #endif + #ifdef ESP8266 + ESP.reset(); + #endif + #elif defined(ESP8266) || defined(ESP32) + if (!connectedOnce) { + trc(F("reseting wifi manager")); + setup_wifimanager(true); // if we didn't connected once to mqtt we reset and start in AP mode again to have a chance to change the parameters + } #endif - #ifdef ESP8266 - ESP.reset(); - #endif - #elif defined(ESP8266) || defined(ESP32) - if (!connectedOnce) { - trc(F("reseting wifi manager")); - setup_wifimanager(true); // if we didn't connected once to mqtt we reset and start in AP mode again to have a chance to change the parameters - } - #endif + } + }else{ + trc("wifi disconnected"); } } } else { //connected