Page 2 of 2

Re: Why is it not good practice to call esp_wifi_connect() after esp_wifi_disconnect()?

Posted: Sun Mar 24, 2024 3:15 pm
by maria36
Calling esp_wifi_connect() immediately after esp_wifi_disconnect() can lead to unpredictable behavior and potential conflicts in the Wi-Fi connection process. It's important to allow sufficient time for the disconnection process to complete before initiating a new connection to ensure stability and avoid potential race conditions or resource conflicts.

Re: Why is it not good practice to call esp_wifi_connect() after esp_wifi_disconnect()?

Posted: Mon Mar 25, 2024 2:41 am
by ESP_Sprite
Because esp_wifi_disconnect() doesn't disconnect WiFi and then return; rather it tells the WiFi stack to initiate a disconnect. The WiFi stack will then in the background go through all things needed to disconnect gracefully (e.g. inform the AP that the connection is broken) and will finally return an event telling you the ESP is actually disconnected. If you call esp_wifi_connect() directly after esp_wifi_disconnect() (without waiting for that event), the disconnect procedure might still be underway and results will be unpredictable.

Re: Why is it not good practice to call esp_wifi_connect() after esp_wifi_disconnect()?

Posted: Fri May 10, 2024 1:44 pm
by karunt
@ESP_Sprite - so what is an appropriate amount of time to allow for esp_wifi_disconnect() and corresponding code to work its magic before calling esp_wifi_connect() again? I ask because I've waited as long as 5 minutes (which I think is long enough) after calling esp_wifi_disconnect() before re-trying esp_wifi_connect(), and the alternating success/failure/success ... pattern described in my earlier post persists.

Also, what event is emitted once the wifi disconnection is completed so that we can prevent esp_wifi_connect() from happening again till this event is emitted?

Re: Why is it not good practice to call esp_wifi_connect() after esp_wifi_disconnect()?

Posted: Fri May 10, 2024 2:25 pm
by ESP_Sprite
karunt wrote:
Fri May 10, 2024 1:44 pm
@ESP_Sprite - so what is an appropriate amount of time to allow for esp_wifi_disconnect() and corresponding code to work its magic before calling esp_wifi_connect() again? I ask because I've waited as long as 5 minutes (which I think is long enough) after calling esp_wifi_disconnect() before re-trying esp_wifi_connect(), and the alternating success/failure/success ... pattern described in my earlier post persists.

Also, what event is emitted once the wifi disconnection is completed so that we can prevent esp_wifi_connect() from happening again till this event is emitted?
Specifically the WIFI_EVENT_STA_DISCONNECTED event. See e.g. this WiFi example for an illustration on how to reconnect after a disconnect.