Description
Board
n/a
Device Description
n/a
Hardware Configuration
n/a
Version
v2.0.4
IDE Name
n/a
Operating System
n/a
Flash frequency
n/a
PSRAM enabled
yes
Upload speed
n/a
Description
WiFiSTAClass
has an _autoReconnect member, which defaults to true
. When WiFiGeneric::_eventCallback()
handles a ARDUINO_EVENT_WIFI_STA_DISCONNECTED
event, it checks WiFiSTAClass::getAutoReconnect()
and only reconnects if the following criteria is true:
https://github.com/espressif/arduino-esp32/blob/2.0.4/libraries/WiFi/src/WiFiGeneric.cpp#L971
else if(WiFi.getAutoReconnect()){
if((reason == WIFI_REASON_AUTH_EXPIRE) ||
(reason >= WIFI_REASON_BEACON_TIMEOUT && reason != WIFI_REASON_AUTH_FAIL))
{
log_d("WiFi AutoReconnect Running");
WiFi.disconnect();
WiFi.begin();
}
}
The code above excludes reasons such as WIFI_REASON_ASSOC_EXPIRE
, WIFI_REASON_NOT_ASSOCED
, WIFI_REASON_4WAY_HANDSHAKE_TIMEOUT
, WIFI_REASON_GROUP_KEY_UPDATE_TIMEOUT
and perhaps other reasons that may randomly occur.
I propose that reconnection should occur for more than the reasons currently in the code above because of the following:
- The ESP-IDF Programming Guide recommends reconnecting for more reasons:
The most common event handle code for this event in application is to call
esp_wifi_connect()
to reconnect the Wi-Fi. However, if the event is raised becauseesp_wifi_disconnect()
is called, the application should not callesp_wifi_connect()
to reconnect. It is the application's responsibility to distinguish whether the event is caused byesp_wifi_disconnect()
or other reasons. Sometimes a better reconnection strategy is required. Refer to Wi-Fi Reconnect and Scan When Wi-Fi Is Connecting.
s6.2: In the scenario described above, the application event callback function relays WIFI_EVENT_STA_DISCONNECTED to the application task. The recommended actions are: 1) call
esp_wifi_connect()
to reconnect the Wi-Fi, 2) close all sockets, and 3) re-create them if necessary. For details, please refer to WIFI_EVENT_STA_DISCONNECTED.
https://docs.espressif.com/projects/esp-idf/en/latest/esp32/api-guides/wifi.html#wi-fi-reconnect
The station may disconnect due to many reasons, e.g., the connected AP is restarted. It is the application's responsibility to reconnect. The recommended reconnection strategy is to call
esp_wifi_connect()
on receiving event WIFI_EVENT_STA_DISCONNECTED.Sometimes the application needs more complex reconnection strategy:
- If the disconnect event is raised because the
esp_wifi_disconnect()
is called, the application may not want to do the reconnection.- If the
esp_wifi_scan_start()
may be called at anytime, a better reconnection strategy is necessary. Refer to Scan When Wi-Fi Is Connecting.Another thing that need to be considered is that the reconnection may not connect the same AP if there are more than one APs with the same SSID. The reconnection always select current best APs to connect.
- The ESP-IDF examples reconnect for all reasons:
https://github.com/espressif/esp-idf/blob/v4.4.2/examples/wifi/getting_started/station/main/station_example_main.c#L68
https://github.com/espressif/esp-idf/blob/v4.4.2/examples/provisioning/wifi_prov_mgr/main/app_main.c#L100
- ESP-Jumpstart, a "ready reference, a known set of best steps, gathered from previous experience of others" reconnects for all reasons:
- Auto reconnect is already on by default to provide a sensible default to new developers. Why not make that default also sensibly reconnect for all reasons?
Sketch
n/a
Debug Message
n/a
Other Steps to Reproduce
No response
I have checked existing issues, online documentation and the Troubleshooting Guide
- I confirm I have checked existing issues, online documentation and Troubleshooting guide.
Activity
SuGlider commentedon Sep 3, 2022
@RefactorFactory - thanks for reporting such detailed and well explained issue!
Let's check it out and find a good fix.
mrengineer7777 commentedon Sep 29, 2022
I have noticed this on 2.0.3 (platform = https://github.com/tasmota/platform-espressif32/releases/download/v2.0.3/platform-espressif32-2.0.3.zip).
[ 16898][W][WiFiGeneric.cpp:873] _eventCallback(): Reason: 4 - ASSOC_EXPIRE WiFi disconnected 'WIFI_REASON_ASSOC_EXPIRE'
This event happens half the time after programming. Must reboot to get the device to connect.
mrengineer7777 commentedon Oct 11, 2022
The WiFi failure on "ASSOC_EXPIRE" is a serious bug for us, so I plan to submit a PR to fix this issue. Extensive analysis follows. Feedback wanted!
When ARDUINO_EVENT_WIFI_STA_DISCONNECTED occurs,
esp_err_t WiFiGenericClass::_eventCallback(arduino_event_t *event)
attempts to reconnect for the following reasons:These are the current disconnect reasons from esp_wifi_types.h, wifi_err_reason_t:
Based on my understanding of scan-when-wi-fi-is-connecting , I would break down the disconnect reasons as:
Since the initial retry fires on AUTH_FAIL, I would argue it should retry for ALL disconnect reasons except WIFI_REASON_ASSOC_LEAVE.
I believe the auto-reconnect should trigger for all Timeout and Transient errors. I don't know what to do with the Unknown reasons.
Note to self: will be submitting PR against WiFiGeneric.cpp
WiFi Should Reconnect For Most Reasons
WiFi Should Reconnect For Most Reasons
WiFi Should Reconnect For Most Reasons
WiFi Should Reconnect For Most Reasons
RefactorFactory commentedon Oct 12, 2022
While #7344 is an great improvement over the existing code and it handles all the WiFi disconnect reasons that I've personally seen, why not just do as the official ESP-IDF samples?
Consider if there is a problem with #7344 in the future. At that time, if we ask Espressif, they might say "hmm, we never encountered such a problem because we never do that in our ESP-IDF samples (and perhaps other tests)." This hypothetical problem could have been avoided by matching what Espressif does in their code.
Another way to look at this: is it a "bug" in the ESP-IDF samples that they don't do something as complicated as we're suggesting for Arduino-esp32? Perhaps the ESP-IDF samples are "simple" because they're just samples, so it's ok for them, but not real projects?
BTW, I think esphome retries on all disconnect reasons, but I'm not 100% sure because their code is more complicated.
9 remaining items