Page 1 of 2

[answered] how to get my IP address?

Posted: Tue Apr 24, 2018 11:13 pm
by mzimmers
I know this is an absurdly simple question, but...I don't know how to determine the IP address I've been assigned by the DHCP server. I've seen some examples of how to do this in linux, but they aren't very portable. Does anyone have a simple technique for this?

Thanks.

Re: how to get my IP address?

Posted: Wed Apr 25, 2018 3:17 am
by ESP_Sprite
It's given to you in an event. See esp-idf/examples/wifi/simple_wifi/main/simple_wifi.c around line 50 for an example.

Re: how to get my IP address?

Posted: Wed Apr 25, 2018 3:44 pm
by mzimmers
Ah, thank you Sprite. So is that the only way to get it, or can I retrieve it programmatically (for example, with a LWIP equivalent of ioctl/SIOCGIFADDR?

Re: how to get my IP address?

Posted: Wed Apr 25, 2018 3:59 pm
by kolban
Howdy,
The function called "tcpip_adapter_get_ip_info" can be used to obtain your interface IP address, netmask and gateway. You can pass in TCPIP_ADAPTERE_IF_STA to get the information you desire.

Re: how to get my IP address?

Posted: Wed Apr 25, 2018 4:27 pm
by fly135
This is provided in the event handler....

Code: Select all

        case SYSTEM_EVENT_STA_GOT_IP:
          eprintf(eLOG_EVENTQ,"SYSTEM_EVENT_STA_GOT_IP\r\n");
          eprintf(eLOG_EVENTQ,"Got IP: %s\r\n",
                   ip4addr_ntoa(&event->event_info.got_ip.ip_info.ip));

Re: how to get my IP address?

Posted: Wed Apr 25, 2018 9:30 pm
by mzimmers
Hi Neil - yep, that did the trick. Code posted here for anyone else who might be interested (or for me since I'll probably forget).

Code: Select all

#include <tcpip_adapter.h>

tcpip_adapter_ip_info_t ipInfo; 
char str[256];
    	
// IP address.
tcpip_adapter_get_ip_info(TCPIP_ADAPTER_IF_STA, &ipInfo);
sprintf(str, "%x", ipInfo.ip.addr);

Re: how to get my IP address?

Posted: Fri Oct 26, 2018 6:25 am
by burkulesomesh43
mzimmers wrote:Hi Neil - yep, that did the trick. Code posted here for anyone else who might be interested (or for me since I'll probably forget).

Code: Select all

#include <tcpip_adapter.h>

tcpip_adapter_ip_info_t ipInfo; 
char str[256];
    	
// IP address.
tcpip_adapter_get_ip_info(TCPIP_ADAPTER_IF_STA, &ipInfo);
sprintf(str, "%x", ipInfo.ip.addr);
I had printed this ip in str, it showing the value 8600a8c0.

Re: how to get my IP address?

Posted: Fri Oct 26, 2018 10:46 am
by Vader_Mester
burkulesomesh43 wrote:
mzimmers wrote:Hi Neil - yep, that did the trick. Code posted here for anyone else who might be interested (or for me since I'll probably forget).

Code: Select all

#include <tcpip_adapter.h>

tcpip_adapter_ip_info_t ipInfo; 
char str[256];
    	
// IP address.
tcpip_adapter_get_ip_info(TCPIP_ADAPTER_IF_STA, &ipInfo);
sprintf(str, "%x", ipInfo.ip.addr);
I had printed this ip in str, it showing the value 8600a8c0.
If you used the same formating "%x", then you get the hex value. Try different formating.

Re: [answered] how to get my IP address?

Posted: Fri Oct 26, 2018 12:48 pm
by Resch2061
You can print the IP address as octets as follows (from the top of my head):

Code: Select all

printf("My IP: " IPSTR "\n", IP2STR(&ipinfo.ip));

Re: [answered] how to get my IP address?

Posted: Fri Oct 26, 2018 2:27 pm
by burkulesomesh43
Resch2061 wrote:You can print the IP address as octets as follows (from the top of my head):

Code: Select all

printf("My IP: " IPSTR "\n", IP2STR(&ipinfo.ip));
thank you. its working