@@ -960,26 +960,26 @@ esp_err_t WiFiGenericClass::_eventCallback(arduino_event_t *event)
960960 WiFiSTAClass::_setStatus (WL_DISCONNECTED);
961961 }
962962 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
969968 first_connect = false ;
969+ DoReconnect = true ;
970+ log_d (" WiFi Reconnect Running" );
970971 }
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" );
979975 }
980- else if (reason == WIFI_REASON_ASSOC_FAIL){
976+ else if (reason == WIFI_REASON_ASSOC_FAIL) {
981977 WiFiSTAClass::_setStatus (WL_CONNECT_FAILED);
982978 }
979+ if (DoReconnect) {
980+ WiFi.disconnect ();
981+ WiFi.begin ();
982+ }
983983 } else if (event->event_id == ARDUINO_EVENT_WIFI_STA_GOT_IP) {
984984#if ARDUHAL_LOG_LEVEL >= ARDUHAL_LOG_LEVEL_DEBUG
985985 uint8_t * ip = (uint8_t *)&(event->event_info .got_ip .ip_info .ip .addr );
@@ -1063,6 +1063,36 @@ esp_err_t WiFiGenericClass::_eventCallback(arduino_event_t *event)
10631063 return ESP_OK;
10641064}
10651065
1066+ bool WiFiGenericClass::_isReconnectableReason (uint8_t reason) {
1067+ switch (reason) {
1068+ // Timeouts (retry)
1069+ case WIFI_REASON_AUTH_EXPIRE:
1070+ case WIFI_REASON_4WAY_HANDSHAKE_TIMEOUT:
1071+ case WIFI_REASON_GROUP_KEY_UPDATE_TIMEOUT:
1072+ case WIFI_REASON_802_1X_AUTH_FAILED:
1073+ case WIFI_REASON_HANDSHAKE_TIMEOUT:
1074+ // Transient error (reconnect)
1075+ case WIFI_REASON_AUTH_LEAVE:
1076+ case WIFI_REASON_ASSOC_EXPIRE:
1077+ case WIFI_REASON_ASSOC_TOOMANY:
1078+ case WIFI_REASON_NOT_AUTHED:
1079+ case WIFI_REASON_NOT_ASSOCED:
1080+ case WIFI_REASON_ASSOC_NOT_AUTHED:
1081+ case WIFI_REASON_MIC_FAILURE:
1082+ case WIFI_REASON_IE_IN_4WAY_DIFFERS:
1083+ case WIFI_REASON_INVALID_PMKID:
1084+ case WIFI_REASON_BEACON_TIMEOUT:
1085+ case WIFI_REASON_NO_AP_FOUND:
1086+ case WIFI_REASON_ASSOC_FAIL:
1087+ case WIFI_REASON_CONNECTION_FAIL:
1088+ case WIFI_REASON_AP_TSF_RESET:
1089+ case WIFI_REASON_ROAMING:
1090+ return true ;
1091+ default :
1092+ return false ;
1093+ }
1094+ }
1095+
10661096/* *
10671097 * Return the current channel associated with the network
10681098 * @return channel (1-13)
0 commit comments