Page 1 of 1

(solved) connecting to Wifi using static IP

Posted: Sat Sep 05, 2020 4:01 am
by mzimmers
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...

Re: conntecting to Wifi using static IP

Posted: Sat Sep 05, 2020 9:18 pm
by opcode_x64
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

Re: connecting to Wifi using static IP

Posted: Sun Sep 06, 2020 3:20 pm
by tommeyers
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

Re: connecting to Wifi using static IP

Posted: Sun Sep 06, 2020 3:23 pm
by tommeyers
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

Re: connecting to Wifi using static IP

Posted: Mon Sep 07, 2020 3:13 pm
by mzimmers
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.

Re: connecting to Wifi using static IP

Posted: Wed Sep 09, 2020 1:45 pm
by mzimmers
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.