ESP32 WiFi Event Handler Blocks Other High-Priority Tasks on Disconnect

Vaibhav02
Posts: 32
Joined: Wed Sep 04, 2024 8:09 am

ESP32 WiFi Event Handler Blocks Other High-Priority Tasks on Disconnect

Postby Vaibhav02 » Sat Apr 12, 2025 12:02 pm

Hello ESP-IDF community,

I’m working on an ESP32 project using ESP-IDF, and I’m facing an issue where my code gets stuck in the WiFi event handler when WiFi disconnects. This prevents other tasks, including one with priority 7, from executing. I expected the higher-priority task to run, but it seems the WiFi event handler is blocking the FreeRTOS scheduler. I’d appreciate any insights or suggestions to resolve this.

nopnop2002
Posts: 362
Joined: Thu Oct 03, 2019 10:52 pm
Contact:

Re: ESP32 WiFi Event Handler Blocks Other High-Priority Tasks on Disconnect

Postby nopnop2002 » Sat Apr 12, 2025 11:19 pm

Unless you reveal the WiFi handler code you're using, no one can know what the problem is.

Vaibhav02
Posts: 32
Joined: Wed Sep 04, 2024 8:09 am

Re: ESP32 WiFi Event Handler Blocks Other High-Priority Tasks on Disconnect

Postby Vaibhav02 » Mon Apr 14, 2025 9:12 am

My wifi handler code:

Code: Select all

void wifi_event_handler(void *arg, esp_event_base_t event_base,
                               int32_t event_id, void *event_data)
{
    if (event_base == WIFI_EVENT && event_id == WIFI_EVENT_AP_STACONNECTED)
    {
        wifi_event_ap_staconnected_t *event = (wifi_event_ap_staconnected_t *)event_data;
        ESP_LOGI(TAG, "Station " MACSTR " joined, AID=%d",
                 MAC2STR(event->mac), event->aid);
    }
    
    else if (event_base == WIFI_EVENT && event_id == WIFI_EVENT_AP_STADISCONNECTED)
    {
        wifi_event_ap_stadisconnected_t *event = (wifi_event_ap_stadisconnected_t *)event_data;
        ESP_LOGI(TAG, "Station " MACSTR " left, AID=%d, reason:%d",
                 MAC2STR(event->mac), event->aid, event->reason);
    }
    else if (event_base == WIFI_EVENT && event_id == WIFI_EVENT_STA_DISCONNECTED)
    {
        if (s_retry_num < EXAMPLE_ESP_MAXIMUM_RETRY)
        {
            esp_wifi_connect();
            s_retry_num++;
            ESP_LOGI("sta....", "retry to connect to the AP");
            vTaskDelay(pdMS_TO_TICKS(4000)); // Delay between retries (10 seconds)
        }
        else
        {
            esp_wifi_connect();    // Attempt to reconnect
            vTaskDelay(pdMS_TO_TICKS(1000)); // Wait 1 second before reconnecting
            s_retry_num = 0; // Reset the retry count if needed
        }
        wifi_connected = false;  // Wi-Fi not connected
    }
    else if (event_base == WIFI_EVENT && event_id == WIFI_EVENT_STA_START)
    {
        esp_wifi_connect();
        ESP_LOGI("sta connection ...", "Station started");
        start_webserver(); // Ensure the server is started in STA mode as well
    }
    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 _ sta ...", "Got IP:" IPSTR, IP2STR(&event->ip_info.ip));
        s_retry_num = 0;
        xEventGroupSetBits(s_wifi_event_group, WIFI_CONNECTED_BIT);
        wifi_connected = true;  // Wi-Fi connected successfully
    }
}

Who is online

Users browsing this forum: Baidu [Spider], ChatGPT-User, Google [Bot] and 2 guests