Error with Wi-Fi Connection Using esp_event_loop_create_default() in ESP32

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

Error with Wi-Fi Connection Using esp_event_loop_create_default() in ESP32

Postby Vaibhav02 » Fri Sep 06, 2024 6:06 am

I’m using the simple HTTP server example in the ESP-IDF. I’ve added code to connect to Wi-Fi using credentials provided through a POST request. However, I encounter the following error when trying to connect:

Code: Select all

ESP_ERROR_CHECK failed: esp_err_t 0x103 (ESP_ERR_INVALID_STATE) at 0x400d7d3e
0x400d7d3e: wifi_init_sta at C:/Users/Admin/Desktop/simple/main/wifi.c:51 (discriminator 1)

file: "./main/wifi.c" line 51
func: wifi_init_sta
expression: esp_event_loop_create_default()

abort() was called at PC 0x400893b7 on core 0


I understand that the esp_event_loop_create_default() function can only be called once in the lifecycle, but how can I connect to a new Wi-Fi network without it? The code keeps trying to connect and backtracks. What would be the correct approach to resolve this issue?

MicroController
Posts: 1694
Joined: Mon Oct 17, 2022 7:38 pm
Location: Europe, Germany

Re: Error with Wi-Fi Connection Using esp_event_loop_create_default() in ESP32

Postby MicroController » Fri Sep 06, 2024 7:50 am

You call esp_event_loop_create_default() once, when the application starts. After that, starting, stopping, disconnecting, connecting,... WiFi can be done at any time without ever calling esp_event_loop_create_default() again.

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

Re: Error with Wi-Fi Connection Using esp_event_loop_create_default() in ESP32

Postby Vaibhav02 » Fri Sep 06, 2024 8:27 am

But I am facing below issue when the function is commented in the wifi_init_sta() function.

Code: Select all

E (458594) esp_netif_lwip: esp_netif_new_api: Failed to configure netif with config=0x3ffc8640 (config or if_key is NULL or duplicate key)

assert failed: esp_netif_create_default_wifi_sta wifi_default.c:388 (netif)


Backtrace: 0x40081a56:0x3ffc84e0 0x400893c1:0x3ffc8500 0x400911b1:0x3ffc8520 0x400e11f9:0x3ffc8640 0x400d7d5c:0x3ffc8670 0x400d7861:0x3ffc8840 0x400e35d5:0x3ffc88e0 0x400e2331:0x3ffc8920 0x400e23d0:0x3ffc89b0 0x400e2ad8:0x3ffc89d0 0x400e12c4:0x3ffc89f0 0x4015753b:0x3ffc8a10 0x400e19c2:0x3ffc8a30 0x400e1a2a:0x3ffc8a80 0x40089ecd:0x3ffc8aa0
My wifi_init_sta() function is like below:

Code: Select all

void wifi_init_sta(char *ssid, char *ssid_password) 
{   
    printf("starting wifi_init_sta\n");

    // Disconnect from the current Wi-Fi
    ESP_ERROR_CHECK(esp_wifi_disconnect());  

    // Create an event group to track the Wi-Fi events
    s_wifi_event_group = xEventGroupCreate();

    // Initialize the network interface
    ESP_ERROR_CHECK(esp_netif_init());

    //....................commenting below function ....................
    //ESP_ERROR_CHECK(esp_event_loop_create_default());


    // Create the default station interface
    esp_netif_create_default_wifi_sta();

    // Initialize the Wi-Fi driver with default configuration
    wifi_init_config_t cfg = WIFI_INIT_CONFIG_DEFAULT();
    ESP_ERROR_CHECK(esp_wifi_init(&cfg));

    // Register event handlers for Wi-Fi and IP events
    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,
                                                        &event_handler,
                                                        NULL,
                                                        &instance_any_id));
    ESP_ERROR_CHECK(esp_event_handler_instance_register(IP_EVENT,
                                                        IP_EVENT_STA_GOT_IP,
                                                        &event_handler,
                                                        NULL,
                                                        &instance_got_ip));

    // Set the Wi-Fi configuration using the provided SSID and password
    wifi_config_t wifi_config = {
        .sta = {
            .ssid = {0},
            .password = {0},
            .threshold.authmode = WIFI_AUTH_WPA2_PSK, // Use WPA2 by default
        },
    };
    strncpy((char *)wifi_config.sta.ssid, ssid, sizeof(wifi_config.sta.ssid));
    strncpy((char *)wifi_config.sta.password, ssid_password, sizeof(wifi_config.sta.password));

    // Set the Wi-Fi mode to station and configure the Wi-Fi settings
    ESP_ERROR_CHECK(esp_wifi_set_mode(WIFI_MODE_STA));
    ESP_ERROR_CHECK(esp_wifi_set_config(ESP_IF_WIFI_STA, &wifi_config));

    // Start the Wi-Fi driver
    ESP_ERROR_CHECK(esp_wifi_start());

    // Connect to the specified Wi-Fi network
    ESP_ERROR_CHECK(esp_wifi_connect());

    // Wait for either the connection to succeed or fail
    EventBits_t bits = xEventGroupWaitBits(s_wifi_event_group,
                                           WIFI_CONNECTED_BIT | WIFI_FAIL_BIT,
                                           pdFALSE,
                                           pdFALSE,
                                           portMAX_DELAY);

    // Check which event happened
    if (bits & WIFI_CONNECTED_BIT)
    {
        ESP_LOGI(TAG, "Connected to Wi-Fi network SSID: %s, Password: %s", ssid, ssid_password);
    }
    else if (bits & WIFI_FAIL_BIT)
    {
        ESP_LOGI(TAG, "Failed to connect to SSID: %s, Password: %s", ssid, ssid_password);
    }
    else
    {
        ESP_LOGE(TAG, "Unexpected Wi-Fi event");
    }

    // Unregister event handlers when done
    ESP_ERROR_CHECK(esp_event_handler_instance_unregister(IP_EVENT, IP_EVENT_STA_GOT_IP, instance_got_ip));
    ESP_ERROR_CHECK(esp_event_handler_instance_unregister(WIFI_EVENT, ESP_EVENT_ANY_ID, instance_any_id));
}

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

Re: Error with Wi-Fi Connection Using esp_event_loop_create_default() in ESP32

Postby Vaibhav02 » Fri Sep 06, 2024 8:41 am

But I am facing below issue when the function is commented in the wifi_init_sta() function.

Code: Select all

E (458594) esp_netif_lwip: esp_netif_new_api: Failed to configure netif with config=0x3ffc8640 (config or if_key is NULL or duplicate key)

assert failed: esp_netif_create_default_wifi_sta wifi_default.c:388 (netif)


Backtrace: 0x40081a56:0x3ffc84e0 0x400893c1:0x3ffc8500 0x400911b1:0x3ffc8520 0x400e11f9:0x3ffc8640 0x400d7d5c:0x3ffc8670 0x400d7861:0x3ffc8840 0x400e35d5:0x3ffc88e0 0x400e2331:0x3ffc8920 0x400e23d0:0x3ffc89b0 0x400e2ad8:0x3ffc89d0 0x400e12c4:0x3ffc89f0 0x4015753b:0x3ffc8a10 0x400e19c2:0x3ffc8a30 0x400e1a2a:0x3ffc8a80 0x40089ecd:0x3ffc8aa0
My wifi_init_sta() function is like below:

Code: Select all

void wifi_init_sta(char *ssid, char *ssid_password) 
{   
    printf("starting wifi_init_sta\n");

    // Disconnect from the current Wi-Fi
    ESP_ERROR_CHECK(esp_wifi_disconnect());  

    // Create an event group to track the Wi-Fi events
    s_wifi_event_group = xEventGroupCreate();

    // Initialize the network interface
    ESP_ERROR_CHECK(esp_netif_init());

    //....................commenting below function ....................
    //ESP_ERROR_CHECK(esp_event_loop_create_default());


    // Create the default station interface
    esp_netif_create_default_wifi_sta();

    // Initialize the Wi-Fi driver with default configuration
    wifi_init_config_t cfg = WIFI_INIT_CONFIG_DEFAULT();
    ESP_ERROR_CHECK(esp_wifi_init(&cfg));

    // Register event handlers for Wi-Fi and IP events
    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,
                                                        &event_handler,
                                                        NULL,
                                                        &instance_any_id));
    ESP_ERROR_CHECK(esp_event_handler_instance_register(IP_EVENT,
                                                        IP_EVENT_STA_GOT_IP,
                                                        &event_handler,
                                                        NULL,
                                                        &instance_got_ip));

    // Set the Wi-Fi configuration using the provided SSID and password
    wifi_config_t wifi_config = {
        .sta = {
            .ssid = {0},
            .password = {0},
            .threshold.authmode = WIFI_AUTH_WPA2_PSK, // Use WPA2 by default
        },
    };
    strncpy((char *)wifi_config.sta.ssid, ssid, sizeof(wifi_config.sta.ssid));
    strncpy((char *)wifi_config.sta.password, ssid_password, sizeof(wifi_config.sta.password));

    // Set the Wi-Fi mode to station and configure the Wi-Fi settings
    ESP_ERROR_CHECK(esp_wifi_set_mode(WIFI_MODE_STA));
    ESP_ERROR_CHECK(esp_wifi_set_config(ESP_IF_WIFI_STA, &wifi_config));

    // Start the Wi-Fi driver
    ESP_ERROR_CHECK(esp_wifi_start());

    // Connect to the specified Wi-Fi network
    ESP_ERROR_CHECK(esp_wifi_connect());

    // Wait for either the connection to succeed or fail
    EventBits_t bits = xEventGroupWaitBits(s_wifi_event_group,
                                           WIFI_CONNECTED_BIT | WIFI_FAIL_BIT,
                                           pdFALSE,
                                           pdFALSE,
                                           portMAX_DELAY);

    // Check which event happened
    if (bits & WIFI_CONNECTED_BIT)
    {
        ESP_LOGI(TAG, "Connected to Wi-Fi network SSID: %s, Password: %s", ssid, ssid_password);
    }
    else if (bits & WIFI_FAIL_BIT)
    {
        ESP_LOGI(TAG, "Failed to connect to SSID: %s, Password: %s", ssid, ssid_password);
    }
    else
    {
        ESP_LOGE(TAG, "Unexpected Wi-Fi event");
    }

    // Unregister event handlers when done
    ESP_ERROR_CHECK(esp_event_handler_instance_unregister(IP_EVENT, IP_EVENT_STA_GOT_IP, instance_got_ip));
    ESP_ERROR_CHECK(esp_event_handler_instance_unregister(WIFI_EVENT, ESP_EVENT_ANY_ID, instance_any_id));
}

MicroController
Posts: 1694
Joined: Mon Oct 17, 2022 7:38 pm
Location: Europe, Germany

Re: Error with Wi-Fi Connection Using esp_event_loop_create_default() in ESP32

Postby MicroController » Fri Sep 06, 2024 6:30 pm

If you're calling this wifi_init_sta() function more than once between two reboots,
a) you should not call esp_event_loop_create_default() inside that function and
b) you have a memory leak the size of one event group per call.

To be clear: It's never too early to call esp_event_loop_create_default(). It doesn't have to be done in-between any of the WiFi setup functions.

Who is online

Users browsing this forum: No registered users and 95 guests