(solved) connecting to Wifi using static IP

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

(solved) connecting to Wifi using static IP

Postby mzimmers » Sat Sep 05, 2020 4:01 am

Hi all -

I'm trying to get a ESP32 with a fixed IP address to connect to an access point. I'm getting an error "Host is unreachable."

My calls to esp_wifi_start and esp_wifi_connect seem to be returning successfully. Is there anything special I need to do? I didn't see anything in the wifi_init_config_t struct that I need to change, but maybe I'm missing something.

Thanks for any help...
Last edited by mzimmers on Wed Sep 09, 2020 1:46 pm, edited 2 times in total.

opcode_x64
Posts: 47
Joined: Sun Jan 13, 2019 5:39 pm

Re: conntecting to Wifi using static IP

Postby opcode_x64 » Sat Sep 05, 2020 9:18 pm

Hey,

without seeing any code:
As far as I know and understand the WiFi driver is independent from the TCP/IP layer, meaning connecting to an access point has nothing to do with TCP/IP at first. After your connection is established, then your access point is assigning your device (ESP32) an IP address if DHCP is enabled on the access point. Did you check your access point settings ? Are the "static" TCP/IP configurations such IP, Gateway etc.. are configured proper on your ESP32 and are they compliant with the access point ?


Good luck !

opcode_x64

tommeyers
Posts: 184
Joined: Tue Apr 17, 2018 1:51 pm
Location: Santiago, Dominican Republic

Re: connecting to Wifi using static IP

Postby tommeyers » Sun Sep 06, 2020 3:20 pm

I have a recollection of a wifi lig that can be activated. I tried to find that - no luck.

That log, if it exists, might give more clues.

Tom
IT Professional, Maker
Santiago, Dominican Republic

tommeyers
Posts: 184
Joined: Tue Apr 17, 2018 1:51 pm
Location: Santiago, Dominican Republic

Re: connecting to Wifi using static IP

Postby tommeyers » Sun Sep 06, 2020 3:23 pm

I have a recollection of a wifi lig that can be activated. I tried to find that - no luck.

That log, if it exists, might give more clues.

Here it is:
https://docs.espressif.com/projects/esp ... m/log.html

Tom
IT Professional, Maker
Santiago, Dominican Republic

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

Re: connecting to Wifi using static IP

Postby mzimmers » Mon Sep 07, 2020 3:13 pm

Thanks for the replies, guys. I already get wifi log messages, and there's nothing helpful in there.

I'll look at my AP more closely to see whether it restricts static IP addresses, though that seems unlikely.

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

Re: connecting to Wifi using static IP

Postby mzimmers » Wed Sep 09, 2020 1:45 pm

So, here's what was missing:

Code: Select all

tcpip_adapter_ip_info_t m_ipInfo = { {0}, {0}, {0} };

if (m_tasks->flash->get(NVS_KEY_IPSOURCE) == IP_SOURCE_TXT[IP_SOURCE_STATIC])
{
	ESP_ERROR_CHECK(tcpip_adapter_dhcpc_stop(TCPIP_ADAPTER_IF_STA));

	// extract elements, convert to string and pass to NVS.
	s.assign(m_tasks->flash->get(NVS_KEY_IPADDR));
	inet_pton(AF_INET, s.c_str(), &(m_ipInfo.ip.addr));

	s.assign(m_tasks->flash->get(NVS_KEY_IPGATEWAY));
	inet_pton(AF_INET,  s.c_str(), &(m_ipInfo.gw.addr));

	s.assign(m_tasks->flash->get(NVS_KEY_IPSUBNET));
	inet_pton(AF_INET,  s.c_str(), &(m_ipInfo.netmask.addr));

	ESP_ERROR_CHECK(tcpip_adapter_set_ip_info(TCPIP_ADAPTER_IF_STA, &m_ipInfo));
}
In short, the calls to tcpip_adapter_dhcpc_stop() and tcpip_adapter_set_ip_info() were what was missing. Note: this is a solution for esp-idf v4.0.1. V4.1 deprecates tcpip_adapter in favor of netif.

Who is online

Users browsing this forum: Baidu [Spider], Gaston1980, Google [Bot] and 118 guests