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

maria36
Posts: 1
Joined: Sun Mar 24, 2024 3:09 pm

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

Postby maria36 » Sun Mar 24, 2024 3:15 pm

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.

ESP_Sprite
Posts: 9729
Joined: Thu Nov 26, 2015 4:08 am

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

Postby ESP_Sprite » Mon Mar 25, 2024 2:41 am

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.

karunt
Posts: 93
Joined: Sat Apr 03, 2021 7:58 am

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

Postby karunt » 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?

ESP_Sprite
Posts: 9729
Joined: Thu Nov 26, 2015 4:08 am

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

Postby ESP_Sprite » Fri May 10, 2024 2:25 pm

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.

Who is online

Users browsing this forum: No registered users and 89 guests