Why am I stuck in esp_event_loop_delete_default() function?

Dansen
Posts: 9
Joined: Thu Oct 28, 2021 9:42 am

Why am I stuck in esp_event_loop_delete_default() function?

Postby Dansen » Mon Nov 15, 2021 10:27 am

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:

Code: Select all

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.

Code: Select all

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:

Code: Select all

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());
}

Dansen
Posts: 9
Joined: Thu Oct 28, 2021 9:42 am

Re: Why am I stuck in esp_event_loop_delete_default() function?

Postby Dansen » Mon Nov 15, 2021 3:46 pm

Further Info:

My program stucks at this line:
https://github.com/espressif/esp-idf/bl ... ent.c#L659

It must have something to do with this function:

Code: Select all

esp_err_t example_connect(void)
{
#if EXAMPLE_DO_CONNECT
    if (s_semph_get_ip_addrs != NULL) {
        return ESP_ERR_INVALID_STATE;
    }
#endif
    start();
    ESP_ERROR_CHECK(esp_register_shutdown_handler(&stop));
    ESP_LOGI(TAG, "Waiting for IP(s)");
    for (int i = 0; i < NR_OF_IP_ADDRESSES_TO_WAIT_FOR; ++i) {
        xSemaphoreTake(s_semph_get_ip_addrs, 1000 / portMAX_DELAY);
    }
    // iterate over active interfaces, and print out IPs of "our" netifs
    esp_netif_t *netif = NULL;
    esp_netif_ip_info_t ip;
    for (int i = 0; i < esp_netif_get_nr_of_ifs(); ++i) {
        netif = esp_netif_next(netif);
        if (is_our_netif(TAG, netif)) {
            ESP_LOGI(TAG, "Connected to %s", esp_netif_get_desc(netif));
            ESP_ERROR_CHECK(esp_netif_get_ip_info(netif, &ip));

            ESP_LOGI(TAG, "- IPv4 address: " IPSTR, IP2STR(&ip.ip));
#ifdef CONFIG_EXAMPLE_CONNECT_IPV6
            esp_ip6_addr_t ip6[MAX_IP6_ADDRS_PER_NETIF];
            int ip6_addrs = esp_netif_get_all_ip6(netif, ip6);
            for (int j = 0; j < ip6_addrs; ++j) {
                esp_ip6_addr_type_t ipv6_type = esp_netif_ip6_get_addr_type(&(ip6[j]));
                ESP_LOGI(TAG, "- IPv6 address: " IPV6STR ", type: %s", IPV62STR(ip6[j]), s_ipv6_addr_types[ipv6_type]);
            }
#endif

        }
    }
    return ESP_OK;
}
this function gets called before trying to connect in stationmode

What do you guys think?

Who is online

Users browsing this forum: No registered users and 130 guests