add wifi reconnect timeout with esp reset
This commit is contained in:
parent
42b442d209
commit
2c71863b08
2 changed files with 74 additions and 7 deletions
|
@ -168,16 +168,20 @@ void messageReceived(String &topic, String &payload) {
|
||||||
|
|
||||||
bool mqtt_loop(unsigned long loopmillis) {
|
bool mqtt_loop(unsigned long loopmillis) {
|
||||||
static unsigned long last_client_loop=0;
|
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
|
#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) {
|
if (loopmillis >= last_client_loop+CLIENT_LOOP_INTERVAL) {
|
||||||
last_client_loop=loopmillis;
|
last_client_loop=loopmillis;
|
||||||
client.loop();
|
client.loop();
|
||||||
|
|
||||||
static unsigned long last_connection_try=0;
|
if (client.connected()) {
|
||||||
#define RETRY_CONNECTION 60000
|
if (loopmillis-last_connection_try>RETRY_CONNECTION_INTERVAL) {
|
||||||
if (!client.connected()) {
|
last_connection_try=loopmillis;
|
||||||
if (loopmillis-last_connection_try>RETRY_CONNECTION) {
|
connect();
|
||||||
connect();
|
if (!client.connected()){
|
||||||
|
Serial.println("MQTT reconnect failed");
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}else{
|
}else{
|
||||||
return true;
|
return true;
|
||||||
|
|
67
src/main.cpp
67
src/main.cpp
|
@ -1,6 +1,24 @@
|
||||||
#include <Arduino.h>
|
#include <Arduino.h>
|
||||||
#include <Wire.h>
|
#include <Wire.h>
|
||||||
|
|
||||||
|
/*
|
||||||
|
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
|
// 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.begin(mqtt_host, net);
|
||||||
client.onMessage(messageReceived);
|
client.onMessage(messageReceived);
|
||||||
connect();
|
connect();
|
||||||
|
publishInfo("localip",WiFi.localIP().toString());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
@ -205,13 +224,45 @@ void setup() {
|
||||||
}
|
}
|
||||||
|
|
||||||
void loop() {
|
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.");
|
Serial.println("Wifi no connected.");
|
||||||
|
pixels.setPixelColor(0, pixels.Color(255, 0,0));
|
||||||
|
pixels.show();
|
||||||
connect();
|
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
|
enableTiming=true; //reactivate
|
||||||
|
|
||||||
#ifdef UPTIMEINTERVAL
|
#ifdef UPTIMEINTERVAL
|
||||||
|
@ -250,7 +301,13 @@ void loop() {
|
||||||
#ifdef FLOW_PIN
|
#ifdef FLOW_PIN
|
||||||
static bool sentFlowError=false;
|
static bool sentFlowError=false;
|
||||||
if (isPumpRunning(1)) { //Check pump 1 flow
|
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)) {
|
if (!checkFlow(0.2, 20*1000)) {
|
||||||
|
#ifdef PIN_NEOPIXEL
|
||||||
|
pixels.setPixelColor(0, pixels.Color(255, 100,0));
|
||||||
|
#endif
|
||||||
if (!sentFlowError) { //only sent once
|
if (!sentFlowError) { //only sent once
|
||||||
sentFlowError=true;
|
sentFlowError=true;
|
||||||
Serial.println("pumpError flow too low");
|
Serial.println("pumpError flow too low");
|
||||||
|
@ -260,6 +317,9 @@ void loop() {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}else{
|
}else{
|
||||||
|
#ifdef PIN_NEOPIXEL
|
||||||
|
pixels.setPixelColor(0, pixels.Color(0, 0,0));
|
||||||
|
#endif
|
||||||
checkFlow(0,0,true); //reset reset timer
|
checkFlow(0,0,true); //reset reset timer
|
||||||
sentFlowError=false;
|
sentFlowError=false;
|
||||||
}
|
}
|
||||||
|
@ -651,6 +711,9 @@ void loop() {
|
||||||
}*/
|
}*/
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#ifdef PIN_NEOPIXEL
|
||||||
|
pixels.show();
|
||||||
|
#endif
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
Loading…
Add table
Reference in a new issue