@@ -142,12 +142,16 @@ static uint8_t wifi_sta_disconn_reason = 0;
142
142
static bool mdns_initialised = false;
143
143
#endif
144
144
145
+ static uint8_t conf_wifi_sta_reconnects = 0 ;
146
+ static uint8_t wifi_sta_reconnects ;
147
+
145
148
// This function is called by the system-event task and so runs in a different
146
149
// thread to the main MicroPython task. It must not raise any Python exceptions.
147
150
static esp_err_t event_handler (void * ctx , system_event_t * event ) {
148
151
switch (event -> event_id ) {
149
152
case SYSTEM_EVENT_STA_START :
150
153
ESP_LOGI ("wifi" , "STA_START" );
154
+ wifi_sta_reconnects = 0 ;
151
155
break ;
152
156
case SYSTEM_EVENT_STA_CONNECTED :
153
157
ESP_LOGI ("network" , "CONNECTED" );
@@ -199,15 +203,23 @@ static esp_err_t event_handler(void *ctx, system_event_t *event) {
199
203
wifi_sta_connected = false;
200
204
if (wifi_sta_connect_requested ) {
201
205
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 ;
209
217
}
210
218
}
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
+ }
211
223
}
212
224
break ;
213
225
}
@@ -361,6 +373,7 @@ STATIC mp_obj_t esp_connect(size_t n_args, const mp_obj_t *pos_args, mp_map_t *k
361
373
ESP_EXCEPTIONS (esp_wifi_set_config (ESP_IF_WIFI_STA , & wifi_sta_config ));
362
374
}
363
375
376
+ wifi_sta_reconnects = 0 ;
364
377
// connect to the WiFi AP
365
378
MP_THREAD_GIL_EXIT ();
366
379
ESP_EXCEPTIONS (esp_wifi_connect ());
@@ -395,7 +408,9 @@ STATIC mp_obj_t esp_status(size_t n_args, const mp_obj_t *args) {
395
408
if (wifi_sta_connected ) {
396
409
// Happy path, connected with IP
397
410
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 )) {
399
414
// No connection or error, but is requested = Still connecting
400
415
return MP_OBJ_NEW_SMALL_INT (STAT_CONNECTING );
401
416
} 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
633
648
cfg .ap .max_connection = mp_obj_get_int (kwargs -> table [i ].value );
634
649
break ;
635
650
}
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
+ }
636
659
default :
637
660
goto unknown ;
638
661
}
@@ -704,6 +727,11 @@ STATIC mp_obj_t esp_config(size_t n_args, const mp_obj_t *args, mp_map_t *kwargs
704
727
val = MP_OBJ_NEW_SMALL_INT (cfg .ap .max_connection );
705
728
break ;
706
729
}
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 ;
707
735
default :
708
736
goto unknown ;
709
737
}
0 commit comments