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

How to properly read the local IP address of the device (IDFGH-14355) #15145

Closed
3 tasks done
pavel808 opened this issue Jan 5, 2025 · 3 comments
Closed
3 tasks done
Labels
Status: Opened Issue is new

Comments

@pavel808
Copy link

pavel808 commented Jan 5, 2025

Answers checklist.

  • I have read the documentation ESP-IDF Programming Guide and the issue is not addressed there.
  • I have updated my IDF branch (master or release) to the latest version and checked that the issue is present there.
  • I have searched the issue tracker for a similar issue and not found a similar issue.

General issue report

I have an esp32-s3 device and I am trying to read its assigned local Wi-Fi IP address as below using esp_netif_get_default_netif()

This does not work unfortunately. intf is not NULL, but doesn't get the actual IP address etc.

What is strange however is that this actually worked on a previous network, but not on the current one.

I need to obtain the IP address automatically because I need to delegate an mDNS hostname using mdns_delegate_hostname_add() .

Hard-coding the device IP address works fine and the mDNS server works as expected then, but of course the address needs to be read from the network interface somehow.

esp_netif_t *intf = esp_netif_get_default_netif();;
while (intf == NULL)
{
    ESP_LOGE(TAG, "ERROR ! netif is NULL");
    sleep(2);
    intf = esp_netif_get_default_netif();
}

mdns_ip_addr_t addr4, addr6;
addr4.addr.type = ESP_IPADDR_TYPE_V4;
esp_netif_ip_info_t info;
esp_netif_get_ip_info(intf, &info);

// IP address is 0.0.0.0 when printed
ESP_LOGI(TAG, "IP Address of device is : " IPSTR "\n", IP2STR(&info.ip));
@espressif-bot espressif-bot added the Status: Opened Issue is new label Jan 5, 2025
@github-actions github-actions bot changed the title How to properly read the local IP address of the device How to properly read the local IP address of the device (IDFGH-14355) Jan 5, 2025
@nopnop2002
Copy link

    esp_netif_ip_info_t ip_info;
    esp_netif_get_ip_info(esp_netif_get_handle_from_ifkey("WIFI_STA_DEF"), &ip_info);
    ESP_LOGI(TAG, "ip_info.ip=%s", ip4addr_ntoa((const ip4_addr_t *)&ip_info.ip));
    ESP_LOGI(TAG, "ip_info.ip=" IPSTR, IP2STR(&ip_info.ip));

@david-cermak
Copy link
Collaborator

The issue arises because the DHCP client hasn't yet assigned an IP address to the interface when you're calling esp_netif_get_ip_info(), so it returns 0.0.0.0. This is expected behavior during the network setup phase. Instead of polling for the network interface, use the ESP-IDF event system to listen for the IP_EVENT_STA_GOT_IP event, which indicates that the interface has acquired a valid IP address. This ensures you only attempt to retrieve the IP once it’s ready, avoiding timing issues. After receiving the event, you can safely use the IP address for tasks like delegating the mDNS hostname.

@pavel808
Copy link
Author

@nopnop2002 @david-cermak Thanks. I have solved it now. Assigning a callback to assign the hostname when IP_EVENT_STA_GOT_IP occurs works the best for me.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Status: Opened Issue is new
Projects
None yet
Development

No branches or pull requests

4 participants