Esp IDF examples: does WiFi station_example_main.c need 2 event handler instances?

LaWi14
Posts: 13
Joined: Wed Nov 20, 2019 6:05 pm

Esp IDF examples: does WiFi station_example_main.c need 2 event handler instances?

Postby LaWi14 » Mon Nov 10, 2025 5:18 pm

In esp-idf/examples/wifi/getting_started/station/main/station_example_main.c there are 2 event handler instances defined and used.
Wouldn't it suffice to use only 1 or even none and setting the last argument in the register function to NULL?

Code: Select all

    esp_event_handler_instance_t instance_any_id;
    esp_event_handler_instance_t instance_got_ip;
    ESP_ERROR_CHECK(esp_event_handler_instance_register(WIFI_EVENT,
                                                        ESP_EVENT_ANY_ID,
                                                        &event_handler,
                                                        NULL,
                                                        &instance_any_id));
    ESP_ERROR_CHECK(esp_event_handler_instance_register(IP_EVENT,
                                                        IP_EVENT_STA_GOT_IP,
                                                        &event_handler,
                                                        NULL,
                                                        &instance_got_ip));

MicroController
Posts: 2661
Joined: Mon Oct 17, 2022 7:38 pm
Location: Europe, Germany

Re: Esp IDF examples: does WiFi station_example_main.c need 2 event handler instances?

Postby MicroController » Mon Nov 10, 2025 9:20 pm

Wouldn't it suffice to use only 1 or even none...?
Suffice ... for what?
If you don't care about a specific event, you obviously don't have to register any event listener for it.

Note though that in the case of WiFi you do care for both IP_EVENT_STA_GOT_IP and WIFI_EVENT_STA_DISCONNECTED, at least if you have a server (HTTP,...) running because it may need to be (re)started when a (new) IP address is acquired, and you may want to shut it down when the WiFi connection breaks.

LaWi14
Posts: 13
Joined: Wed Nov 20, 2019 6:05 pm

Re: Esp IDF examples: does WiFi station_example_main.c need 2 event handler instances?

Postby LaWi14 » Tue Nov 11, 2025 4:36 pm

I do not refer to the event handlers (type esp_event_handler_t), but to the event handler instances (type esp_event_handler_instance_t). I do not see different behaviour for the above code and a code where I set the handler instances to NULL:

Code: Select all

    ESP_ERROR_CHECK(esp_event_handler_instance_register(WIFI_EVENT,
                                                        ESP_EVENT_ANY_ID,
                                                        &event_handler,
                                                        NULL,
                                                        NULL));
    ESP_ERROR_CHECK(esp_event_handler_instance_register(IP_EVENT,
                                                        IP_EVENT_STA_GOT_IP,
                                                        &event_handler,
                                                        NULL,
                                                        NULL));
The documentation says
instance – [out] An event handler instance object related to the registered event handler and data, can be NULL. This needs to be kept if the specific callback instance should be unregistered before deleting the whole event loop. Registering the same event handler multiple times is possible and yields distinct instance objects. The data can be the same for all registrations. If no unregistration is needed, but the handler should be deleted when the event loop is deleted, instance can be NULL.
So the use of an non-NULL esp_event_handler_instance_t seems to be for multiple registration / unambigouos unregistering, using NULL will give the same result as using the (old, "legacy") call esp_event_handler_register(). And that may be sufficient in station_example_main.c, or?

Who is online

Users browsing this forum: No registered users and 8 guests