Why am I stuck in esp_event_loop_delete_default() function?

572 Views Asked by At

I am working on my ESP32-S2 and I want to implement a Restserver. As a template I use the https "simple" example provided by the esp-idf. My ESP32 should try to connect to a local router with a hardcoded SSID and password. The ESP tries to connect to a router every 1-2 seconds. After 4 attempts I want the ESP to stop connecting to the Router and start and Accesspoint instead. Now I'm facing some problems implementing this logic. After an unsuccessful connection try I'm calling my deinit_wifi function which looks like this:

void deinit_wifi(void)
{  

 stop_webserver(mainserver);


#ifdef CONFIG_EXAMPLE_CONNECT_WIFI
ESP_ERROR_CHECK(esp_event_handler_unregister(IP_EVENT, IP_EVENT_STA_GOT_IP, &connect_handler));
ESP_ERROR_CHECK(esp_event_handler_unregister(WIFI_EVENT, WIFI_EVENT_STA_DISCONNECTED, &disconnect_handler));
#endif // CONFIG_EXAMPLE_CONNECT_WIFI
#ifdef CONFIG_EXAMPLE_CONNECT_ETHERNET
ESP_ERROR_CHECK(esp_event_handler_unregister(IP_EVENT, IP_EVENT_ETH_GOT_IP, &connect_handler));
ESP_ERROR_CHECK(esp_event_handler_unregister(ETH_EVENT, ETHERNET_EVENT_DISCONNECTED, &disconnect_handler));
#endif // CONFIG_EXAMPLE_CONNECT_ETHERNET

    ESP_ERROR_CHECK(example_disconnect());

    ESP_ERROR_CHECK(esp_event_loop_delete_default());

    init_access_point();

}

I noticed, that my ESP gets stuck within the following line:

   ESP_ERROR_CHECK(esp_event_loop_delete_default());

I started searching for the problem and looked in the definition and added some printf's to locate the line in which the function get stuck.

esp_err_t esp_event_loop_delete_default(void)
{
    printf("\n I'm in function \n");
    if (!s_default_loop) {
        printf("\n Invalide state?! \n");
        return ESP_ERR_INVALID_STATE;
    }

    esp_err_t err;
    printf("\n I try to delete \n");
    err = esp_event_loop_delete(s_default_loop);

    printf("\n i just deleted \n");
    if (err != ESP_OK) {
        printf("\n s_default_loop is not null \n");
        return err;
    }

    s_default_loop = NULL;
    printf("\n s_default_loop is null now \n");
    return ESP_OK;
}

My last printed line was "I try to delete". So that means, my program stucks in this function:

    err = esp_event_loop_delete(s_default_loop);

Anyone got and idea?

for completion here is my init function:

void init_Wifi(void)
{
    

    ESP_ERROR_CHECK(nvs_flash_init());
    ESP_ERROR_CHECK(esp_netif_init());
    ESP_ERROR_CHECK(esp_event_loop_create_default());

    /* Register event handlers to start server when Wi-Fi or Ethernet is connected,
     * and stop server when disconnection happens.
     */

#ifdef CONFIG_EXAMPLE_CONNECT_WIFI
    ESP_ERROR_CHECK(esp_event_handler_register(IP_EVENT, IP_EVENT_STA_GOT_IP, &connect_handler, &mainserver));
    ESP_ERROR_CHECK(esp_event_handler_register(WIFI_EVENT, WIFI_EVENT_STA_DISCONNECTED, &disconnect_handler, &mainserver));
#endif // CONFIG_EXAMPLE_CONNECT_WIFI
#ifdef CONFIG_EXAMPLE_CONNECT_ETHERNET
    ESP_ERROR_CHECK(esp_event_handler_register(IP_EVENT, IP_EVENT_ETH_GOT_IP, &connect_handler, &mainserver));
    ESP_ERROR_CHECK(esp_event_handler_register(ETH_EVENT, ETHERNET_EVENT_DISCONNECTED, &disconnect_handler, &mainserver));
#endif // CONFIG_EXAMPLE_CONNECT_ETHERNET

    /* This helper function configures Wi-Fi or Ethernet, as selected in menuconfig.
     * Read "Establishing Wi-Fi or Ethernet Connection" section in
     * examples/protocols/README.md for more information about this function.
     */
    ESP_ERROR_CHECK(example_connect());
}
1

There are 1 best solutions below

12
Lajos Arpad On

So, inside esp_event_loop_delete_default you call esp_event_loop_delete, passing s_default_loop to it. Since your program is stuck, we can exclude infinite recursion in your code, because then you would see many prints. As a result, s_default_loop leads to something repeated infinitely many times in the C code that you use in your program.

Here https://github.com/espressif/esp-idf/blob/4a011f318377ab717b9dc459019c1055602c93ff/components/esp_event/include/esp_event.h#L70

the code says that event_loop must not be NULL.

Here: https://github.com/espressif/esp-idf/blob/4a011f318377ab717b9dc459019c1055602c93ff/components/esp_event/esp_event.c#L639

you can find the implementation of the function. The code says that there is some recursion involved, precisely here:

https://github.com/espressif/esp-idf/blob/4a011f318377ab717b9dc459019c1055602c93ff/components/esp_event/esp_event.c#L649

These are the information chunks you need to solve the problem. However, the actual solution requires debugging the C code and seeing what happens there.