--- a/main/task_wifi.c Fri Apr 07 14:31:17 2023 +0200 +++ b/main/task_wifi.c Tue Apr 11 11:11:13 2023 +0200 @@ -14,15 +14,11 @@ SemaphoreHandle_t xSemaphoreWiFi = NULL; ///< Semaphore WiFi task. EventGroupHandle_t xEventGroupWifi; ///< Events WiFi task. -wifi_config_t wifi_Config = { ///< Current STA configuration. - .sta = { - .ssid = ESP_WIFI_SSID, - .password = ESP_WIFI_PASS, - .scan_method = WIFI_FAST_SCAN, - }, -}; +esp_event_handler_instance_t instance_any_id; ///< WiFi event handler. +esp_event_handler_instance_t instance_got_ip; ///< IP event handler. + WIFI_State *wifi_state = NULL; ///< Public state for other tasks. -esp_netif_t *sta_netif = NULL; ///< Station interface +esp_netif_t *sta_netif = NULL; ///< Station interface. const int TASK_WIFI_REQUEST_STA_DISCONNECT = BIT1; ///< When set, means a client requested to disconnect from currently connected AP. @@ -34,6 +30,10 @@ const int TASK_WIFI_STA_CONNECTED = BIT7; ///< Indicate that we are connected to AP as station, flip of BIT6. +static void init_wifi(void); +void wifi_connect(void); + + /****************************************************************************/ @@ -72,7 +72,6 @@ break; case WIFI_EVENT_STA_CONNECTED: { -// system_event_sta_connected_t* event = (wifi_event_sta_connected_t*) event_data; wifi_ap_record_t ap_info; esp_wifi_sta_get_ap_info(&ap_info); ESP_LOGI(TAG, "Event STA connected rssi=%d", ap_info.rssi); @@ -90,11 +89,26 @@ case WIFI_EVENT_STA_DISCONNECTED: { wifi_event_sta_disconnected_t* disconnected = (wifi_event_sta_disconnected_t*) event_data; + ESP_LOGI(TAG, "Event STA disconnected, reason: %d", disconnected->reason); - ESP_LOGI(TAG, "Event STA disconnected, reason: %d", disconnected->reason); + /* + * If it's not a normal request to disconnect, make sure the mqtt + * connection will be removed. + */ if (disconnected->reason != 8) request_mqtt(false); - if (xSemaphoreTake(xSemaphoreWiFi, 35) == pdTRUE) { + + /* + * Error conditions. + */ + if (disconnected->reason == 2) { + ESP_LOGW(TAG, "Auth Expire: try to recover"); + wifi_connect(); + } else if (disconnected->reason == 39) { + ESP_LOGW(TAG, "Timeout: try to recover"); + ESP_ERROR_CHECK(esp_wifi_connect()); + } + if (xSemaphoreTake(xSemaphoreWiFi, 25) == pdTRUE) { wifi_state->STA_connected = false; wifi_state->STA_online = false; xSemaphoreGive(xSemaphoreWiFi); @@ -154,52 +168,96 @@ } +static void init_wifi(void) +{ + ESP_LOGI(TAG, "initialise_wifi() start"); + ESP_ERROR_CHECK(esp_event_loop_create_default()); + xSemaphoreWiFi = xSemaphoreCreateMutex(); + + /* initialize the tcp stack */ + ESP_ERROR_CHECK(esp_netif_init()); + sta_netif = esp_netif_create_default_wifi_sta(); + assert(sta_netif); + + wifi_init_config_t wifi_init_config = WIFI_INIT_CONFIG_DEFAULT(); + ESP_ERROR_CHECK(esp_wifi_init(&wifi_init_config)); + + ESP_ERROR_CHECK(esp_event_handler_instance_register(WIFI_EVENT, ESP_EVENT_ANY_ID, &wifi_event_handler, NULL, &instance_any_id) ); + ESP_ERROR_CHECK(esp_event_handler_instance_register(IP_EVENT, ESP_EVENT_ANY_ID, &got_ip_event_handler, NULL, &instance_got_ip) ); + + ESP_ERROR_CHECK(esp_wifi_set_mode(WIFI_MODE_STA)); + ESP_ERROR_CHECK( esp_wifi_start() ); + + xEventGroupClearBits(xEventGroupWifi, TASK_WIFI_STA_CONNECTED); + xEventGroupSetBits(xEventGroupWifi, TASK_WIFI_STA_DISCONNECTED); + + ESP_LOGI(TAG, "initialise_wifi() done"); +} + + +void wifi_connect(void) +{ + ESP_LOGI(TAG, "wifi_connect() start"); + wifi_config_t wifi_Config = { ///< Current STA configuration. + .sta = { + .ssid = ESP_WIFI_SSID, + .password = ESP_WIFI_PASS, + }, + }; + // .threshold.authmode = WIFI_AUTH_WPA2_PSK, + // .pmf_cfg = { + // .capable = true, + // .required = false + // }, + + ESP_ERROR_CHECK(esp_wifi_disconnect()); + ESP_ERROR_CHECK(esp_wifi_set_config(ESP_IF_WIFI_STA, &wifi_Config) ); + + xEventGroupClearBits(xEventGroupWifi, TASK_WIFI_STA_FAILED); + esp_err_t wifierror = esp_wifi_connect(); + + if (wifierror != ESP_OK) { + ESP_LOGE(TAG, "esp_wifi_connect() rc=%04x %s", (int)wifierror, esp_err_to_name(wifierror)); + xEventGroupSetBits(xEventGroupWifi, TASK_WIFI_STA_FAILED); + } else { + ESP_LOGI(TAG, "Connected Ok"); + } + ESP_LOGI(TAG, "wifi_connect() done"); +} + void task_wifi( void * pvParameters ) { - ESP_LOGI(TAG, "Start WiFi"); + uint64_t starttime = 0; - /* event handler and event group for the wifi driver */ - xEventGroupWifi = xEventGroupCreate(); - /* initialize the tcp stack */ - ESP_ERROR_CHECK(esp_netif_init()); - ESP_ERROR_CHECK(esp_event_loop_create_default()); - sta_netif = esp_netif_create_default_wifi_sta(); - assert(sta_netif); + ESP_LOGI(TAG, "Start WiFi"); + esp_log_level_set("wifi", ESP_LOG_WARNING); /* * memory allocation of objects used by the task */ - xSemaphoreWiFi = xSemaphoreCreateMutex(); wifi_state = malloc(sizeof(WIFI_State)); memset(wifi_state, 0x00, sizeof(WIFI_State)); sprintf(wifi_state->STA_ssid, "%s", ESP_WIFI_SSID); /* + * event group for the wifi driver + */ + xEventGroupWifi = xEventGroupCreate(); + + /* * init wifi as station */ - wifi_init_config_t wifi_init_config = WIFI_INIT_CONFIG_DEFAULT(); - ESP_ERROR_CHECK(esp_wifi_init(&wifi_init_config)); - - esp_event_handler_instance_t instance_any_id; - esp_event_handler_instance_t instance_got_ip; - ESP_ERROR_CHECK(esp_event_handler_instance_register(WIFI_EVENT, ESP_EVENT_ANY_ID, &wifi_event_handler, NULL, &instance_any_id) ); - ESP_ERROR_CHECK(esp_event_handler_instance_register(IP_EVENT, ESP_EVENT_ANY_ID, &got_ip_event_handler, NULL, &instance_got_ip) ); - - ESP_ERROR_CHECK(esp_wifi_set_mode(WIFI_MODE_STA)); - ESP_ERROR_CHECK(esp_wifi_set_config(WIFI_IF_STA, &wifi_Config) ); - ESP_ERROR_CHECK(esp_wifi_start()); - - //xEventGroupSetBits(xEventGroupWifi, TASK_WIFI_REQUEST_STA_CONNECT); - xEventGroupClearBits(xEventGroupWifi, TASK_WIFI_STA_CONNECTED); - xEventGroupSetBits(xEventGroupWifi, TASK_WIFI_STA_DISCONNECTED); + init_wifi(); EventBits_t uxBits; ESP_LOGI(TAG, "Startup completed, enter task loop"); for(;;) { - /* actions that can trigger: request a connection or a disconnection */ + /* + * Actions that can trigger: request a connection or a disconnection + */ uxBits = xEventGroupWaitBits(xEventGroupWifi, TASK_WIFI_REQUEST_STA_CONNECT | TASK_WIFI_REQUEST_STA_DISCONNECT, pdFALSE, pdFALSE, portMAX_DELAY ); @@ -211,22 +269,17 @@ ESP_ERROR_CHECK(esp_wifi_disconnect()); xEventGroupWaitBits(xEventGroupWifi, TASK_WIFI_STA_DISCONNECTED, pdFALSE, pdTRUE, portMAX_DELAY ); - /* finally: release the request bit */ + /* + * Finally: release the request bit + */ xEventGroupClearBits(xEventGroupWifi, TASK_WIFI_REQUEST_STA_DISCONNECT); + ESP_LOGI(TAG, "Connection time %llu", (esp_timer_get_time() / 1000) - starttime); } else if (uxBits & TASK_WIFI_REQUEST_STA_CONNECT) { - ESP_LOGI(TAG, "Request STA connect `%s' `%s'", wifi_Config.sta.ssid, wifi_Config.sta.password); - xEventGroupClearBits(xEventGroupWifi, TASK_WIFI_STA_FAILED); - ESP_ERROR_CHECK(esp_wifi_set_config(WIFI_IF_STA, &wifi_Config)); - - esp_err_t wifierror = esp_wifi_connect(); - if (wifierror != ESP_OK) { - ESP_LOGE(TAG, "esp_wifi_connect() rc=%04x", (int)wifierror); - xEventGroupSetBits(xEventGroupWifi, TASK_WIFI_STA_FAILED); - } else { - ESP_LOGI(TAG, "Connected Ok"); - } + ESP_LOGI(TAG, "Request STA connect `%s' `%s'", ESP_WIFI_SSID, ESP_WIFI_PASS); + starttime = esp_timer_get_time() / 1000; + wifi_connect(); /* * 3 scenarios here: connection is successful and TASK_WIFI_STA_CONNECTED will be posted @@ -240,26 +293,21 @@ /* * only save the config if the connection was successful! */ - if (uxBits & TASK_WIFI_STA_CONNECTED) { - /* save wifi config */ - //SaveStaConfig(); - } else { + if (uxBits & TASK_WIFI_STA_FAILED) { ESP_LOGI(TAG, "No AP found"); vTaskDelay(3000 / portTICK_PERIOD_MS); ESP_LOGW(TAG, "Connection failed"); /* failed attempt to connect regardles of the reason */ - - /* otherwise: reset the config */ - //memset(task_wifi_ConfigSTA, 0x00, sizeof(wifi_config_t)); } } - /* finally: release the request bit */ + /* + * Finally: release the request bit + */ xEventGroupClearBits(xEventGroupWifi, TASK_WIFI_REQUEST_STA_CONNECT); } } /* for(;;) */ - vTaskDelay(10 / portTICK_PERIOD_MS); }