1

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());
}
Lajos Arpad
  • 64,414
  • 37
  • 100
  • 175
Dansen
  • 55
  • 1
  • 8

1 Answers1

1

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.

Lajos Arpad
  • 64,414
  • 37
  • 100
  • 175
  • Hello! thanks for your answer! I'm still kind of new to that stuff and It is a little hard for me to comprehend those github function definitions. Normally i'm debugging with print statements which i can't really use in this case because I can only access the esp_event.h in visual studio code and not the .c file. You may got a tip for me to debug my program? – Dansen Nov 15 '21 at 10:54
  • @Dansen this is open-source. You can actually download the source-code, add your print statements and build. Also, it would be great if we knew what the value of `s_default_loop` is when you call `esp_event_loop_delete`. – Lajos Arpad Nov 15 '21 at 11:01
  • I tried printf("\n s_default_loop value %p \n", s_default_loop); I coudln't use %c or %s since it is of type "esp_event_loop_handle_t". Not sure if this Value is useable but I got the value 0x3ffd275c printed out in my console – Dansen Nov 15 '21 at 11:16
  • @Dansen is this page helpful: https://docs.espressif.com/projects/esp-idf/en/latest/esp32/api-reference/system/esp_event.html ? – Lajos Arpad Nov 15 '21 at 11:23
  • well that page is definetly helpful, but i don't think that i can apply the information i read on to my problem. I couldn't find any given restirction or requirements for calling esp_event_loop_delete_default I got the same order of "esp_event_loop_create_default", "esp_event_handler_register", "esp_event_handler_unregister" and "esp_event_loop_delete_default". – Dansen Nov 15 '21 at 12:40
  • @Dansen I do not know more about this specific type than you. If you could find a reference where it is defined, either at GitHub or another place, then I would probably be able to help you further. – Lajos Arpad Nov 15 '21 at 12:48
  • Well there is the esp_event_loop_handle_t variable definied: https://github.com/espressif/esp-idf/blob/master/components/esp_event/include/esp_event_base.h#L28 – Dansen Nov 15 '21 at 12:58
  • 1
    I just debugged the esp_event_loop_delete() function and noticed, that on line : https://github.com/espressif/esp-idf/blob/4a011f318377ab717b9dc459019c1055602c93ff/components/esp_event/esp_event.c#L659 my program stucks. I also noticed that it got something to do with this function: https://github.com/espressif/esp-idf/blob/master/examples/common_components/protocol_examples_common/connect.c#L200 since the function esp_event_loop_delete() does not get stuck before calling this function. Just after calling example_connect(), this behaviour occurs – Dansen Nov 15 '21 at 15:50
  • @Dansen excellent work. Are you sure that your program is not stuck on line 600 (https://github.com/espressif/esp-idf/blob/4a011f318377ab717b9dc459019c1055602c93ff/components/esp_event/esp_event.c#L660)? It seems to be unlikely that it would stuck on a condition that gives no reason whatsoever for your program to be stuck. Maybe there is a loop somewhere that never ends. Also, can we safely say that loop->task is never NULL? Because if any of the loop->task is null, then that's an invalid parameter and it will probably not be deleted. And your algorithm finishes when everything is deleted. – Lajos Arpad Nov 16 '21 at 11:38
  • So, it might be the case that your algorithm finishes when all events are removed, but some task values are NULL and as a result they are not removed. – Lajos Arpad Nov 16 '21 at 11:39
  • yeah you are right. it is stuck on line 660. I marked it wrong :). I assume there is a queue somewhere of tasks that need to be finished. And this queued task does not finish. Therefore it can't be deleted, since its still running. I don't know where it is but i got to find it somewhere – Dansen Nov 16 '21 at 12:42
  • @Dansen I think the key to the solution is to find a pattern of events. Something that is attempted to be deleted again and again. For example, when you call the function, it would be great to know the values of each events to be deleted. If you could debug your application and see what is being passed as a parameter, that might be helpful. Also, it's possible that some new events are always created. – Lajos Arpad Nov 17 '21 at 13:41