summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorThomas Wenrich <twenrich@gmail.com>2021-06-13 16:22:49 +0200
committerDamien George <damien@micropython.org>2021-06-17 18:48:06 +1000
commit060066804a4543e79c0bdc60a1221bb8628982e9 (patch)
treee8119730bdee2ce206e92ea90e76353c2e9529da
parent48437cec4547a21af73102c1ccdf079f63fff534 (diff)
esp32/modnetwork: Add "reconnects" option to WLAN STA interface.
This adds a wlan.config(reconnects=N) option to set the number of reconnect attempts that will be made if the WLAN connection goes down. The default is N=-1 (infinite retries, current behavior). Setting wlan.config(reconnects=0) will disable the reconnect attempts. A nice side effect of reconnects=0 is that wlan.status() will report the disconnect reason now. See related issue #5326.
-rw-r--r--ports/esp32/modnetwork.c44
1 files changed, 36 insertions, 8 deletions
diff --git a/ports/esp32/modnetwork.c b/ports/esp32/modnetwork.c
index 9a8d5b2e1..e2bd20a1a 100644
--- a/ports/esp32/modnetwork.c
+++ b/ports/esp32/modnetwork.c
@@ -142,12 +142,16 @@ static uint8_t wifi_sta_disconn_reason = 0;
static bool mdns_initialised = false;
#endif
+static uint8_t conf_wifi_sta_reconnects = 0;
+static uint8_t wifi_sta_reconnects;
+
// This function is called by the system-event task and so runs in a different
// thread to the main MicroPython task. It must not raise any Python exceptions.
static esp_err_t event_handler(void *ctx, system_event_t *event) {
switch (event->event_id) {
case SYSTEM_EVENT_STA_START:
ESP_LOGI("wifi", "STA_START");
+ wifi_sta_reconnects = 0;
break;
case SYSTEM_EVENT_STA_CONNECTED:
ESP_LOGI("network", "CONNECTED");
@@ -199,15 +203,23 @@ static esp_err_t event_handler(void *ctx, system_event_t *event) {
wifi_sta_connected = false;
if (wifi_sta_connect_requested) {
wifi_mode_t mode;
- if (esp_wifi_get_mode(&mode) == ESP_OK) {
- if (mode & WIFI_MODE_STA) {
- // STA is active so attempt to reconnect.
- esp_err_t e = esp_wifi_connect();
- if (e != ESP_OK) {
- ESP_LOGI("wifi", "error attempting to reconnect: 0x%04x", e);
- }
+ if (esp_wifi_get_mode(&mode) != ESP_OK) {
+ break;
+ }
+ if (!(mode & WIFI_MODE_STA)) {
+ break;
+ }
+ if (conf_wifi_sta_reconnects) {
+ ESP_LOGI("wifi", "reconnect counter=%d, max=%d",
+ wifi_sta_reconnects, conf_wifi_sta_reconnects);
+ if (++wifi_sta_reconnects >= conf_wifi_sta_reconnects) {
+ break;
}
}
+ esp_err_t e = esp_wifi_connect();
+ if (e != ESP_OK) {
+ ESP_LOGI("wifi", "error attempting to reconnect: 0x%04x", e);
+ }
}
break;
}
@@ -361,6 +373,7 @@ STATIC mp_obj_t esp_connect(size_t n_args, const mp_obj_t *pos_args, mp_map_t *k
ESP_EXCEPTIONS(esp_wifi_set_config(ESP_IF_WIFI_STA, &wifi_sta_config));
}
+ wifi_sta_reconnects = 0;
// connect to the WiFi AP
MP_THREAD_GIL_EXIT();
ESP_EXCEPTIONS(esp_wifi_connect());
@@ -395,7 +408,9 @@ STATIC mp_obj_t esp_status(size_t n_args, const mp_obj_t *args) {
if (wifi_sta_connected) {
// Happy path, connected with IP
return MP_OBJ_NEW_SMALL_INT(STAT_GOT_IP);
- } else if (wifi_sta_connect_requested) {
+ } else if (wifi_sta_connect_requested
+ && (conf_wifi_sta_reconnects == 0
+ || wifi_sta_reconnects < conf_wifi_sta_reconnects)) {
// No connection or error, but is requested = Still connecting
return MP_OBJ_NEW_SMALL_INT(STAT_CONNECTING);
} else if (wifi_sta_disconn_reason == 0) {
@@ -633,6 +648,14 @@ STATIC mp_obj_t esp_config(size_t n_args, const mp_obj_t *args, mp_map_t *kwargs
cfg.ap.max_connection = mp_obj_get_int(kwargs->table[i].value);
break;
}
+ case QS(MP_QSTR_reconnects): {
+ int reconnects = mp_obj_get_int(kwargs->table[i].value);
+ req_if = WIFI_IF_STA;
+ // parameter reconnects == -1 means to retry forever.
+ // here means conf_wifi_sta_reconnects == 0 to retry forever.
+ conf_wifi_sta_reconnects = (reconnects == -1) ? 0 : reconnects + 1;
+ break;
+ }
default:
goto unknown;
}
@@ -704,6 +727,11 @@ STATIC mp_obj_t esp_config(size_t n_args, const mp_obj_t *args, mp_map_t *kwargs
val = MP_OBJ_NEW_SMALL_INT(cfg.ap.max_connection);
break;
}
+ case QS(MP_QSTR_reconnects):
+ req_if = WIFI_IF_STA;
+ int rec = conf_wifi_sta_reconnects - 1;
+ val = MP_OBJ_NEW_SMALL_INT(rec);
+ break;
default:
goto unknown;
}