WiFi.setAutoReconnect function

Gilbert
Posts: 41
Joined: Wed Sep 16, 2020 2:58 pm

WiFi.setAutoReconnect function

Postby Gilbert » Wed Mar 27, 2024 2:39 pm

Hello,
I'm new to ESP32 WiFi and I'm exploring some of the features.
When I set AutoReconnect to 'true' using the WiFi.setAutoReconnect() function I expect that ESP32 will immediately attempt to re-establish the WiFi connection when that connection is lost. To test this, I used the function WiFi.disconnect(false) by inputting a command using the Serial monitor in the IDE - 'false' implies that the WiFi radio stays ON - and I get the WiFi event 'ARDUINO_EVENT_WIFI_STA_DISCONNECTED', which is what I expected. However, since WiFi has been configured to auto-reconnect, I expect a new event like ' ARDUINO_EVENT_WIFI_STA_CONNECTED', but nothing happens. The WiFi remains disconnected. How is the auto-reconnect supposed act when the connection is lost?
I also tried with WiFi.disconnect(true), which implies that the radio is turned off, but also then, the WiFi remains disconnected - which makes more sense than in the previous case.
Of course I can restart the WiFi using Wifi.begin(), but that means that I have to monitor the Wifi status in my code and manage the connection by my own code. Kinda defeats the purpose (at least that is my expectation) to have a feaure for automatic reconnection?
To be honest, I haven't tried this by turning my AP off. Would that make a difference?

lbernstone
Posts: 829
Joined: Mon Jul 22, 2019 3:20 pm

Re: WiFi.setAutoReconnect function

Postby lbernstone » Wed Mar 27, 2024 4:41 pm

If you manually turn it off with a disconnect, it doesn't reconnect. It is equivalent to ending the session. Reconnect means that if something happens (typically the router reboots), the client will attempt to re-establish the existing session there. In practice, I find it insufficient for much the same reason as your disconnect there- sometimes the router will actively tell a client to disconnect, or nullify their authorization, and then you are left with a stuck device. Use WiFi events instead so you can more closely manage what to do in what situation.

Gilbert
Posts: 41
Joined: Wed Sep 16, 2020 2:58 pm

Re: WiFi.setAutoReconnect function

Postby Gilbert » Wed Mar 27, 2024 8:32 pm

Thanks for the clarification. Bottom line is that I have to monitor the connection state by my own code and I cannot rely 100 % on the automatic reconnection.
I also assume that having a wifi connection does not warrant that I'm connected to the internet, right? I'm not sure what my router does when the connection with my ISP is broken.
With that in mind I plan to ping a service like Google or Microsoft from time to time, just make sure that my internet stuff works - or, in case of the opposite, I can figure out what's going on.

lbernstone
Posts: 829
Joined: Mon Jul 22, 2019 3:20 pm

Re: WiFi.setAutoReconnect function

Postby lbernstone » Wed Mar 27, 2024 10:13 pm

Even better, ping what you actually want to get to. Google's dns servers have pretty good uptime. The endpoint you target may not.

Who is online

Users browsing this forum: No registered users and 71 guests