ESPAsyncWebServer losing connection over time, is there a watchdog?

I’m using this library:

and everything is working well initially. But after many hours, it stops responding to pings. Is there any way to add a simple watchdog to a project like this?

I’m on a xiao esp32S3 board.

Essentially I’m just using the simple example:

I don’t think that the issue is related to ESPAsyncWebServer but to your wifi connection.

You can use WiFi.onEvent() to monitor the wifi status and implement an automatic reconnection when the wifi connection get lost.

1 Like

Ah, yea. I agree, it’s likely the wifi that gets lost. I will check out that Wifi api, thanks!!

UPDATE: Looking at this resource. Looks very helpful so far.

Interesting. So just losing wifi signal for (short?) time doesn’t disconnect me.

I have setup similar to the readme for ESPAsyncWebServer. Making sure that I only set this shouldReboot flag if server has been started:

void loop() {
  if(shouldReboot){
    ic("Rebooting...");
    ESP.restart();
  }
}
Setup started...

Connecting
[  1454][W][WiFiGeneric.cpp:1407] setTxPower(): Neither AP or STA has been started
[WiFi-event]0
WiFi interface ready
[WiFi-event]2
WiFi client started
[WiFi-event]4
Connected to access point
[WiFi-event]7
o.
Wifi connected! IP:192.168.1.5

Setup Complete, starting server...

but here is my ping showing the board is unreachable for a time when I cover with tin foil. I’ll keep playing, but this seems like it’s going to work well, thanks again!!

Request timeout for icmp_seq 0
Request timeout for icmp_seq 1
Request timeout for icmp_seq 2
Request timeout for icmp_seq 3
Request timeout for icmp_seq 4
Request timeout for icmp_seq 5
Request timeout for icmp_seq 6
Request timeout for icmp_seq 7
Request timeout for icmp_seq 8
Request timeout for icmp_seq 9
Request timeout for icmp_seq 10
Request timeout for icmp_seq 11
Request timeout for icmp_seq 12
Request timeout for icmp_seq 13
Request timeout for icmp_seq 14
Request timeout for icmp_seq 15
Request timeout for icmp_seq 16
Request timeout for icmp_seq 17
Request timeout for icmp_seq 18
Request timeout for icmp_seq 19
Request timeout for icmp_seq 20
Request timeout for icmp_seq 21
Request timeout for icmp_seq 22
Request timeout for icmp_seq 23
Request timeout for icmp_seq 24
Request timeout for icmp_seq 25
64 bytes from 192.168.1.5: icmp_seq=0 ttl=255 time=26632.296 ms
64 bytes from 192.168.1.5: icmp_seq=1 ttl=255 time=25630.972 ms
64 bytes from 192.168.1.5: icmp_seq=2 ttl=255 time=24629.793 ms
64 bytes from 192.168.1.5: icmp_seq=27 ttl=255 time=288.321 ms
64 bytes from 192.168.1.5: icmp_seq=28 ttl=255 time=108.083 ms
64 bytes from 192.168.1.5: icmp_seq=29 ttl=255 time=417.088 ms
64 bytes from 192.168.1.5: icmp_seq=30 ttl=255 time=216.332 ms
64 bytes from 192.168.1.5: icmp_seq=31 ttl=255 time=456.435 ms
64 bytes from 192.168.1.5: icmp_seq=32 ttl=255 time=198.182 ms
64 bytes from 192.168.1.5: icmp_seq=33 ttl=255 time=702.244 ms
64 bytes from 192.168.1.5: icmp_seq=34 ttl=255 time=715.689 ms
64 bytes from 192.168.1.5: icmp_seq=35 ttl=255 time=40.765 ms
Request timeout for icmp_seq 38
Request timeout for icmp_seq 39
Request timeout for icmp_seq 40
Request timeout for icmp_seq 41
Request timeout for icmp_seq 42
Request timeout for icmp_seq 43
Request timeout for icmp_seq 44
Request timeout for icmp_seq 45
Request timeout for icmp_seq 46
Request timeout for icmp_seq 47
Request timeout for icmp_seq 48
Request timeout for icmp_seq 49
Request timeout for icmp_seq 50
Request timeout for icmp_seq 51
Request timeout for icmp_seq 52
64 bytes from 192.168.1.5: icmp_seq=53 ttl=255 time=791.886 ms
64 bytes from 192.168.1.5: icmp_seq=54 ttl=255 time=192.314 ms
64 bytes from 192.168.1.5: icmp_seq=55 ttl=255 time=333.694 ms
Request timeout for icmp_seq 56
64 bytes from 192.168.1.5: icmp_seq=56 ttl=255 time=1010.432 ms
64 bytes from 192.168.1.5: icmp_seq=57 ttl=255 time=475.960 ms
64 bytes from 192.168.1.5: icmp_seq=58 ttl=255 time=1026.618 ms
64 bytes from 192.168.1.5: icmp_seq=59 ttl=255 time=1440.030 ms
64 bytes from 192.168.1.5: icmp_seq=60 ttl=255 time=1962.385 ms
64 bytes from 192.168.1.5: icmp_seq=61 ttl=255 time=1015.741 ms
64 bytes from 192.168.1.5: icmp_seq=62 ttl=255 time=59.394 ms

Your’re watching the wrong branch!
There might be differences. You need to look the 2.x branch:

Ok, some great resources for this area:

https://docs.espressif.com/projects/esp-idf/en/v5.0.1/esp32/api-guides/wifi.html#esp32-wi-fi-api-error-code

and

The latter was helpful for the example titled “Reconnect to Wi-Fi Network After Lost Connection (Wi-Fi Events)”, and the former was great for the full background.

Please note that ARDUINO_EVENT_WIFI_STA_DISCONNECTED is also triggered periodically before and during a connection attempt.

You may need to set a flag to distinguish between a first-time connection and an interrupted Wi-Fi connection.

Were you able to solve your problem?

Thanks for that note! The example in the link I shared actually uses a value “previousMillis” that is initialized to 0 but I think it’s better to initialize it to the current value of millis() because that prevents any reconnection attempt during the first defined interval of 30 seconds.

Hard to say without more testing, but so far at least it’s been running for 12 hours on two esp32s3 xiao boards. I’ll update here after a few days if all goes well.

I’m actually using this with hubitat, and found a nice shared example of a ping status that my Flutter app can query and display for the two xiao boards.

My comment referred to the WiFi.onEvent function.
The randomnerd example does not make use of this, but tracks the state within the loop function.

Yea, sorry for the mixing of things.

Your point is totally valid, and I originally tried to use the error code disconnect (5) to trigger a reset with the flags as you mentioned (boolean to record when server was started, and one to mark an ESP.restart() in main loop), but then found the disconnect and reconnect example in main loop and went that route.

The event handler is great though to see what’s going on better at the very least.

So with the randomnerd example, its working very well. I also added a simple endpoint to the server to be able to query a count of number of times either (I’m running two esp32c3’s) does a restart and one of the two has the count of 1 to confirm it has helped already.

Hello,
I use wifi event and a wathdog to the web socket.
With that, each side can know if connexion is lost.
The event socket close can go thru router.
So if it is a router between the server and the client, the only method is a watchdog.
Be careful, if the web client disconnect the displayed values is not accurate.
Enjoy javascript…
Best regards
Thierry