diff --git a/src/main.cpp b/src/main.cpp index e05a349..26e3ecc 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -100,12 +100,20 @@ const unsigned long amsSendInterval = 60000; // 1 minute uint8_t weightSend = 0; int16_t lastWeight = 0; -uint8_t wifiErrorCounter = 0; + +unsigned long lastWifiCheckTime = 0; +const unsigned long wifiCheckInterval = 60000; // Überprüfe alle 60 Sekunden (60000 ms) // ##### PROGRAM START ##### void loop() { unsigned long currentMillis = millis(); + // Überprüfe regelmäßig die WLAN-Verbindung + if (millis() - lastWifiCheckTime > wifiCheckInterval) { + checkWiFiConnection(); + lastWifiCheckTime = millis(); + } + // Send AMS Data min every Minute if (currentMillis - lastAmsSendTime >= amsSendInterval) { diff --git a/src/wlan.cpp b/src/wlan.cpp index f527cdf..c1fd90e 100644 --- a/src/wlan.cpp +++ b/src/wlan.cpp @@ -8,48 +8,78 @@ WiFiManager wm; bool wm_nonblocking = false; +uint8_t wifiErrorCounter = 0; void initWiFi() { - // Optimierte WiFi-Einstellungen - WiFi.mode(WIFI_STA); // explicitly set mode, esp defaults to STA+AP - WiFi.setSleep(false); // disable sleep mode - esp_wifi_set_ps(WIFI_PS_NONE); - - // Maximale Sendeleistung - WiFi.setTxPower(WIFI_POWER_19_5dBm); // Set maximum transmit power + // Optimierte WiFi-Einstellungen + WiFi.mode(WIFI_STA); // explicitly set mode, esp defaults to STA+AP + WiFi.setSleep(false); // disable sleep mode + esp_wifi_set_ps(WIFI_PS_NONE); - // Optimiere TCP/IP Stack - esp_wifi_set_protocol(WIFI_IF_STA, WIFI_PROTOCOL_11B | WIFI_PROTOCOL_11G | WIFI_PROTOCOL_11N); - - // Aktiviere WiFi-Roaming für bessere Stabilität - esp_wifi_set_rssi_threshold(-80); + // Maximale Sendeleistung + WiFi.setTxPower(WIFI_POWER_19_5dBm); // Set maximum transmit power + + // Optimiere TCP/IP Stack + esp_wifi_set_protocol(WIFI_IF_STA, WIFI_PROTOCOL_11B | WIFI_PROTOCOL_11G | WIFI_PROTOCOL_11N); - if(wm_nonblocking) wm.setConfigPortalBlocking(false); - wm.setConfigPortalTimeout(320); // Portal nach 5min schließen + // Aktiviere WiFi-Roaming für bessere Stabilität + esp_wifi_set_rssi_threshold(-80); + + if(wm_nonblocking) wm.setConfigPortalBlocking(false); + wm.setConfigPortalTimeout(320); // Portal nach 5min schließen + + oledShowTopRow(); + oledShowMessage("WiFi Setup"); + bool res; + // res = wm.autoConnect(); // auto generated AP name from chipid + res = wm.autoConnect("FilaMan"); // anonymous ap + // res = wm.autoConnect("spoolman","password"); // password protected ap + + if(!res) { + Serial.println("Failed to connect or hit timeout"); + // ESP.restart(); oledShowTopRow(); - oledShowMessage("WiFi Setup"); - - bool res; - // res = wm.autoConnect(); // auto generated AP name from chipid - res = wm.autoConnect("FilaMan"); // anonymous ap - // res = wm.autoConnect("spoolman","password"); // password protected ap - - if(!res) { - Serial.println("Failed to connect or hit timeout"); - // ESP.restart(); - oledShowTopRow(); - oledShowMessage("WiFi not connected Check Portal"); + oledShowMessage("WiFi not connected Check Portal"); + } + else { + wifiOn = true; + + //if you get here you have connected to the WiFi + Serial.println("connected...yeey :)"); + Serial.print("IP address: "); + Serial.println(WiFi.localIP()); + + oledShowTopRow(); + display.display(); + } +} + +void checkWiFiConnection() { + if (WiFi.status() != WL_CONNECTED) + { + Serial.println("WiFi connection lost. Reconnecting..."); + oledShowTopRow(); + oledShowMessage("WiFi reconnecting..."); + WiFi.reconnect(); // Versuche, die Verbindung wiederherzustellen + delay(5000); // Warte 5 Sekunden, bevor erneut geprüft wird + if (WiFi.status() != WL_CONNECTED) + { + Serial.println("Failed to reconnect. Restarting WiFi..."); + WiFi.disconnect(); + delay(1000); + wifiErrorCounter++; + //initWiFi(); } - else { - wifiOn = true; - - //if you get here you have connected to the WiFi - Serial.println("connected...yeey :)"); - Serial.print("IP address: "); - Serial.println(WiFi.localIP()); - - oledShowTopRow(); - display.display(); + else + { + wifiErrorCounter = 0; } - } \ No newline at end of file + } + + if (wifiErrorCounter >= 5) + { + Serial.println("Too many WiFi errors. Restarting..."); + ESP.restart(); + } +} \ No newline at end of file diff --git a/src/wlan.h b/src/wlan.h index 2ba117f..7c22caf 100644 --- a/src/wlan.h +++ b/src/wlan.h @@ -4,5 +4,6 @@ #include void initWiFi(); +void checkWiFiConnection(); #endif \ No newline at end of file