Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ESP32 gets no IP and returns no error if esp_netif_create_default_wifi_sta() called before esp_event_loop_create_default() (IDFGH-10321) #11580

Closed
mik13ST opened this issue Jun 2, 2023 · 1 comment
Assignees
Labels
Resolution: NA Issue resolution is unavailable Status: Done Issue is done internally

Comments

@mik13ST
Copy link
Contributor

mik13ST commented Jun 2, 2023

I am learning ESP32 and ESP-IDF. I was trying to make it connect to WiFi. I looked at the https://github.com/espressif/esp-idf/tree/master/examples/wifi/getting_started/station example and copied relevant method calls until things started working. In this code

void wifi_init_sta(void)
{
    s_wifi_event_group = xEventGroupCreate();

    ESP_ERROR_CHECK(esp_netif_init());

    ESP_ERROR_CHECK(esp_event_loop_create_default());
    esp_netif_create_default_wifi_sta();

    wifi_init_config_t cfg = WIFI_INIT_CONFIG_DEFAULT();
    ESP_ERROR_CHECK(esp_wifi_init(&cfg));

I skipped esp_event_loop_create_default() because I didn't know why I need it at first. At a later point I found out why and added this call somewhere before esp_event_handler_instance_register() calls. This worked, I got the WIFI_EVENT_STA_CONNECTED event in my event handler (again, derived from the example), but I never got the IP_EVENT_STA_GOT_IP nor the device ever asked for IP (checked on my router). No runtime errors or warning logs were generated that would hint this being the problem.

I think this should result in an error of sorts. It's not obvious there is a tight coupling (or dependency) between these two methods and the example doesn't mention this tight coupling either. This was very difficult to troubleshoot for a beginner like me, I spent a couple of hours on this.

The reason for this call order requirement is because esp_netif_create_default_wifi_sta() calls esp_event_handler_register() which as a result relies on already existing event loop created by the user.

Also, the esp_netif_create_default_wifi_sta documentation doesn't mention this requirement either.

The implementation looks like this:

/**
 * @brief User init default station (official API)
 */
esp_netif_t* esp_netif_create_default_wifi_sta(void)
{
    esp_netif_config_t cfg = ESP_NETIF_DEFAULT_WIFI_STA();
    esp_netif_t *netif = esp_netif_new(&cfg);
    assert(netif);
    esp_netif_attach_wifi_station(netif);
    esp_wifi_set_default_wifi_sta_handlers();
    return netif;
}

The esp_wifi_set_default_wifi_sta_handlers() call internally returns errors but this function doesn't handle them in any way. I think even assert() as seen in assert(netif) would be better than this. Please don't ignore errors.

An alternative could be the esp_wifi_set_default_wifi_sta_handlers function would create its own event loop so it wouldn't depend on the user's event loop.

@espressif-bot espressif-bot added the Status: Opened Issue is new label Jun 2, 2023
@github-actions github-actions bot changed the title ESP32 gets no IP and returns no error if esp_netif_create_default_wifi_sta() called before esp_event_loop_create_default() ESP32 gets no IP and returns no error if esp_netif_create_default_wifi_sta() called before esp_event_loop_create_default() (IDFGH-10321) Jun 2, 2023
@KaeLL
Copy link
Contributor

KaeLL commented Jun 2, 2023

Good point. Wi-Fi deinitialization has the same issue, where the order of function calls isn't clear.

@espressif-bot espressif-bot added Status: In Progress Work is in progress and removed Status: Opened Issue is new labels Jun 21, 2023
@espressif-bot espressif-bot added Status: Done Issue is done internally Resolution: NA Issue resolution is unavailable and removed Status: In Progress Work is in progress labels Jul 21, 2023
espressif-bot pushed a commit that referenced this issue Aug 20, 2023
- Added ESP_ERROR_CHECK() checks to `esp_wifi_set_default_wifi_..._handlers()` calls
- Added ESP_ERROR_CHECH() to `esp_netif_attach_wifi_...()` calls
- Updated documentation to reflect the changes

Closes #11580
espressif-bot pushed a commit that referenced this issue Sep 1, 2023
- Added ESP_ERROR_CHECK() checks to `esp_wifi_set_default_wifi_..._handlers()` calls
- Added ESP_ERROR_CHECH() to `esp_netif_attach_wifi_...()` calls
- Updated documentation to reflect the changes

Closes #11580
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Resolution: NA Issue resolution is unavailable Status: Done Issue is done internally
Projects
None yet
Development

No branches or pull requests

4 participants