[answered] how to get my IP address?

User avatar
mzimmers
Posts: 643
Joined: Wed Mar 07, 2018 11:54 pm
Location: USA

[answered] how to get my IP address?

Postby mzimmers » Tue Apr 24, 2018 11:13 pm

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.
Last edited by mzimmers on Fri Apr 27, 2018 1:57 pm, edited 1 time in total.

ESP_Sprite
Posts: 9764
Joined: Thu Nov 26, 2015 4:08 am

Re: how to get my IP address?

Postby ESP_Sprite » Wed Apr 25, 2018 3:17 am

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.

User avatar
mzimmers
Posts: 643
Joined: Wed Mar 07, 2018 11:54 pm
Location: USA

Re: how to get my IP address?

Postby mzimmers » Wed Apr 25, 2018 3:44 pm

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?

User avatar
kolban
Posts: 1683
Joined: Mon Nov 16, 2015 4:43 pm
Location: Texas, USA

Re: how to get my IP address?

Postby kolban » Wed Apr 25, 2018 3:59 pm

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.
Free book on ESP32 available here: https://leanpub.com/kolban-ESP32

User avatar
fly135
Posts: 606
Joined: Wed Jan 03, 2018 8:33 pm
Location: Orlando, FL

Re: how to get my IP address?

Postby fly135 » Wed Apr 25, 2018 4:27 pm

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));

User avatar
mzimmers
Posts: 643
Joined: Wed Mar 07, 2018 11:54 pm
Location: USA

Re: how to get my IP address?

Postby mzimmers » Wed Apr 25, 2018 9:30 pm

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);

burkulesomesh43
Posts: 132
Joined: Tue Aug 14, 2018 6:21 am
Location: India

Re: how to get my IP address?

Postby burkulesomesh43 » Fri Oct 26, 2018 6:25 am

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.
--
Somesh Burkule

User avatar
Vader_Mester
Posts: 300
Joined: Tue Dec 05, 2017 8:28 pm
Location: Hungary
Contact:

Re: how to get my IP address?

Postby Vader_Mester » Fri Oct 26, 2018 10:46 am

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.

Code: Select all

task_t coffeeTask()
{
	while(atWork){
		if(!xStreamBufferIsEmpty(mug)){
			coffeeDrink(mug);
		} else {
			xTaskCreate(sBrew, "brew", 9000, &mug, 1, NULL);
			xSemaphoreTake(sCoffeeRdy, portMAX_DELAY);
		}
	}
	vTaskDelete(NULL);
}

Resch2061
Posts: 40
Joined: Mon May 01, 2017 1:56 pm

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

Postby Resch2061 » Fri Oct 26, 2018 12:48 pm

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));

burkulesomesh43
Posts: 132
Joined: Tue Aug 14, 2018 6:21 am
Location: India

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

Postby burkulesomesh43 » Fri Oct 26, 2018 2:27 pm

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
--
Somesh Burkule

Who is online

Users browsing this forum: No registered users and 140 guests