Selaa lähdekoodia

Fix No Wifi reconnect

Benjamin Harris 1 kuukausi sitten
vanhempi
sitoutus
977fda0c90
1 muutettua tiedostoa jossa 28 lisäystä ja 0 poistoa
  1. 28 0
      ModulosDSP_101.ino

+ 28 - 0
ModulosDSP_101.ino

@@ -459,6 +459,7 @@ void setup() {
   Serial.printf("Reset reason: %d\n", (int)esp_reset_reason());
 
   WiFi.mode(WIFI_STA);
+  WiFi.setAutoReconnect(true);
   WiFi.begin(ssid, password);
   while (WiFi.waitForConnectResult() != WL_CONNECTED) {
     Serial.println("Connection Failed! Rebooting...");
@@ -698,10 +699,37 @@ static void handleTcpBridgeClient(WiFiClient& client)
   ledOff();
 }
 
+//=============================================================
+// WiFi watchdog
+//=============================================================
+static uint32_t s_wifiLastCheck = 0;
+static bool     s_wifiLost      = false;
+
+static void maintainWifi()
+{
+  if (millis() - s_wifiLastCheck < 5000) return;
+  s_wifiLastCheck = millis();
+
+  if (WiFi.status() != WL_CONNECTED) {
+    if (!s_wifiLost) {
+      Serial.println("WiFi lost");
+      lcd.setCursor(0, 3); lcd.print("WiFi lost...        ");
+      s_wifiLost = true;
+    }
+    WiFi.reconnect();
+  } else if (s_wifiLost) {
+    Serial.print("WiFi reconnected, IP: "); Serial.println(WiFi.localIP());
+    tcpServer.begin();   // re-register listening socket with recovered stack
+    printWifiInfo();     // update LCD with current IP
+    s_wifiLost = false;
+  }
+}
+
 //=============================================================
 // Loop
 //=============================================================
 void loop() {
+  maintainWifi();
   httpServer.handleClient();
   if (uploadActive) { delay(1); return; }
   WiFiClient client = tcpServer.available();