Page 1 of 1

(solved) esp_event_loop_create_default() returned ESP_ERR_INVALID_STATE

Posted: Fri Oct 29, 2021 4:20 am
by mzimmers
Hi all -

Trying to write an initialization routine...starts like this:

Code: Select all

esp_err_t wifiInit(wifi_mode_t mode) {
    esp_err_t err = ESP_OK;
    do {
        // step 1 in https://docs.espressif.com/projects/esp-idf/en/latest/api-guides/wifi.html:
        // Wifi/LwIP Init.
        // step 1.1: call esp_netif_init()
        err = esp_netif_init();
        if (err != ESP_OK) {
            ESP_LOGE(TAG, "wifiInit(): esp_netif_init() returned \"%s\".", esp_err_to_name(err));
            continue;
        }

        // step 1.2 
        // identify the event handler.
        err = esp_event_loop_create_default();
        if (err != ESP_OK) {
            ESP_LOGE(TAG, "wifiInit(): esp_event_loop_create_default() returned \"%s\".", esp_err_to_name(err));
            continue;
        }
And the call to esp_event_loop_create_default() returns ESP_ERR_INVALID_STATE.

What state is the error message referring to? And, what could be causing this so early in the routine?

Thanks...

Re: esp_event_loop_create_default() returned ESP_ERR_INVALID_STATE

Posted: Fri Oct 29, 2021 4:59 am
by boarchuz
Probably because it's already initialised. Many components depend on the event loop being initialised, so you should do it once early in your main initialisation. Repeated calls will return ESP_ERR_INVALID_STATE.

Is there another esp_event_loop_create_default() somewhere before this wifiInit()?

Re: esp_event_loop_create_default() returned ESP_ERR_INVALID_STATE

Posted: Sat Oct 30, 2021 3:31 am
by mzimmers
Hi boarchuz - that was the problem. I had one call, but it was in a routine that was repeated. Thanks for the help.

Re: (solved) esp_event_loop_create_default() returned ESP_ERR_INVALID_STATE

Posted: Thu Jun 09, 2022 2:49 pm
by leschge
This behavior is not documented in the reference, and it should be declared what cause this error raises. (Probably because it's already initialised. )
https://docs.espressif.com/projects/esp ... e_defaultv

Re: (solved) esp_event_loop_create_default() returned ESP_ERR_INVALID_STATE

Posted: Wed Oct 26, 2022 5:24 pm
by HPekiM
Is there any means to detect if "esp_event_loop_create_default()" was already called before? Or do I have to keep track of that call on my own?

Thanks in advance
Mike

Re: (solved) esp_event_loop_create_default() returned ESP_ERR_INVALID_STATE

Posted: Wed Oct 26, 2022 5:28 pm
by mzimmers
Well, you could use the return from the call.