RFC: offical API to save WiFi-connection parameter
After @MarkR42 explained that the automatic storing of the WiFi-connection parameter can wear-out the flash of the ESP8266 many discussions came up: #2510, #2547, #2564 and http://forum.micropython.org/viewtopic.php?f=16&t=2631
This explains the importance of this issue.
In principle, the feature of saving the connection parameter is due to the ESP8266 SDK and strictly speaking not compatible to the existing HW API (https://github.com/micropython/micropython/wiki/Hardware-API#the-wlan-class). But on the other hand it provides a nice user experience. Also @pfalcon explained that this is a common feature in smart phones, computers, … So we have to expect, that the ESP8266 will not be the only device that supports saving of WiFi-connection parameters.
But, this side effects can lead to permanent hardware damage. Thus, we need a way to at least deactivate this feature.
Therefore, we have to find an agreement about the method, default behaviour and API names. That is the reason for this issue.
There are already three different possible methods described:
a) #2510 additional parameter save_to_flash in wlan.connect()
b) https://github.com/micropython/micropython/pull/2510#issuecomment-255714114 additional function esp.persist_wlan(val) which allows the user to turn on/off the feature of saving WiFi settings to flash
c) #2564 additional function esp.save_config() to save configuration to flash on demand
As default behaviour we have the possibilities:
wlan.connect()andwlan.active()always save to flash. That will probably lead to permanent hardware damage and is not possible for devices that do not support the saving (e.g. the WiPy board).wlan.connect()andwlan.active()don't always save to flash. This will break the user experience of the ESP8266 users.
What are your comments?
ports/esp8266: Add auto_connect & reconnects options to WLAN.config().
Adds support for:
- disabling/enabling the wifi auto-connect "feature" of the esp8266 (
WLAN.config(auto_connect=True/False)); and - setting the reconnect policy for the esp8266 after disconnecting from wifi (
WLAN.config(reconnects=xx)).
Motivation
The auto-connect feature is regarded by some as a mis-feature of the esp8266 and is one source of incompatible behaviour for code running on esp32 and esp8266. The auto_connect=False option allows users to ensure their esp8266 device's behaviour is deterministically derived from startup code. The reconnects option provides similar (but not the same) functionality to the reconnects opton on ESP32.
The semantics of the reconnects option differ from the esp32 in important ways:
- reconnects=0:
- ESP32 and ESP8266: No attempt is made to reconnect after disconnection from AP
- reconnects>=1:
- ESP32: Sets the maximum number of times the device will attempt to reconnect to AP
- ESP8266: Sets the number of times device will attempt to retry dhcp after disconnecting.
Note: WLAN.config("reconnects") is not supported since the esp8266 SDK provide no mechanism for querying the current settings.
This PR adds:
WLAN.config(auto_connect=True/False)to enable/disable the ESP8266 from automatically connecting to wifi networks after WLAN.active(True).WLAN.config(reconnects=xx)wherexxis an integer to limit the number of times to attempt to reconnect after becoming disconnected from a wifi access point.