Refactor Wi-Fi connection logic to improve stability, simplify event
handling, and enhance logging and watchdog reliability.
This commit is contained in:
@@ -8,7 +8,7 @@
|
|||||||
#include "main.hpp"
|
#include "main.hpp"
|
||||||
#include "wifi-credentials.h"
|
#include "wifi-credentials.h"
|
||||||
|
|
||||||
#define WIFI_TIMEOUT_TICKS pdMS_TO_TICKS(30000)
|
#define WIFI_TIMEOUT_TICKS pdMS_TO_TICKS(8000)
|
||||||
|
|
||||||
static const char* TAG = "WIFI";
|
static const char* TAG = "WIFI";
|
||||||
const int WIFI_CONNECTED_BIT = BIT0;
|
const int WIFI_CONNECTED_BIT = BIT0;
|
||||||
@@ -17,20 +17,33 @@ EventGroupHandle_t wifi_event_group;
|
|||||||
|
|
||||||
static void wifi_event_handler(void* arg, esp_event_base_t event_base, int32_t event_id, void* event_data) {
|
static void wifi_event_handler(void* arg, esp_event_base_t event_base, int32_t event_id, void* event_data) {
|
||||||
if (event_base == WIFI_EVENT) {
|
if (event_base == WIFI_EVENT) {
|
||||||
switch(event_id) {
|
switch (event_id) {
|
||||||
case WIFI_EVENT_STA_START:
|
case WIFI_EVENT_STA_START:
|
||||||
|
ESP_LOGI(TAG, "Wi-Fi process started, connecting...");
|
||||||
|
|
||||||
esp_wifi_connect();
|
esp_wifi_connect();
|
||||||
break;
|
break;
|
||||||
case WIFI_EVENT_STA_DISCONNECTED:
|
case WIFI_EVENT_STA_DISCONNECTED: {
|
||||||
esp_wifi_connect();
|
wifi_event_sta_disconnected_t* disconn = (wifi_event_sta_disconnected_t*) event_data;
|
||||||
|
ESP_LOGW(TAG, "Disconnected, reason: %d", disconn->reason);
|
||||||
|
|
||||||
xEventGroupClearBits(wifi_event_group, WIFI_CONNECTED_BIT);
|
xEventGroupClearBits(wifi_event_group, WIFI_CONNECTED_BIT);
|
||||||
|
esp_wifi_connect();
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
default:
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
else if (event_base == IP_EVENT && event_id == IP_EVENT_STA_GOT_IP) xEventGroupSetBits(wifi_event_group, WIFI_CONNECTED_BIT);
|
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;
|
||||||
|
ESP_LOGI(TAG, "Got IP: " IPSTR, IP2STR(&event->ip_info.ip));
|
||||||
|
xEventGroupSetBits(wifi_event_group, WIFI_CONNECTED_BIT);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void wifi_init_sta(void) {
|
|
||||||
|
static void wifi_init_sta(void) {
|
||||||
esp_err_t nvs_err = nvs_flash_init();
|
esp_err_t nvs_err = nvs_flash_init();
|
||||||
|
|
||||||
if (nvs_err == ESP_ERR_NVS_NO_FREE_PAGES || nvs_err == ESP_ERR_NVS_NEW_VERSION_FOUND) {
|
if (nvs_err == ESP_ERR_NVS_NO_FREE_PAGES || nvs_err == ESP_ERR_NVS_NEW_VERSION_FOUND) {
|
||||||
@@ -69,24 +82,11 @@ void wifiTask(void *pvParameters) {
|
|||||||
wifi_init_sta();
|
wifi_init_sta();
|
||||||
ESP_ERROR_CHECK(esp_task_wdt_add(NULL));
|
ESP_ERROR_CHECK(esp_task_wdt_add(NULL));
|
||||||
|
|
||||||
TickType_t startTick = xTaskGetTickCount();
|
for(;;) {
|
||||||
do {
|
EventBits_t bits = xEventGroupWaitBits(wifi_event_group, WIFI_CONNECTED_BIT, pdFALSE, pdTRUE, WIFI_TIMEOUT_TICKS);
|
||||||
EventBits_t bits = xEventGroupWaitBits(wifi_event_group, WIFI_CONNECTED_BIT, pdFALSE, pdTRUE, pdMS_TO_TICKS(1000));
|
|
||||||
ESP_LOGI(TAG, "Connecting to Wi-Fi...");
|
|
||||||
|
|
||||||
if (bits & WIFI_CONNECTED_BIT) break;
|
ESP_ERROR_CHECK(esp_task_wdt_reset());
|
||||||
|
ESP_LOGI(TAG, "Wi-Fi task reset");
|
||||||
ESP_LOGI(TAG, "Connection attempt was unsuccessful");
|
vTaskDelay(pdMS_TO_TICKS(4000));
|
||||||
vTaskDelay(pdMS_TO_TICKS(250));
|
|
||||||
} while ((xTaskGetTickCount() - startTick) < WIFI_TIMEOUT_TICKS);
|
|
||||||
|
|
||||||
if (xEventGroupGetBits(wifi_event_group) & WIFI_CONNECTED_BIT) ESP_LOGI(TAG, "Connection attempt was successful");
|
|
||||||
else {
|
|
||||||
ESP_LOGE(TAG, "Critical error: Wi-Fi network refused connection or not found");
|
|
||||||
criticalErrorFlag = true;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
esp_task_wdt_reset();
|
|
||||||
ESP_LOGI(TAG, "Wi-Fi reset");
|
|
||||||
vTaskDelay(pdMS_TO_TICKS(4000));
|
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user