Page 1 of 1

What exactly is the event_handler function?

Posted: Fri Jun 09, 2017 5:49 am
by whateverever
Might be a stupid question, but i'm new i all this esp32 sdk programming. What exactly is the event_handler in this function:

Code: Select all

esp_err_t event_handler(void *ctx, system_event_t *event)
{
    return ESP_OK;
}
Why should i implement this function in the beginning of my script?

Re: What exactly is the event_handler function?

Posted: Fri Jun 09, 2017 7:40 am
by ESP_Sprite
The event handler is the way you can get notifications from eg WiFi (connected, disconnected etc). The event handler you have there is not very useful for that, it's just a stub.

Re: What exactly is the event_handler function?

Posted: Fri Jun 09, 2017 7:49 am
by whateverever
So i have to work with some kind of ESP_ERROR_CHECK?
In example:

Code: Select all

ESP_ERROR_CHECK( esp_wifi_start() );
or

Code: Select all

wifi_init_config_t initconf = WIFI_INIT_CONFIG_DEFAULT();
ESP_ERROR_CHECK( esp_wifi_init(&initconf) );
?

Or is it more for functions like

Code: Select all

ESP_ERROR_CHECK( esp_event_loop_init(event_handler, NULL) );
?

Re: What exactly is the event_handler function?

Posted: Fri Jun 09, 2017 8:13 am
by f.h-f.s.
You can see what ESP_ERROR_CHECK does in esp-idf/components/esp32/include/esp_err.h

Code: Select all

#ifdef NDEBUG
#define ESP_ERROR_CHECK(x) do {                                         \
        esp_err_t rc = (x);                                             \
        (void) sizeof(rc);                                              \
    } while(0);
#else
#define ESP_ERROR_CHECK(x) do {                                         \
        esp_err_t rc = (x);                                             \
        if (rc != ESP_OK) {                                             \
            _esp_error_check_failed(rc, __FILE__, __LINE__,             \
                                    __ASSERT_FUNC, #x);                 \
        }                                                               \
    } while(0);
#endif
in the examples you can see multiple error checks, it checks if the function returns ESP_OK, otherwise it will take action.

Code: Select all

    tcpip_adapter_init();
    wifi_event_group = xEventGroupCreate();
    ESP_ERROR_CHECK( esp_event_loop_init(event_handler, NULL) );
    wifi_init_config_t cfg = WIFI_INIT_CONFIG_DEFAULT();
    ESP_ERROR_CHECK( esp_wifi_init(&cfg) );
    ESP_ERROR_CHECK( esp_wifi_set_storage(WIFI_STORAGE_RAM) );
    wifi_config_t wifi_config = {
        .sta = {
            .ssid = EXAMPLE_WIFI_SSID,
            .password = EXAMPLE_WIFI_PASS,
        },
    };
    ESP_LOGI(TAG, "Setting WiFi configuration SSID %s...", wifi_config.sta.ssid);
    ESP_ERROR_CHECK( esp_wifi_set_mode(WIFI_MODE_STA) );
    ESP_ERROR_CHECK( esp_wifi_set_config(ESP_IF_WIFI_STA, &wifi_config) );
    ESP_ERROR_CHECK( esp_wifi_start() );
   
This initializes your event handler, and error check checks if that function returns succesfully.

Code: Select all

    ESP_ERROR_CHECK( esp_event_loop_init(event_handler, NULL) );
There are many event that can be handled by the event handler. see esp-idf/components/esp32/include/esp_event.h and the event_handler functions in the examples. https://github.com/espressif/esp-idf/bl ... ple_main.c

Re: What exactly is the event_handler function?

Posted: Fri Jun 09, 2017 10:30 am
by whateverever
Ah, i see. Now i understand, why i need to return ESP_OK;
Thanks to you, guys!