@@ -925,6 +925,29 @@ esp_err_t WiFiGenericClass::_eventCallback(arduino_event_t *event)
925
925
{
926
926
static bool first_connect = true ;
927
927
928
+ if (event->event_id < ARDUINO_EVENT_MAX) {
929
+ log_d (" Arduino Event: %d - %s" , event->event_id , arduino_event_names[event->event_id ]);
930
+ }
931
+ if (event->event_id == ARDUINO_EVENT_WIFI_SCAN_DONE) {
932
+ WiFiScanClass::_scanDone ();
933
+
934
+ } else if (event->event_id == ARDUINO_EVENT_WIFI_STA_START) {
935
+ WiFiSTAClass::_setStatus (WL_DISCONNECTED);
936
+ setStatusBits (STA_STARTED_BIT);
937
+ if (esp_wifi_set_ps (_sleepEnabled) != ESP_OK){
938
+ log_e (" esp_wifi_set_ps failed" );
939
+ }
940
+ } else if (event->event_id == ARDUINO_EVENT_WIFI_STA_STOP) {
941
+ WiFiSTAClass::_setStatus (WL_NO_SHIELD);
942
+ clearStatusBits (STA_STARTED_BIT | STA_CONNECTED_BIT | STA_HAS_IP_BIT | STA_HAS_IP6_BIT);
943
+ } else if (event->event_id == ARDUINO_EVENT_WIFI_STA_CONNECTED) {
944
+ WiFiSTAClass::_setStatus (WL_IDLE_STATUS);
945
+ setStatusBits (STA_CONNECTED_BIT);
946
+
947
+ // esp_netif_create_ip6_linklocal(esp_netifs[ESP_IF_WIFI_STA]);
948
+ } else if (event->event_id == ARDUINO_EVENT_WIFI_STA_DISCONNECTED) {
949
+ static bool first_connect = true ;
950
+
928
951
if (event->event_id < ARDUINO_EVENT_MAX) {
929
952
log_d (" Arduino Event: %d - %s" , event->event_id , arduino_event_names[event->event_id ]);
930
953
}
@@ -960,26 +983,26 @@ esp_err_t WiFiGenericClass::_eventCallback(arduino_event_t *event)
960
983
WiFiSTAClass::_setStatus (WL_DISCONNECTED);
961
984
}
962
985
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 ();
986
+
987
+ bool DoReconnect = false ;
988
+ if (reason == WIFI_REASON_ASSOC_LEAVE) { // Voluntarily disconnected. Don't reconnect!
989
+ }
990
+ else if (first_connect) { // Retry once for all failure reasons
969
991
first_connect = false ;
992
+ DoReconnect = true ;
993
+ log_d (" WiFi Reconnect Running" );
970
994
}
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
- }
995
+ else if (WiFi.getAutoReconnect () && _isReconnectableReason (reason)) {
996
+ DoReconnect = true ;
997
+ log_d (" WiFi AutoReconnect Running" );
979
998
}
980
- else if (reason == WIFI_REASON_ASSOC_FAIL){
999
+ else if (reason == WIFI_REASON_ASSOC_FAIL) {
981
1000
WiFiSTAClass::_setStatus (WL_CONNECT_FAILED);
982
1001
}
1002
+ if (DoReconnect) {
1003
+ WiFi.disconnect ();
1004
+ WiFi.begin ();
1005
+ }
983
1006
} else if (event->event_id == ARDUINO_EVENT_WIFI_STA_GOT_IP) {
984
1007
#if ARDUHAL_LOG_LEVEL >= ARDUHAL_LOG_LEVEL_DEBUG
985
1008
uint8_t * ip = (uint8_t *)&(event->event_info .got_ip .ip_info .ip .addr );
@@ -1063,6 +1086,12 @@ esp_err_t WiFiGenericClass::_eventCallback(arduino_event_t *event)
1063
1086
return ESP_OK;
1064
1087
}
1065
1088
1089
+ bool WiFiGenericClass::_isReconnectableReason (uint8_t reason) {
1090
+ return (reason == WIFI_REASON_AUTH_EXPIRE) ||
1091
+ (reason == WIFI_REASON_ASSOC_EXPIRE) ||
1092
+ (reason >= WIFI_REASON_BEACON_TIMEOUT);
1093
+ }
1094
+
1066
1095
/* *
1067
1096
* Return the current channel associated with the network
1068
1097
* @return channel (1-13)
0 commit comments