Page 1 of 1

esp_event_post doesn't seem to be posting to default event loop

Posted: Tue Feb 28, 2023 4:35 pm
by karunt
My project has three source codes in addition to main.c. They are spiffs.c, event_loop.c and wifi_prov_mgr.c. Wifi_prov_mgr.c is a copy of the main program from ESP's wifi_provisioning example (this example has its own event_handler to handle events posted to the default event loop). The only change I made to the wifi_provisioning example upon copying it to wifi_prov_mgr.c is that I took away the creation of the default even loop and moved it to another file - event_loop.c, which is intended to manage execution of other source codes upon the occurrence of some custom events from each of the various .c files.

From main.c, I call the starting functions in both event_loop.c and spiffs.c. This is what my code for event_loop.c looks like:

Code: Select all

ESP_EVENT_DEFINE_BASE(CUSTOM_EVENT);

static void event_loop_event_handler(void* arg, esp_event_base_t event_base,
                          int32_t event_id, void* event_data)
{if (event_base == CUSTOM_EVENT){
    switch (event_id) {
    case SPIFFSDONE:
        ESP_LOGI(TAG, "Done with spiffs.");
        wifi_provision();
        break;
    case WIFIDONE:
        ESP_LOGI(TAG, "Done with wifi_prov.");
        break;
    }
  }
}

/* Initialize the event loop */
    void event_loop_start(void){
        ESP_ERROR_CHECK(esp_event_loop_create_default());
        ESP_ERROR_CHECK(esp_event_handler_instance_register(CUSTOM_EVENT, ESP_EVENT_ANY_ID, event_handler1, NULL, NULL));
    }
When spiffs.c is done, it posts a SPIFFS_DONE event to the default loop:

Code: Select all

ESP_ERROR_CHECK(esp_event_post(CUSTOM_EVENT, SPIFFSDONE, NULL, 0, 0));
and event_loop.c prints
Done with spiffs
and starts wifi_provision() from wifi_prov_mgr.c.

However, when wifi_provision.c posts CUSTOM_EVENT/WIFIDONE to the default loop

Code: Select all

esp_event_post(CUSTOM_EVENT, WIFIDONE, NULL, 0, 0);
nothing happens. I would have expected event_loop.c to print
Done with wifi_prov
The odd part is that if I call wifi_provision() in wifi_prov_mgr.c directly from main.c, it's CUSTOM_EVENT (WIFIDONE) event is recognized by the event_loop_event_handler function in event_loop.c and the function prints
Done with wifi_prov
.

Can someone help me figure out why event_loop_event_handler() isn't able to recognize the CUSTOM_EVENT WIFIDONE event posted by wifi_prov_mgr.c when wifi_provision() is called directly from event_loop.c instead of main.c?