How to get IP address?

Mithras
Posts: 1
Joined: Sun Mar 29, 2020 7:33 pm

How to get IP address?

Postby Mithras » Sun Mar 29, 2020 8:14 pm

Hello there.
Please can you tell me easy method how to get IP address?
Actually I am trying something like that:

Code: Select all

 esp_netif_ip_info_t ip_info;
    esp_netif_get_ip_info(IP_EVENT_STA_GOT_IP,&ip_info);
    printf("My IP: " IPSTR "\n", IP2STR(&ip_info.ip));
    printf("My GW: " IPSTR "\n", IP2STR(&ip_info.gw));
    printf("My NETMASK: " IPSTR "\n", IP2STR(&ip_info.netmask));
But output looks little different from expectation:
I (29194) esp_netif_handlers: sta ip: 192.168.1.22, mask: 255.255.255.0, gw: 192.168.1.1
I (29194) connect: Connected

My IP: 156.113.13.128
My GW: 176.50.64.63
My NETMASK: 128.49.253.63

rsimpsonbusa
Posts: 131
Joined: Tue May 17, 2016 8:12 pm

Re: How to get IP address?

Postby rsimpsonbusa » Fri Apr 03, 2020 8:40 pm

Hi.

You seem to be connected to an AP, that usually have the 192.168.x.x address. "Your" IP is the Routers IP not the AP. Try that.

chegewara
Posts: 2378
Joined: Wed Jun 14, 2017 9:00 pm

Re: How to get IP address?

Postby chegewara » Sat Apr 04, 2020 9:00 pm

This is wrong use of printf.
IP2STR macro returns string, to use it with printf you have to use %s, not IPSTR.

ESP_igrr
Posts: 2072
Joined: Tue Dec 01, 2015 8:37 am

Re: How to get IP address?

Postby ESP_igrr » Sun Apr 05, 2020 10:56 am

I think the printf usage is correct, this is indeed how IPSTR and IP2STR should be used.

You might be confused about the expect result though. If the ESP32 is connected to an AP, it gets a local address from this IP. The address printed by the ESP indeed looks like a local address. On the other hand, 156.113.13.128 is a global IP address, e.g. the one which your AP gets from the internet provider. However the AP does network address translation, so the ESP can use its local address to talk to the remote hosts.

nvannote
Posts: 51
Joined: Thu Nov 14, 2019 10:42 pm

Re: How to get IP address?

Postby nvannote » Sun Apr 05, 2020 10:59 am

Hi Mithras,

Looking at the code you posted; you're calling esp_netif_get_ip_info with a network interface of IP_EVENT_STA_GOT_IP which is an event type, not an interface. You probably wanted to use ESP_IF_WIFI_STA.

Note, While I do have a snapshot of v4.1-beta; I am currently setup for v4.0 release/stable. Seems they changed these very APIs (tcpip_adapter_* to esp_netif_*) going forward; So I have not verified the above with a compile; but the docs and a search of the ESP-IDF codebase seems to confirm the above.


Hi chegewara,

The IP2STR macro expands to 4 integers that represent the octets of the IP address, separated by commas; not a string.

The IPSTR macro expands to "%d.%d.%d.%d".

The printf as it is being used in the original post is correct.

Best Regards

johnny_henriksen
Posts: 1
Joined: Mon Apr 12, 2021 7:10 am

Re: How to get IP address?

Postby johnny_henriksen » Mon Apr 12, 2021 7:49 am

Hi Mithras,

Small advice for future: if function returns error codes check them - it makes things easier.
This is the link to esp_netif_get_ip_info() documentation.

https://docs.espressif.com/projects/esp ... _ip_info_t

If you check the error code that your implementation returns you will see it is "ESP_ERR_ESP_NETIF_INVALID_PARAMS" error. The reason for that error is you are not passing a pointer to network interface handler but integer (or something else). That is the same reason why nvannote's suggestion also wont work.

If you are using an example code you can obtain interface handler by using

Code: Select all

 esp_netif_t *netif = get_example_netif()
If you are using your own code than you probably know how to get it.

So your code should look something like this:

Code: Select all

    esp_netif_t *netif = function_that_returns_netif_ptr(); // there are different ways for this I guess 
    esp_netif_ip_info_t ip_info;
    esp_netif_get_ip_info(netif, &ip_info);
    printf("My IP: " IPSTR "\n", IP2STR(&ip_info.ip));
    printf("My GW: " IPSTR "\n", IP2STR(&ip_info.gw));
    printf("My NETMASK: " IPSTR "\n", IP2STR(&ip_info.netmask));
Best regards

juand1479
Posts: 1
Joined: Tue Dec 06, 2022 3:20 am

Re: How to get IP address?

Postby juand1479 » Tue Jan 17, 2023 6:26 am

Por si de pronto algunos quedaron con la duda respecto a este post, otra forma óptima para obtener la ip es por medio del handler encargado de la conexión a wifi, si están un poco perdidos en cuanto a este tema adjunto un código de ejemplo para visualizar mejor la estructura general.

https://github.com/espressif/esp-idf/bl ... ple_main.c

en la sección que muestro a continuación del código anterior es donde debemos incluir el código:

Code: Select all

tatic void event_handler(void* arg, esp_event_base_t event_base,
                                int32_t event_id, void* event_data)
{
    if (event_base == WIFI_EVENT && event_id == WIFI_EVENT_STA_START) {
        esp_wifi_connect();
    } else if (event_base == WIFI_EVENT && event_id == WIFI_EVENT_STA_DISCONNECTED) {
        if (s_retry_num < EXAMPLE_ESP_MAXIMUM_RETRY) {
            esp_wifi_connect();
            s_retry_num++;
            ESP_LOGI(TAG, "retry to connect to the AP");
        } else {
            xEventGroupSetBits(s_wifi_event_group, WIFI_FAIL_BIT);
        }
        ESP_LOGI(TAG,"connect to the AP fail");
    } else if (event_base == IP_EVENT && event_id == IP_EVENT_STA_GOT_IP) {
        ip_event_got_ip_t* event = (ip_event_got_ip_t*) event_data;
        ESP_LOGI(TAG, "got ip:" IPSTR, IP2STR(&event->ip_info.ip));  //  <----------------------( aqui es donde se obtiene la ip )
        s_retry_num = 0;
        xEventGroupSetBits(s_wifi_event_group, WIFI_CONNECTED_BIT);
    }
}

asegurense de realizarlo en el procedimiento de IP_EVENT_STA_GOT_IP de otra forma obtendrán el error que presentan las personas anteriores ya que por ejemplo en WIFI_EVENT_STA_START aún no se ha establecido una dirección ip por lo que se está llenando con cualquier otro valor :D :D :D

sergiomarina
Posts: 48
Joined: Wed Feb 05, 2020 6:29 pm

Re: How to get IP address?

Postby sergiomarina » Sat Feb 18, 2023 8:38 am

look into (IDF 5.0) folder
esp-idf\examples\common_components
open
protocol_examples_common.h
and see the macro
#define get_example_netif() get_example_netif_from_desc(EXAMPLE_NETIF_DESC_STA)

in your code (after successful IP connection)
esp_netif_ip_info_t ip_infoMyIf;
esp_netif_get_ip_info(get_example_netif(),&ip_infoMyIf);

now all data are loaded in the struct "esp_netif_ip_info_t" "ip_infoMyIf"
So you can retrieve them

It worked for me.

Who is online

Users browsing this forum: Majestic-12 [Bot] and 174 guests