Skip to content

Commit 0600668

Browse files
twenrichdpgeorge
authored andcommitted
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.
1 parent 48437ce commit 0600668

File tree

1 file changed

+36
-8
lines changed

1 file changed

+36
-8
lines changed

ports/esp32/modnetwork.c

Lines changed: 36 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -142,12 +142,16 @@ static uint8_t wifi_sta_disconn_reason = 0;
142142
static bool mdns_initialised = false;
143143
#endif
144144

145+
static uint8_t conf_wifi_sta_reconnects = 0;
146+
static uint8_t wifi_sta_reconnects;
147+
145148
// This function is called by the system-event task and so runs in a different
146149
// thread to the main MicroPython task. It must not raise any Python exceptions.
147150
static esp_err_t event_handler(void *ctx, system_event_t *event) {
148151
switch (event->event_id) {
149152
case SYSTEM_EVENT_STA_START:
150153
ESP_LOGI("wifi", "STA_START");
154+
wifi_sta_reconnects = 0;
151155
break;
152156
case SYSTEM_EVENT_STA_CONNECTED:
153157
ESP_LOGI("network", "CONNECTED");
@@ -199,15 +203,23 @@ static esp_err_t event_handler(void *ctx, system_event_t *event) {
199203
wifi_sta_connected = false;
200204
if (wifi_sta_connect_requested) {
201205
wifi_mode_t mode;
202-
if (esp_wifi_get_mode(&mode) == ESP_OK) {
203-
if (mode & WIFI_MODE_STA) {
204-
// STA is active so attempt to reconnect.
205-
esp_err_t e = esp_wifi_connect();
206-
if (e != ESP_OK) {
207-
ESP_LOGI("wifi", "error attempting to reconnect: 0x%04x", e);
208-
}
206+
if (esp_wifi_get_mode(&mode) != ESP_OK) {
207+
break;
208+
}
209+
if (!(mode & WIFI_MODE_STA)) {
210+
break;
211+
}
212+
if (conf_wifi_sta_reconnects) {
213+
ESP_LOGI("wifi", "reconnect counter=%d, max=%d",
214+
wifi_sta_reconnects, conf_wifi_sta_reconnects);
215+
if (++wifi_sta_reconnects >= conf_wifi_sta_reconnects) {
216+
break;
209217
}
210218
}
219+
esp_err_t e = esp_wifi_connect();
220+
if (e != ESP_OK) {
221+
ESP_LOGI("wifi", "error attempting to reconnect: 0x%04x", e);
222+
}
211223
}
212224
break;
213225
}
@@ -361,6 +373,7 @@ STATIC mp_obj_t esp_connect(size_t n_args, const mp_obj_t *pos_args, mp_map_t *k
361373
ESP_EXCEPTIONS(esp_wifi_set_config(ESP_IF_WIFI_STA, &wifi_sta_config));
362374
}
363375

376+
wifi_sta_reconnects = 0;
364377
// connect to the WiFi AP
365378
MP_THREAD_GIL_EXIT();
366379
ESP_EXCEPTIONS(esp_wifi_connect());
@@ -395,7 +408,9 @@ STATIC mp_obj_t esp_status(size_t n_args, const mp_obj_t *args) {
395408
if (wifi_sta_connected) {
396409
// Happy path, connected with IP
397410
return MP_OBJ_NEW_SMALL_INT(STAT_GOT_IP);
398-
} else if (wifi_sta_connect_requested) {
411+
} else if (wifi_sta_connect_requested
412+
&& (conf_wifi_sta_reconnects == 0
413+
|| wifi_sta_reconnects < conf_wifi_sta_reconnects)) {
399414
// No connection or error, but is requested = Still connecting
400415
return MP_OBJ_NEW_SMALL_INT(STAT_CONNECTING);
401416
} 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
633648
cfg.ap.max_connection = mp_obj_get_int(kwargs->table[i].value);
634649
break;
635650
}
651+
case QS(MP_QSTR_reconnects): {
652+
int reconnects = mp_obj_get_int(kwargs->table[i].value);
653+
req_if = WIFI_IF_STA;
654+
// parameter reconnects == -1 means to retry forever.
655+
// here means conf_wifi_sta_reconnects == 0 to retry forever.
656+
conf_wifi_sta_reconnects = (reconnects == -1) ? 0 : reconnects + 1;
657+
break;
658+
}
636659
default:
637660
goto unknown;
638661
}
@@ -704,6 +727,11 @@ STATIC mp_obj_t esp_config(size_t n_args, const mp_obj_t *args, mp_map_t *kwargs
704727
val = MP_OBJ_NEW_SMALL_INT(cfg.ap.max_connection);
705728
break;
706729
}
730+
case QS(MP_QSTR_reconnects):
731+
req_if = WIFI_IF_STA;
732+
int rec = conf_wifi_sta_reconnects - 1;
733+
val = MP_OBJ_NEW_SMALL_INT(rec);
734+
break;
707735
default:
708736
goto unknown;
709737
}

0 commit comments

Comments
 (0)