[Solved] Being a WiFi station but not using DHCP ...

User avatar
kolban
Posts: 1683
Joined: Mon Nov 16, 2015 4:43 pm
Location: Texas, USA

[Solved] Being a WiFi station but not using DHCP ...

Postby kolban » Sun Oct 16, 2016 2:00 am

Studying the WiFi exposed APIs in ESP-IDF, it seems that the correct path to be a station is to:
  • tcpip_adapter_init()
  • esp_wifi_init()
  • esp_wifi_set_mode()
  • esp_wifi_set_config()
  • esp_wifi_start()
  • esp_wifi_connect()
That seems to assume that my station (ESP32) is going to use the services of a DHCP provider to obtain local IP address, gateway and netmask. Looking the the tcpip_adapter APIs, I see we have an API called tcpip_adapter_start() that allows me to specify my own IP address, gateway and netmask. Perfect. However, what I don't understand if where to inject this API call in the above sequence of WiFi calls in order for the function to be honored. Note that this assumes that I am even down the right track which is still subject to discussion :-)
Last edited by kolban on Sun Oct 16, 2016 5:40 am, edited 1 time in total.
Free book on ESP32 available here: https://leanpub.com/kolban-ESP32

WiFive
Posts: 3529
Joined: Tue Dec 01, 2015 7:35 am

Re: Being a WiFi station but not using DHCP ...

Postby WiFive » Sun Oct 16, 2016 3:05 am

tcpip_adapter_init()
tcpip_adapter_dhcpc_stop(TCPIP_ADAPTER_IF_STA)
tcpip_adapter_set_ip_info(TCPIP_ADAPTER_IF_STA, &static_IP_info)

User avatar
kolban
Posts: 1683
Joined: Mon Nov 16, 2015 4:43 pm
Location: Texas, USA

Re: Being a WiFi station but not using DHCP ...

Postby kolban » Sun Oct 16, 2016 5:14 am

Many thanks, so the full recipe would be:
  • tcpip_adapter_init()
  • tcpip_adapter_dhcpc_stop(TCPIP_ADAPTER_IF_STA)
  • tcpip_adapter_set_ip_info(TCPIP_ADAPTER_IF_STA, &static_IP_info)
  • esp_wifi_init()
  • esp_wifi_set_mode()
  • esp_wifi_set_config()
  • esp_wifi_start()
  • esp_wifi_connect()
I tested and it seems to work perfectly. The code fragment I used to set the static IP info is as follows in case it is useful to someone:

Code: Select all

tcpip_adapter_ip_info_t ipInfo;
IP4_ADDR(&ipInfo.ip, 192,168,1,99);
IP4_ADDR(&ipInfo.gw, 192,168,1,1);
IP4_ADDR(&ipInfo.netmask, 255,255,255,0);
tcpip_adapter_set_ip_info(TCPIP_ADAPTER_IF_STA, &ipInfo);
That sounds great. Thank you VERY MUCH. You have awesome knowledge.
Free book on ESP32 available here: https://leanpub.com/kolban-ESP32

johto391
Posts: 2
Joined: Fri Apr 05, 2019 9:33 am

Re: [Solved] Being a WiFi station but not using DHCP ...

Postby johto391 » Fri Apr 05, 2019 9:36 am

Hi Kolban,
On which esp-idf version did you get the tcpip_adapter_dhcpc_stop to work for interface TCPIP_ADAPTER_IF_STA?
// Johan

Fredrik
Posts: 1
Joined: Fri Feb 14, 2020 9:02 pm

Re: [Solved] Being a WiFi station but not using DHCP ...

Postby Fredrik » Fri Feb 14, 2020 9:29 pm

tcpip_adapter_dhcpc_stop(TCPIP_ADAPTER_IF_STA) returns ESP_ERR_TCPIP_ADAPTER_INVALID_PARAMS for me.

I guess it works on some versions.
So is there a way to set a static ip?

Who is online

Users browsing this forum: Bing [Bot] and 117 guests