How to get public IP address of the network my esp32 is connected to?

Zeni241
Posts: 86
Joined: Tue Nov 20, 2018 4:28 am

How to get public IP address of the network my esp32 is connected to?

Postby Zeni241 » Mon Nov 27, 2023 12:18 pm

I am trying to get public IP address of the network my esp32 is connected for use in geolocation.
My code is:

Code: Select all

esp_netif_t *sta_netif;

-			 sta_netif = esp_netif_create_default_wifi_sta();
-
-
if (bits & WIFI_CONNECTED_BIT)
                    {
                        esp_netif_ip_info_t ip_info;
                        
                        esp_netif_get_ip_info(sta_netif, &ip_info);
                        printf("My IPV4: " IPSTR "\n", IP2STR(&ip_info.ip));
                        printf("My GW: " IPSTR "\n", IP2STR(&ip_info.gw));
                        printf("My NETMASK: " IPSTR "\n", IP2STR(&ip_info.netmask));
	

-
-
-
-
The result I am getting is:
My IPV4: 192.168.100.156
My GW: 192.168.100.1
My NETMASK: 255.255.255.0
This IP4 address is not my public IPV4 address .
How can I get the correct IP of the network my esp32 is connected to so that I could use it for geolocation?

Thanks

Who is online

Users browsing this forum: Google [Bot] and 186 guests