What exactly is the event_handler function?

whateverever
Posts: 8
Joined: Wed May 03, 2017 7:21 pm

What exactly is the event_handler function?

Postby whateverever » Fri Jun 09, 2017 5:49 am

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?

ESP_Sprite
Posts: 9711
Joined: Thu Nov 26, 2015 4:08 am

Re: What exactly is the event_handler function?

Postby ESP_Sprite » Fri Jun 09, 2017 7:40 am

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.

whateverever
Posts: 8
Joined: Wed May 03, 2017 7:21 pm

Re: What exactly is the event_handler function?

Postby whateverever » Fri Jun 09, 2017 7:49 am

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) );
?

f.h-f.s.
Posts: 214
Joined: Thu Dec 08, 2016 2:53 pm

Re: What exactly is the event_handler function?

Postby f.h-f.s. » Fri Jun 09, 2017 8:13 am

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

whateverever
Posts: 8
Joined: Wed May 03, 2017 7:21 pm

Re: What exactly is the event_handler function?

Postby whateverever » Fri Jun 09, 2017 10:30 am

Ah, i see. Now i understand, why i need to return ESP_OK;
Thanks to you, guys!

Who is online

Users browsing this forum: Google [Bot] and 67 guests