[answered] how to get my IP address?
[answered] how to get my IP address?
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.
Thanks.
Last edited by mzimmers on Fri Apr 27, 2018 1:57 pm, edited 1 time in total.
-
- Posts: 9764
- Joined: Thu Nov 26, 2015 4:08 am
Re: how to get my IP address?
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?
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?
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.
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
Re: how to get my IP address?
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?
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);
-
- Posts: 132
- Joined: Tue Aug 14, 2018 6:21 am
- Location: India
Re: how to get my IP address?
I had printed this ip in str, it showing the value 8600a8c0.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);
--
Somesh Burkule
Somesh Burkule
- Vader_Mester
- Posts: 300
- Joined: Tue Dec 05, 2017 8:28 pm
- Location: Hungary
- Contact:
Re: how to get my IP address?
If you used the same formating "%x", then you get the hex value. Try different formating.burkulesomesh43 wrote:I had printed this ip in str, it showing the value 8600a8c0.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);
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);
}
Re: [answered] how to get my IP address?
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));
-
- Posts: 132
- Joined: Tue Aug 14, 2018 6:21 am
- Location: India
Re: [answered] how to get my IP address?
thank you. its workingResch2061 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));
--
Somesh Burkule
Somesh Burkule
Who is online
Users browsing this forum: Baidu [Spider] and 161 guests