diff --git a/ADIS_ESP32_Eclipse/main/wifi.c b/ADIS_ESP32_Eclipse/main/wifi.c index e3363fa..257407b 100644 --- a/ADIS_ESP32_Eclipse/main/wifi.c +++ b/ADIS_ESP32_Eclipse/main/wifi.c @@ -68,6 +68,7 @@ static EventGroupHandle_t s_wifi_event_group; #define WIFI_EVENT_HANDLER_CONNECTED_BIT (1<<0) #define WIFI_EVENT_HANDLER_FAIL_BIT (1<<1) #define WIFI_CONNECTED_BIT (1<<2) +#define WIFI_WAS_CONNECTED_ONCE (1<<3) // enables retry when the WiFi was lost static esp_netif_t *APP_WiFi_NetIf; static bool APP_WiFi_isOn = true; @@ -88,20 +89,35 @@ static void event_handler(void* arg, esp_event_base_t event_base, int32_t event_ McuLog_info("WIFI_EVENT_STA_START: start event"); esp_wifi_connect(); } else if (event_base == WIFI_EVENT && event_id == WIFI_EVENT_STA_DISCONNECTED) { - McuLog_info("WIFI_EVENT_STA_DISCONNECTED: disconnected, retry %d", s_retry_num); + McuLog_info("WIFI_EVENT_STA_DISCONNECTED: disconnected"); + // set bit for WiFiTask => state is disconnected + xEventGroupClearBits(s_wifi_event_group, WIFI_CONNECTED_BIT); + // any retrys left? if (s_retry_num < CONFIG_ESP_MAXIMUM_RETRY) { esp_wifi_connect(); - s_retry_num++; - McuLog_info("retry to connect to the AP"); - } else { + McuLog_info("retrying to connect to the AP, retry %d", ++s_retry_num); + } // if wifi already connected => endless reconnect, no counting + else if(xEventGroupGetBits(s_wifi_event_group)&WIFI_WAS_CONNECTED_ONCE){ + esp_wifi_connect(); + McuLog_info("retrying endlessly to connect to the AP"); + } + else { + // set bits for init xEventGroupSetBits(s_wifi_event_group, WIFI_EVENT_HANDLER_FAIL_BIT); + xEventGroupClearBits(s_wifi_event_group, WIFI_EVENT_HANDLER_CONNECTED_BIT); } McuLog_info("connect to the AP fail"); } else if (event_base == IP_EVENT && event_id == IP_EVENT_STA_GOT_IP) { ip_event_got_ip_t* event = (ip_event_got_ip_t*) event_data; McuLog_info("IP_EVENT_STA_GOT_IP: got ip:" IPSTR, IP2STR(&event->ip_info.ip)); s_retry_num = 0; + // set bits for init xEventGroupSetBits(s_wifi_event_group, WIFI_EVENT_HANDLER_CONNECTED_BIT); + xEventGroupClearBits(s_wifi_event_group, WIFI_EVENT_HANDLER_FAIL_BIT); + // set bit for WiFiTask => state is connected + xEventGroupSetBits(s_wifi_event_group, WIFI_CONNECTED_BIT); + // set wifi was connected once => enables endless reconnect from here + xEventGroupSetBits(s_wifi_event_group, WIFI_WAS_CONNECTED_ONCE); } } @@ -219,7 +235,6 @@ static void initialise_wifi(void) { } else if (mode == WIFI_PASSWORD_METHOD_PSK) { McuLog_info("connected to AP SSID: %s", CONFIG_WIFI_PSK_SSID); } - xEventGroupSetBits(s_wifi_event_group, WIFI_CONNECTED_BIT); } else if (bits & WIFI_EVENT_HANDLER_FAIL_BIT) { if (mode == WIFI_PASSWORD_METHOD_WPA2) { McuLog_info("Failed to connect to SSID: %s", CONFIG_WIFI_EAP_SSID);