esp_event_loop_create_default "system event loop not initialized" with wifi

Clickau
Posts: 4
Joined: Thu Jan 02, 2020 9:55 pm

esp_event_loop_create_default "system event loop not initialized" with wifi

Postby Clickau » Mon Oct 19, 2020 2:46 pm

I am trying to use the IDF functions to connect to wifi in a project that uses Arduino as a component. When using esp_event_loop_create_default after tcpip_adapter_init and before configuring wifi, the following error appears every time my event handler is called: "event: system event loop not initialized via esp_event_loop_init", but everything works fine, including connection and http requests. If I replace esp_event_loop_create_default with esp_event_loop_init(nullptr, nullptr), which I understand is deprecated, the error goes away and everything still works fine. The strangest thing is that when I created a new project to test this, neither esp_event_loop_create_default nor esp_event_loop_init produce an error. Both the test project and the normal one are on PlatformIO, with framework-espidf @ 3.40001.200521 which I think is IDF v4.0.1 and I included Arduino as a component and initialized it in the test project too. I recorded the logs when trying both options and the only difference is the thread semaphore (I think). I attached the diff without timestamps.
This is the code used for initializing wifi:

Code: Select all

    tcpip_adapter_init();
    ESP_ERROR_CHECK(esp_event_loop_create_default());
    ESP_ERROR_CHECK(esp_event_loop_init(nullptr, nullptr));
    ESP_ERROR_CHECK(esp_event_handler_register(WIFI_EVENT, ESP_EVENT_ANY_ID, wifi_event_handler, nullptr));
    ESP_ERROR_CHECK(esp_event_handler_register(IP_EVENT, IP_EVENT_STA_GOT_IP, wifi_event_handler, nullptr));
    wifi_init_config_t init_cfg = WIFI_INIT_CONFIG_DEFAULT();
    ESP_ERROR_CHECK(esp_wifi_init(&init_cfg));
    ESP_ERROR_CHECK(esp_wifi_set_mode(WIFI_MODE_STA));
    wifi_config_t wifi_config = {};
    wifi_config.sta.threshold.authmode = WIFI_AUTH_WPA2_PSK;
    wifi_config.sta.pmf_cfg.capable = true;
    wifi_config.sta.pmf_cfg.required = false;
    strcpy((char *)wifi_config.sta.ssid, ssid.c_str());
    strcpy((char *)wifi_config.sta.password, password.c_str());
    
    ESP_ERROR_CHECK(esp_wifi_set_config(ESP_IF_WIFI_STA, &wifi_config));
    ESP_ERROR_CHECK(esp_wifi_start());
And the event handler:

Code: Select all

void wifi_event_handler(void *, esp_event_base_t base, int32_t id, void *)
{
    if (base == WIFI_EVENT && id == WIFI_EVENT_STA_START)
    {
        LOG_D("Wifi started");
        esp_err_t err = esp_wifi_connect();
        if (err != ESP_OK)
        {
            LOG_D("esp_wifi_connect error: %s", esp_err_to_name(err));
            xSemaphoreTake(wifiWorkingMutex, portMAX_DELAY);
            wifiWorking = false;
            xSemaphoreGive(wifiWorkingMutex);
        }
    }
    else if (base == WIFI_EVENT && id == WIFI_EVENT_STA_DISCONNECTED)
    {
        LOG_D("Disconnected");
        xSemaphoreTake(wifiWorkingMutex, portMAX_DELAY);
        wifiWorking = false;
        xSemaphoreGive(wifiWorkingMutex);
        esp_err_t err = esp_wifi_connect();
        if (err != ESP_OK)
        {
            LOG_D("esp_wifi_connect error: %s", esp_err_to_name(err));
        }
    }
    else if (base == IP_EVENT && id == IP_EVENT_STA_GOT_IP)
    {
        LOG_D("Got IP");
        xSemaphoreTake(wifiWorkingMutex, portMAX_DELAY);
        wifiWorking = true;
        xSemaphoreGive(wifiWorkingMutex);
    }
}
And here is the diff: (left is esp_event_loop_create_default)

Code: Select all

4,6c4,6
< I  wifi:wifi driver task: 3ffc4d60, prio:23, stack:6656, core=0
< V  esp_adapter: thread sem create: sem=0x3ffc4ec8
< V  esp_adapter: thread sem get: sem=0x3ffc4ec8
---
> I  wifi:wifi driver task: 3ffc4d84, prio:23, stack:6656, core=0
> V  esp_adapter: thread sem create: sem=0x3ffc4eec
> V  esp_adapter: thread sem get: sem=0x3ffc4eec
33,35c33,35
< V  esp_adapter: thread sem get: sem=0x3ffc4ec8
< V  esp_adapter: thread sem get: sem=0x3ffc4ec8
< V  esp_adapter: thread sem get: sem=0x3ffc4ec8
---
> V  esp_adapter: thread sem get: sem=0x3ffc4eec
> V  esp_adapter: thread sem get: sem=0x3ffc4eec
> V  esp_adapter: thread sem get: sem=0x3ffc4eec
62,65c62,64
< E  event: system event loop not initialized via esp_event_loop_init
< 6125D 1c event: running post WIFI_EVENT:2 with handler 0x400d3b54 on loop 0x3ffbd6dc
< V  esp_adapter: thread sem create: sem=0x3ffc9e00
< V  esp_adapter: thread sem get: sem=0x3ffc9e00
---
> 6116 D 1c event: running post WIFI_EVENT:2 with handler 0x400d3b54 on loop 0x3ffbd6dc
> V  esp_adapter: thread sem create: sem=0x3ffc9e58
> V  esp_adapter: thread sem get: sem=0x3ffc9e58
71a71
> D  event: running post SYSTEM_EVENT:2 with handler 0x400eb48c on loop 0x3ffbd6dc
77c77
< I  wifi:security: WPA2-PSK, phy: bgn, rssi: -76
---
> I  wifi:security: WPA2-PSK, phy: bgn, rssi: -74
81,82d80
< E  event: system event loop not initialized via esp_event_loop_init
< I  wifi:AP's beacon interval = 25600 us, DTIM period = 1
83a82
> I  wifi:AP's beacon interval = 25600 us, DTIM period = 1
85c84
< V  esp_adapter: thread sem get: sem=0x3ffc9e00
---
> V  esp_adapter: thread sem get: sem=0x3ffc9e58
95c94
< D  tcpip_adapter: if0 start ip lost tmr: no need start because netif=0x3ffc9e90 interval=120 ip=0
---
> D  tcpip_adapter: if0 start ip lost tmr: no need start because netif=0x3ffc9ee8 interval=120 ip=0
99a99
> D  event: running post SYSTEM_EVENT:4 with handler 0x400eb48c on loop 0x3ffbd6dc
103d102
< E  event: system event loop not initialized via esp_event_loop_init
106a106
> D  event: running post SYSTEM_EVENT:7 with handler 0x400eb48c on loop 0x3ffbd6dc

Who is online

Users browsing this forum: No registered users and 73 guests