From 0a4b3f1e1b4871a0f3dcaa59d8b7aea7105b97e9 Mon Sep 17 00:00:00 2001 From: Manuel Weiser Date: Sat, 21 Feb 2026 12:25:41 +0100 Subject: [PATCH] fix: improve connection error handling and display logic in main loop --- src/main.cpp | 24 ++++++++++++++++--- src/wlan.cpp | 66 ++++++++++++++++++++++++---------------------------- 2 files changed, 52 insertions(+), 38 deletions(-) diff --git a/src/main.cpp b/src/main.cpp index 7135bd1..272e108 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -100,24 +100,42 @@ const unsigned long debounceDelay = 500; // 500 ms debounce delay unsigned long lastConnErrorShowTime = 0; const unsigned long connErrorShowInterval = 10000; // Show connection error every 10 seconds if exists +bool showingConnError = false; +unsigned long connErrorStartTime = 0; +const unsigned long connErrorDisplayDuration = 3000; // ##### PROGRAM START ##### void loop() { unsigned long currentMillis = millis(); // Handle connection errors (not registered or not connected) - if (intervalElapsed(currentMillis, lastConnErrorShowTime, connErrorShowInterval)) { + if (!showingConnError && intervalElapsed(currentMillis, lastConnErrorShowTime, connErrorShowInterval)) { if (!filamanRegistered) { oledShowConnectionError("Not Registered", WiFi.localIP().toString()); - vTaskDelay(pdMS_TO_TICKS(3000)); + showingConnError = true; + connErrorStartTime = currentMillis; mainTaskWasPaused = true; } else if (!filamanConnected) { oledShowConnectionError("API Connection Lost", WiFi.localIP().toString()); - vTaskDelay(pdMS_TO_TICKS(3000)); + showingConnError = true; + connErrorStartTime = currentMillis; mainTaskWasPaused = true; } } + // Clear connection error after duration + if (showingConnError && currentMillis - connErrorStartTime >= connErrorDisplayDuration) { + showingConnError = false; + // Force UI refresh next cycle + lastTopRowUpdateTime = 0; + } + + // Skip the rest of the loop while showing error to avoid UI overwriting + if (showingConnError) { + esp_task_wdt_reset(); + return; + } + // Überprüfe den Status des Touch Sensors if (touchSensorConnected && digitalRead(TTP223_PIN) == HIGH && currentMillis - lastButtonPress > debounceDelay) { diff --git a/src/wlan.cpp b/src/wlan.cpp index 1d221c9..0e3a900 100644 --- a/src/wlan.cpp +++ b/src/wlan.cpp @@ -12,17 +12,17 @@ bool wm_nonblocking = false; uint8_t wifiErrorCounter = 0; void wifiSettings() { - // Optimierte WiFi-Einstellungen - WiFi.mode(WIFI_STA); // explicitly set mode, esp defaults to STA+AP - WiFi.setSleep(false); // disable sleep mode + // Standard WiFi-Einstellungen für höchste Stabilität mit ESPAsyncWebServer + WiFi.mode(WIFI_STA); WiFi.setHostname("FilaMan"); - esp_wifi_set_ps(WIFI_PS_NONE); - // Maximale Sendeleistung - WiFi.setTxPower(WIFI_POWER_19_5dBm); // Set maximum transmit power + // Wir lassen das Power Management (Sleep) auf dem ESP-Standard. + // Ein erzwungenes Ausschalten führt zu RF-Abstürzen bei vielen asynchronen HTTP-Requests. + + // Angemessene Sendeleistung (reduziert Hitzeprobleme) + WiFi.setTxPower(WIFI_POWER_17dBm); // Set stable transmit power (default is 19.5 which gets hot) - // Optimiere TCP/IP Stack - esp_wifi_set_protocol(WIFI_IF_STA, WIFI_PROTOCOL_11B | WIFI_PROTOCOL_11G | WIFI_PROTOCOL_11N); + // Wir überlassen die Protokoll-Aushandlung (b/g/n) dem Treiber. Harte Vorgaben crashen unter Last. // Aktiviere WiFi-Roaming für bessere Stabilität esp_wifi_set_rssi_threshold(-80); @@ -74,37 +74,33 @@ void initWiFi() { void checkWiFiConnection() { if (WiFi.status() != WL_CONNECTED) { - Serial.println("WiFi connection lost. Reconnecting..."); - wifiOn = false; - oledShowTopRow(); - oledDisplayText("WiFi reconnecting"); - WiFi.reconnect(); // Versuche, die Verbindung wiederherzustellen - vTaskDelay(pdMS_TO_TICKS(5000)); // Warte 5 Sekunden, bevor erneut geprüft wird - if (WiFi.status() != WL_CONNECTED) - { - Serial.println("Failed to reconnect. Restarting WiFi..."); - WiFi.disconnect(); - Serial.println("WiFi disconnected."); - vTaskDelay(pdMS_TO_TICKS(1000)); - wifiErrorCounter++; + if (wifiOn) { + Serial.println("WiFi connection lost. LwIP is auto-reconnecting..."); + wifiOn = false; + oledShowTopRow(); + oledDisplayText("WiFi reconnecting"); + } + + wifiErrorCounter++; - //wifiSettings(); - WiFi.reconnect(); - Serial.println("WiFi reconnecting..."); - WiFi.waitForConnectResult(); - } - else + // Only restart after a significant time of no connection (e.g. 5 minutes) + // Assuming WIFI_CHECK_INTERVAL is 60000ms (1 minute), 5 errors = 5 minutes + if (wifiErrorCounter >= 5) { - Serial.println("WiFi reconnected."); - wifiErrorCounter = 0; - wifiOn = true; - oledShowTopRow(); + Serial.println("WiFi unable to reconnect for 5 minutes. Restarting..."); + ESP.restart(); } } - - if (wifiErrorCounter >= 5) + else { - Serial.println("Too many WiFi errors. Restarting..."); - ESP.restart(); + if (!wifiOn) { + Serial.println("WiFi reconnected."); + wifiErrorCounter = 0; + wifiOn = true; + oledShowTopRow(); + } else { + // Reset error counter if we are connected (just to be safe) + wifiErrorCounter = 0; + } } } \ No newline at end of file