@@ -963,26 +963,26 @@ esp_err_t WiFiGenericClass::_eventCallback(arduino_event_t *event)
963963 WiFiSTAClass::_setStatus (WL_DISCONNECTED);
964964 }
965965 clearStatusBits (STA_CONNECTED_BIT | STA_HAS_IP_BIT | STA_HAS_IP6_BIT);
966-  if (first_connect && ((reason == WIFI_REASON_AUTH_EXPIRE) ||
967-  (reason >= WIFI_REASON_BEACON_TIMEOUT)))
968-  {
969-  log_d (" WiFi Reconnect Running"  );
970-  WiFi.disconnect ();
971-  WiFi.begin ();
966+ 
967+  bool  DoReconnect = false ;
968+  if (reason == WIFI_REASON_ASSOC_LEAVE) { // Voluntarily disconnected. Don't reconnect!
969+  }
970+  else  if (first_connect) { // Retry once for all failure reasons
972971 first_connect = false ;
972+  DoReconnect = true ;
973+  log_d (" WiFi Reconnect Running"  );
973974 }
974-  else  if (WiFi.getAutoReconnect ()){
975-  if ((reason == WIFI_REASON_AUTH_EXPIRE) ||
976-  (reason >= WIFI_REASON_BEACON_TIMEOUT && reason != WIFI_REASON_AUTH_FAIL))
977-  {
978-  log_d (" WiFi AutoReconnect Running"  );
979-  WiFi.disconnect ();
980-  WiFi.begin ();
981-  }
975+  else  if (WiFi.getAutoReconnect () && _isReconnectableReason (reason)) {
976+  DoReconnect = true ;
977+  log_d (" WiFi AutoReconnect Running"  );
982978 }
983-  else  if   (reason == WIFI_REASON_ASSOC_FAIL){
979+  else  if (reason == WIFI_REASON_ASSOC_FAIL)  {
984980 WiFiSTAClass::_setStatus (WL_CONNECT_FAILED);
985981 }
982+  if (DoReconnect) {
983+  WiFi.disconnect ();
984+  WiFi.begin ();
985+  }
986986 } else  if (event->event_id  == ARDUINO_EVENT_WIFI_STA_GOT_IP) {
987987#if  ARDUHAL_LOG_LEVEL >= ARDUHAL_LOG_LEVEL_DEBUG
988988 uint8_t  * ip = (uint8_t  *)&(event->event_info .got_ip .ip_info .ip .addr );
@@ -1066,6 +1066,36 @@ esp_err_t WiFiGenericClass::_eventCallback(arduino_event_t *event)
10661066 return  ESP_OK;
10671067}
10681068
1069+ bool  WiFiGenericClass::_isReconnectableReason (uint8_t  reason) {
1070+  switch (reason) {
1071+  // Timeouts (retry)
1072+  case  WIFI_REASON_AUTH_EXPIRE:
1073+  case  WIFI_REASON_4WAY_HANDSHAKE_TIMEOUT:
1074+  case  WIFI_REASON_GROUP_KEY_UPDATE_TIMEOUT:
1075+  case  WIFI_REASON_802_1X_AUTH_FAILED:
1076+  case  WIFI_REASON_HANDSHAKE_TIMEOUT:
1077+  // Transient error (reconnect)
1078+  case  WIFI_REASON_AUTH_LEAVE:
1079+  case  WIFI_REASON_ASSOC_EXPIRE:
1080+  case  WIFI_REASON_ASSOC_TOOMANY:
1081+  case  WIFI_REASON_NOT_AUTHED:
1082+  case  WIFI_REASON_NOT_ASSOCED:
1083+  case  WIFI_REASON_ASSOC_NOT_AUTHED:
1084+  case  WIFI_REASON_MIC_FAILURE:
1085+  case  WIFI_REASON_IE_IN_4WAY_DIFFERS:
1086+  case  WIFI_REASON_INVALID_PMKID:
1087+  case  WIFI_REASON_BEACON_TIMEOUT:
1088+  case  WIFI_REASON_NO_AP_FOUND:
1089+  case  WIFI_REASON_ASSOC_FAIL:
1090+  case  WIFI_REASON_CONNECTION_FAIL:
1091+  case  WIFI_REASON_AP_TSF_RESET:
1092+  case  WIFI_REASON_ROAMING:
1093+  return  true ;
1094+  default :
1095+  return  false ;
1096+  }
1097+ }
1098+ 
10691099/* *
10701100 * Return the current channel associated with the network 
10711101 * @return channel (1-13) 
0 commit comments