From 2c71863b0825f1c9c80204229d5dd490546ddb5b Mon Sep 17 00:00:00 2001 From: Fisch Date: Fri, 1 Aug 2025 16:56:41 +0200 Subject: [PATCH] add wifi reconnect timeout with esp reset --- include/wifi_functions.h | 14 ++++++--- src/main.cpp | 67 ++++++++++++++++++++++++++++++++++++++-- 2 files changed, 74 insertions(+), 7 deletions(-) diff --git a/include/wifi_functions.h b/include/wifi_functions.h index f5d064e..544bce7 100644 --- a/include/wifi_functions.h +++ b/include/wifi_functions.h @@ -168,16 +168,20 @@ void messageReceived(String &topic, String &payload) { bool mqtt_loop(unsigned long loopmillis) { static unsigned long last_client_loop=0; + #define RETRY_CONNECTION_INTERVAL 30000 + static unsigned long last_connection_try=0; #define CLIENT_LOOP_INTERVAL 10 //ms. fixes some wifi issues. from example https://registry.platformio.org/libraries/256dpi/MQTT/examples/ESP32DevelopmentBoard/ESP32DevelopmentBoard.ino if (loopmillis >= last_client_loop+CLIENT_LOOP_INTERVAL) { last_client_loop=loopmillis; client.loop(); - static unsigned long last_connection_try=0; - #define RETRY_CONNECTION 60000 - if (!client.connected()) { - if (loopmillis-last_connection_try>RETRY_CONNECTION) { - connect(); + if (client.connected()) { + if (loopmillis-last_connection_try>RETRY_CONNECTION_INTERVAL) { + last_connection_try=loopmillis; + connect(); + if (!client.connected()){ + Serial.println("MQTT reconnect failed"); + } } }else{ return true; diff --git a/src/main.cpp b/src/main.cpp index af3d432..295f5e1 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -1,6 +1,24 @@ #include #include +/* +TODO: error with waterreservoir esp32-s3 wifi connection +serial output wenn fehler: + +_filteredDistance =86.00 +checking wifi.............Wifi no connected. +checking wifi.............Distance reading B=86 Level=404.00 +_filteredDistance =86.00 +checking wifi.............Wifi no connected. +checking wifi.............Distance reading B=86 Level=404.00 +_filteredDistance =86.00 +checking wifi.............Wifi no connected. +checking wifi.............Publish Topic=waterreservoir/uptime Message=932150453 +Distance reading B=85 Level=405.00 +_filteredDistance =86.00 +checking wifi.............Wifi no connected. + +*/ // ESP32-S3 Devkit M-1 Left USB-C: Program, Right USB-C Serial @@ -122,6 +140,7 @@ void setup() { client.begin(mqtt_host, net); client.onMessage(messageReceived); connect(); + publishInfo("localip",WiFi.localIP().toString()); } @@ -205,13 +224,45 @@ void setup() { } void loop() { + unsigned long loopmillis=millis(); - if (mqtt && WiFi.status() != WL_CONNECTED) { + static uint8_t wifi_reconnect_fail_counter=0; + #define WIFI_RECONNECT_INTERVAL 10000 + #define WIFI_MAX_FAILS_REBOOT 6 + static unsigned long last_wifi_reconnect=0; + if (mqtt && WiFi.status() != WL_CONNECTED && loopmillis-last_wifi_reconnect>WIFI_RECONNECT_INTERVAL) { + last_wifi_reconnect=loopmillis; Serial.println("Wifi no connected."); + pixels.setPixelColor(0, pixels.Color(255, 0,0)); + pixels.show(); connect(); + wifi_reconnect_fail_counter++; + if (wifi_reconnect_fail_counter>WIFI_MAX_FAILS_REBOOT) { + wifi_reconnect_fail_counter=0; + #ifdef PIN_NEOPIXEL + pixels.setPixelColor(0, pixels.Color(255, 0,100)); + pixels.show(); + #endif + Serial.println("Too many wifi reconnect fails. Rebooting ESP"); + Serial.flush(); + delay(1000); + ESP.restart(); + while(1){ //infinite loop until rebooted + delay(1); + } + } + if (WiFi.status() == WL_CONNECTED) { //connect successfull + #ifdef PIN_NEOPIXEL + pixels.setPixelColor(0, pixels.Color(0, 0,0)); + pixels.show(); + #endif + Serial.println("Wifi Reconnect Successfull"); + } + }else{ + wifi_reconnect_fail_counter=0; //reset } - unsigned long loopmillis=millis(); + enableTiming=true; //reactivate #ifdef UPTIMEINTERVAL @@ -250,7 +301,13 @@ void loop() { #ifdef FLOW_PIN static bool sentFlowError=false; if (isPumpRunning(1)) { //Check pump 1 flow + #ifdef PIN_NEOPIXEL + pixels.setPixelColor(0, pixels.Color(0, 255,0)); + #endif if (!checkFlow(0.2, 20*1000)) { + #ifdef PIN_NEOPIXEL + pixels.setPixelColor(0, pixels.Color(255, 100,0)); + #endif if (!sentFlowError) { //only sent once sentFlowError=true; Serial.println("pumpError flow too low"); @@ -260,6 +317,9 @@ void loop() { } } }else{ + #ifdef PIN_NEOPIXEL + pixels.setPixelColor(0, pixels.Color(0, 0,0)); + #endif checkFlow(0,0,true); //reset reset timer sentFlowError=false; } @@ -651,6 +711,9 @@ void loop() { }*/ } + #ifdef PIN_NEOPIXEL + pixels.show(); + #endif } } \ No newline at end of file