From bd7ba17a96aa9a6e82c04c68a4b97c361fe7a11c Mon Sep 17 00:00:00 2001 From: totolouis Date: Wed, 15 Apr 2026 08:35:18 +0200 Subject: [PATCH 1/2] Improve WiFi connection resilience and skip MQTT when offline - Skip MQTT connection attempts when WiFi is not connected - Add better retry system for WiFi connections --- src/main.cpp | 46 ++++++++++++++++++++++++++++++++++++++++++++++ src/net/mqtt.cpp | 5 +++++ 2 files changed, 51 insertions(+) diff --git a/src/main.cpp b/src/main.cpp index 5b36b77..3083ea9 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -180,6 +180,52 @@ void loop() { LOG.handle(); // telnetspy handling ArduinoOTA.handle(); + #if WIFI_ENABLED + // Escalating WiFi reconnection after beacon timeout disconnects + // Phase 1 (attempts 1-3): WiFi.reconnect() every 5s + // Phase 2 (attempts 4-6): Full disconnect + begin cycle every 10s + // Phase 3 (attempt 7+): ESP.restart() as last resort + { + static unsigned long lastWifiCheck = 0; + static uint8_t reconnectAttempts = 0; + unsigned long now = millis(); + unsigned long interval = reconnectAttempts < 3 ? 5000 : 10000; + + if (now - lastWifiCheck > interval) { + lastWifiCheck = now; + if (WiFi.status() != WL_CONNECTED) { + reconnectAttempts++; + + if (reconnectAttempts <= 3) { + // Phase 1: soft reconnect + LOG.print("WiFi disconnected, reconnecting (attempt "); + LOG.print(reconnectAttempts); + LOG.println("/6)..."); + WiFi.reconnect(); + } else if (reconnectAttempts <= 6) { + // Phase 2: full disconnect + begin cycle + LOG.print("WiFi reconnect failed, full reconnect (attempt "); + LOG.print(reconnectAttempts); + LOG.println("/6)..."); + WiFi.disconnect(true); + delay(100); + WiFi.mode(WIFI_STA); + WiFi.begin(); + } else { + // Phase 3: nothing worked, restart + LOG.println("WiFi reconnect failed after 6 attempts, restarting..."); + delay(100); + ESP.restart(); + } + } else if (reconnectAttempts > 0) { + LOG.print("WiFi reconnected to "); + LOG.println(WiFi.SSID()); + reconnectAttempts = 0; + } + } + } + #endif // WIFI_ENABLED + #if MQTT_ENABLED g_mqtt->loop(); diff --git a/src/net/mqtt.cpp b/src/net/mqtt.cpp index 5bc305f..f6463c5 100644 --- a/src/net/mqtt.cpp +++ b/src/net/mqtt.cpp @@ -86,6 +86,11 @@ bool Mqtt::reconnect(bool force) { return false; } + // Don't attempt MQTT if WiFi is not connected + if (WiFi.status() != WL_CONNECTED) { + return false; + } + // Retry once in a while to avoid blocking serial console // Handling too big unsigned long if(!force) { From 6f82b5e9f17c363d6726dc969c6c72cf2802474c Mon Sep 17 00:00:00 2001 From: totolouis Date: Fri, 17 Apr 2026 00:01:01 +0200 Subject: [PATCH 2/2] comments --- src/main.cpp | 16 +++++----------- src/net/mqtt.cpp | 9 ++------- 2 files changed, 7 insertions(+), 18 deletions(-) diff --git a/src/main.cpp b/src/main.cpp index 3083ea9..f094209 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -183,8 +183,7 @@ void loop() { #if WIFI_ENABLED // Escalating WiFi reconnection after beacon timeout disconnects // Phase 1 (attempts 1-3): WiFi.reconnect() every 5s - // Phase 2 (attempts 4-6): Full disconnect + begin cycle every 10s - // Phase 3 (attempt 7+): ESP.restart() as last resort + // Phase 2 (attempts 4+): Full disconnect + begin cycle every 10s { static unsigned long lastWifiCheck = 0; static uint8_t reconnectAttempts = 0; @@ -194,28 +193,23 @@ void loop() { if (now - lastWifiCheck > interval) { lastWifiCheck = now; if (WiFi.status() != WL_CONNECTED) { - reconnectAttempts++; + if (reconnectAttempts < UINT8_MAX) reconnectAttempts++; if (reconnectAttempts <= 3) { // Phase 1: soft reconnect LOG.print("WiFi disconnected, reconnecting (attempt "); LOG.print(reconnectAttempts); - LOG.println("/6)..."); + LOG.println(")..."); WiFi.reconnect(); - } else if (reconnectAttempts <= 6) { + } else { // Phase 2: full disconnect + begin cycle LOG.print("WiFi reconnect failed, full reconnect (attempt "); LOG.print(reconnectAttempts); - LOG.println("/6)..."); + LOG.println(")..."); WiFi.disconnect(true); delay(100); WiFi.mode(WIFI_STA); WiFi.begin(); - } else { - // Phase 3: nothing worked, restart - LOG.println("WiFi reconnect failed after 6 attempts, restarting..."); - delay(100); - ESP.restart(); } } else if (reconnectAttempts > 0) { LOG.print("WiFi reconnected to "); diff --git a/src/net/mqtt.cpp b/src/net/mqtt.cpp index f6463c5..6151813 100644 --- a/src/net/mqtt.cpp +++ b/src/net/mqtt.cpp @@ -81,13 +81,8 @@ void Mqtt::clearSubscriptions() { // force=false by default bool Mqtt::reconnect(bool force) { - // No configuration available - if (this->MqttConfig::isEmpty()) { - return false; - } - - // Don't attempt MQTT if WiFi is not connected - if (WiFi.status() != WL_CONNECTED) { + // No configuration available or WiFi not connected + if (this->MqttConfig::isEmpty() || WiFi.status() != WL_CONNECTED) { return false; }