@@ -960,26 +960,26 @@ esp_err_t WiFiGenericClass::_eventCallback(arduino_event_t *event)
960
960
WiFiSTAClass::_setStatus (WL_DISCONNECTED);
961
961
}
962
962
clearStatusBits (STA_CONNECTED_BIT | STA_HAS_IP_BIT | STA_HAS_IP6_BIT);
963
- if (first_connect && ((reason == WIFI_REASON_AUTH_EXPIRE) ||
964
- (reason >= WIFI_REASON_BEACON_TIMEOUT)))
965
- {
966
- log_d (" WiFi Reconnect Running" );
967
- WiFi.disconnect ();
968
- WiFi.begin ();
963
+
964
+ bool DoReconnect = false ;
965
+ if (reason == WIFI_REASON_ASSOC_LEAVE) { // Voluntarily disconnected. Don't reconnect!
966
+ }
967
+ else if (first_connect) { // Retry once for all failure reasons
969
968
first_connect = false ;
969
+ DoReconnect = true ;
970
+ log_d (" WiFi Reconnect Running" );
970
971
}
971
- else if (WiFi.getAutoReconnect ()){
972
- if ((reason == WIFI_REASON_AUTH_EXPIRE) ||
973
- (reason >= WIFI_REASON_BEACON_TIMEOUT && reason != WIFI_REASON_AUTH_FAIL))
974
- {
975
- log_d (" WiFi AutoReconnect Running" );
976
- WiFi.disconnect ();
977
- WiFi.begin ();
978
- }
972
+ else if (WiFi.getAutoReconnect () && _isReconnectableReason (reason)) {
973
+ DoReconnect = true ;
974
+ log_d (" WiFi AutoReconnect Running" );
979
975
}
980
- else if (reason == WIFI_REASON_ASSOC_FAIL){
976
+ else if (reason == WIFI_REASON_ASSOC_FAIL) {
981
977
WiFiSTAClass::_setStatus (WL_CONNECT_FAILED);
982
978
}
979
+ if (DoReconnect) {
980
+ WiFi.disconnect ();
981
+ WiFi.begin ();
982
+ }
983
983
} else if (event->event_id == ARDUINO_EVENT_WIFI_STA_GOT_IP) {
984
984
#if ARDUHAL_LOG_LEVEL >= ARDUHAL_LOG_LEVEL_DEBUG
985
985
uint8_t * ip = (uint8_t *)&(event->event_info .got_ip .ip_info .ip .addr );
@@ -1063,6 +1063,12 @@ esp_err_t WiFiGenericClass::_eventCallback(arduino_event_t *event)
1063
1063
return ESP_OK;
1064
1064
}
1065
1065
1066
+ bool WiFiGenericClass::_isReconnectableReason (uint8_t reason) {
1067
+ return (reason == WIFI_REASON_AUTH_EXPIRE) ||
1068
+ (reason == WIFI_REASON_ASSOC_EXPIRE) ||
1069
+ (reason >= WIFI_REASON_BEACON_TIMEOUT);
1070
+ }
1071
+
1066
1072
/* *
1067
1073
* Return the current channel associated with the network
1068
1074
* @return channel (1-13)
0 commit comments