ESP32: Wifi peripheral configuration is not reset before/after a soft reset or light/deep sleep.
After a soft reset, micropython restarts with the wifi peripheral configuration continuing as set prior to the reset.
This may be regarded as a feature by some (eg. continued connection to wifi Access Point) after reset, however, it
- requires additional unexpected and undocumented protection code in startup scripts
- eg.
sta.connect(ssid, pwd)will raise an exception after a soft reset if already connected().
- eg.
- difficult to diagnose errors can arise in circumstances where the peripheral is left in a bad state or a state unexpected by the users or the micropython software after reset.
I have made the mistake of chasing apparent bugs in my apps (or while developing PR #6515) only for them to resolve after a hard reset. I have also dealt with a number of users reporting issues which have turned out to be due to the wifi peripheral being in a bad state and not being re-initialised after a soft reset. I suspect this may also be contributing to user frustration with apparent lack of reproducibility or reliability.
I propose to prepare a PR which:
- calls esp_wifi_stop() and esp_wifi_deinit() before soft reset
- also before deepsleep/lightsleep() as required according to W-fi Bluetoth Sleep Modes
An alternative may be to call esp_wifi_deinit() on bootup before esp_wifi_init().
I am aware that some may rely on the current (undocumented) behaviour, though I believe that for predictability and reproducibility it is much more desirable to allways start micropython with the peripheral in a known state.
ports/esp32: Deinit wifi peripheral on soft_reset and stop wifi before light/deep sleep.
This PR adds support for deinitialising the wifi peripheral prior to performing a soft reset. Besides calling esp_wifi_deinit(), it also resets the static state variables in network_wlan.c to their default values.
In this way, the wifi peripheral is always in a known state after a soft reset.
Adds:
network_wlan_deinit_wifi(),network_wlan_stop_wifi(), andnetwork_wlan_init_wifi()tonetwork_wlan.c;WLAN.deinit()method;- so users can invoke a reset of the wifi peripheral and module state
- (as well as freeing wifi and mdns resources).
- call to
network_wlan_deinit_wifi()before soft_reset inmain.c; and - calls to
network_wlan_stop_wifi()before light or deep sleep inmachine.c;- as recommended in Wifi/Bluetooth and Sleep Modes
- else the wifi peripheral may become unusable after light/deep sleep.
- as recommended in Wifi/Bluetooth and Sleep Modes
Resolves #8994.