Skip to content

WiFi auto reconnect should occur for all disconnect reasons #7210

Closed
@RefactorFactory

Description

@RefactorFactory

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:

  1. The ESP-IDF Programming Guide recommends reconnecting for more reasons:

https://docs.espressif.com/projects/esp-idf/en/latest/esp32/api-guides/wifi.html#wifi-event-sta-disconnected

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 because esp_wifi_disconnect() is called, the application should not call esp_wifi_connect() to reconnect. It is the application's responsibility to distinguish whether the event is caused by esp_wifi_disconnect() or other reasons. Sometimes a better reconnection strategy is required. Refer to Wi-Fi Reconnect and Scan When Wi-Fi Is Connecting.

https://docs.espressif.com/projects/esp-idf/en/latest/esp32/api-guides/wifi.html#wi-fi-disconnect-phase

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.

  1. 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

  1. ESP-Jumpstart, a "ready reference, a known set of best steps, gathered from previous experience of others" reconnects for all reasons:

https://github.com/espressif/esp-jumpstart/blob/bf3e26f2295730c8f6e9e7c08c897d2155064c5c/7_mfg/main/app_wifi.c#L112

  1. 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.

Metadata

Metadata

Assignees

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions