From d9cf5e96d6f2b94fc3ed4ecdf4790b931d21de8a Mon Sep 17 00:00:00 2001
From: Ricardo Bartels <ricardo@bitchbrothers.com>
Date: Thu, 31 Dec 2020 03:27:57 +0100
Subject: [PATCH] fixes detection if WiFi connection is active with wifiManger

refs: #94
---
 esp8266-fastled-iot-webserver.ino | 39 ++++++++++++++++++++-----------
 1 file changed, 26 insertions(+), 13 deletions(-)

diff --git a/esp8266-fastled-iot-webserver.ino b/esp8266-fastled-iot-webserver.ino
index 7869f16..f3e055e 100644
--- a/esp8266-fastled-iot-webserver.ino
+++ b/esp8266-fastled-iot-webserver.ino
@@ -813,24 +813,14 @@ void setup() {
     nameString.toCharArray(nameChar, sizeof(nameChar));
 
     wifiManager.setHostname(cfg.hostname);
+    wifiManager.setConfigPortalBlocking(false);
+    wifiManager.setSaveConfigCallback(handleReboot);
 
     //automatically connect using saved credentials if they exist
     //If connection fails it starts an access point with the specified name
     if (wifiManager.autoConnect(nameChar)) {
         Serial.println("Wi-Fi connected");
-        Serial.print("Open http://");
-        Serial.print(WiFi.localIP());
-        Serial.println(" in your browser");
-#ifdef ENABLE_MULTICAST_DNS
-        if (!MDNS.begin(cfg.hostname)) {
-            Serial.println("\nError while setting up MDNS responder! \n");
-        } else {
-            Serial.println("mDNS responder started");
-            MDNS.addService("http", "tcp", 80);
-        }
-#endif
-    }
-    else {
+    } else {
         Serial.println("Wi-Fi manager portal running");
     }
 
@@ -1330,6 +1320,29 @@ void loop() {
 #ifdef ENABLE_MULTICAST_DNS
     MDNS.update();
 #endif // ENABLE_MULTICAST_DNS
+
+    static bool hasConnected = false;
+    EVERY_N_SECONDS(1) {
+        if (wifiManager.getLastConxResult() != WL_CONNECTED) {
+            //      Serial.printf("Connecting to %s\n", ssid);
+            hasConnected = false;
+        }
+        else if (!hasConnected) {
+            hasConnected = true;
+            Serial.print("Connected! Open http://");
+            Serial.print(WiFi.localIP());
+            Serial.println(" in your browser");
+#ifdef ENABLE_MULTICAST_DNS
+            if (!MDNS.begin(cfg.hostname)) {
+                Serial.println("\nError while setting up MDNS responder! \n");
+            } else {
+                Serial.println("mDNS responder started");
+                MDNS.addService("http", "tcp", 80);
+            }
+#endif
+        }
+    }
+
 #ifdef ENABLE_MQTT_SUPPORT
     static bool mqttConnected = false;
 
-- 
GitLab