how to convert esp_ip4_addr_t to ip_addr_t

korstiaan
Posts: 5
Joined: Tue Aug 04, 2020 5:29 pm

how to convert esp_ip4_addr_t to ip_addr_t

Postby korstiaan » Sat Apr 10, 2021 4:04 pm

Hi,

I want to ping my gateway.
I get my gateway address from the event handler (IP_EVENT_STA_GOT_IP) and is of type

Code: Select all

esp_ip4_addr_t gw
.
But to use the ping I need a target of type

Code: Select all

ip_addr_t target
.

Can somebody tell me how to assing the gateway ip address from

Code: Select all

esp_ip4_addr_t gw
to

Code: Select all

ip_addr_t target
?

Code: Select all

target.u_addr = ...?
target.type = ...?
Thanks for helping.

nopnop2002
Posts: 111
Joined: Thu Oct 03, 2019 10:52 pm
Contact:

Re: how to convert esp_ip4_addr_t to ip_addr_t

Postby nopnop2002 » Mon Apr 18, 2022 5:56 am

Convert from esp_ip4_addr_t to ip_addr_t:

Code: Select all

// get current STA information
esp_netif_ip_info_t ip_info;
esp_netif_get_ip_info(esp_netif_get_handle_from_ifkey("WIFI_STA_DEF"), &ip_info);

ip_addr_t gw_addr;
memcpy((char *)&gw_addr.u_addr.ip4, (char *)&ip_info.gw, sizeof(ip4_addr));
gw_addr.type = IPADDR_TYPE_V4;
ESP_LOGI(TAG, "ip4addr_ntoa(gw_addr.u_addr.ip4)=%s", ip4addr_ntoa(&gw_addr.u_addr.ip4));

Who is online

Users browsing this forum: No registered users and 78 guests