logo elektroda
logo elektroda
X
logo elektroda

[Solved] ESP32: Static IP address as STA, DHCP stop - how to bite?

miszczo997 3411 14
ADVERTISEMENT
Treść została przetłumaczona polish » english Zobacz oryginalną wersję tematu
  • #1 19491639
    miszczo997
    Level 28  
    Good morning!
    I have a bit of a problem with esp32. Namely, I would like to connect to the router as STA, but get a static IP address, not assigned by DHCP.
    The problem will arise in that I found a function to stop the DHCP server whose header looks like this:
    Code: C / C++
    Log in, to see the code
    .
    Seemingly everything looks ok, but when you enter its body all it does is return ESP_ERR_NOT_SUPPORTED
    Code: C / C++
    Log in, to see the code
    .
    Has anyone done something similar? How do you bite into this?
    I write the code using the latest esp-idf.
  • ADVERTISEMENT
  • Helpful post
    #2 19491700
    Anonymous
    Level 1  
  • ADVERTISEMENT
  • Helpful post
    #3 19491853
    krzbor
    Level 29  
    miszczo997 wrote:
    get a static IP address, not one assigned by DHCP
    The address is either static (that is, you assign it yourself in code) or dynamic assigned by DHCP. If you want a static address, why don't you enter it in the program code (obviously an address outside the DHCP pool) ?
  • #4 19491948
    miszczo997
    Level 28  
    khoam wrote:
    This function does not stop the DHCP server, but the DHCP client if it was previously enabled.
    .
    Yes, a mental abbreviation, of course I meant retrieving the address from the DHCP server.
    khoam wrote:
    The code you provided is from the file esp_netif_loopback.c
    .
    Exactly from that file. This is how VSCode directed me.
    khoam wrote:
    Example initialisation in STA mode with static address:
    .
    Thank you, I am getting on with the testing.
    khoam wrote:
    So version 4.3?
    .
    Here I cheated, I thought the espressif plugin for VSCode was downloading the latest version. However, I have v4.2.1.
    miszczo@DESKTOP-0D19UAR MINGW64 /c/uC/ESP32/esp-idf ((v4.2.1))
    $ git describe
    v4.2.1
    
    .
    krzbor wrote:
    If you want a static address, why don't you enter it in the program code (obviously an address outside the DHCP pool) ?
    .
    I do this, only ESP kept getting me the address assigned by DHCP anyway, even when I called the function
    esp_netif_dhcps_stop(netif); and then I typed it in manually. Btw, why does the address have to be from outside the DHCP address pool?
  • ADVERTISEMENT
  • Helpful post
    #5 19491960
    Anonymous
    Level 1  
  • ADVERTISEMENT
  • Helpful post
    #6 19492329
    ex-or
    Level 28  
    From a network management point of view, a better option is to leave the DHCP clint and configure the server to always assign the same address to the device. An even better option IMHO is local DNS and using the device name. Not all fucked up routers have this. This is by the way.
  • #7 19492699
    Anonymous
    Level 1  
  • #8 19493484
    miszczo997
    Level 28  
    khoam wrote:
    To avoid a potential conflict with another network device running on the same IP subnet that would take an address via DHCP.
    Right, I hadn't thought of that.
    ex-or wrote:
    From a network management point of view, a better option is to leave the DHCP clint and configure the server to always assign the same address to the device.
    That's for sure, worse as there is no access to the router configuration. Here I want to use two ESP32s that will connect to each other through the router and transmit audio. I'm not connecting them directly to each other because I want to be able to access their configuration from the network via an http server(I want to be able to configure them from the PC via eth cable).
    miszczo997 wrote:
    Thank you, I'm getting on with testing.
    The example given by khoam works as expected.

    Thank you all for your help. I leave the topic open for a few days should anyone have anything else to add.
    Greetings
  • #9 19493518
    Anonymous
    Level 1  
  • #10 19493696
    miszczo997
    Level 28  
    I'm assuming the scenario that the only data I get when connecting to the router is the bssid and network password.
  • #11 19493709
    Anonymous
    Level 1  
  • #12 19493865
    Anonymous
    Level 1  
  • #13 19493944
    miszczo997
    Level 28  
    Erbit wrote:
    Since you only get the SSID and password, how will you know what the address is and what address to assign, what mask and what gateway ?
    I will check with a computer connected to the same network. I just don't know about the address pool, but if DHCP assigns a specific address with a given mask, isn't it enough to just assign an address for the ESP from outside the address pool defined by the mask? Here, I'm not sure what will happen if there are several independent subnets defined in the router - most likely it will all get out of whack anyway.
    khoam wrote:
    If an mDNS service is initiated on both ESP32s
    .
    This will probably be the best approach though. I need to look into this more thoroughly as I was not previously aware of such a service.
  • #14 19494161
    Anonymous
    Level 1  
  • #15 20015014
    miszczo997
    Level 28  
    Final working wifi startup procedure based on the instructions given by @khoam
    Code: C / C++
    Log in, to see the code
    .

Topic summary

✨ The discussion revolves around configuring an ESP32 to connect to a router in Station (STA) mode while using a static IP address instead of one assigned by DHCP. The user encountered issues with the function `esp_netif_dhcpc_stop`, which is intended to stop the DHCP client but returned an error indicating it is not supported. Responses provided insights on initializing the ESP32 with a static IP, emphasizing the importance of selecting an IP address outside the DHCP pool to avoid conflicts. Suggestions included using mDNS for device discovery and considering DHCP reservation on the router for a more stable configuration. The final working code example was shared, demonstrating the correct setup for static IP assignment based on hardware detection.

FAQ

TL;DR: Static IP on ESP32 STA takes 3 steps; "This function does not stop the DHCP server, but the DHCP client." Use esp_netif_dhcpc_stop, set IP/GW/mask, then connect. [Elektroda, khoam, post #19491700]

Why it matters: For ESP-IDF developers who need reliable static IP on ESP32 STA without touching router settings.

Quick Facts

How do I set a static IP for ESP32 in STA mode (ESP-IDF)?

Do three things. 1) Create the default STA netif and call esp_netif_dhcpc_stop(my_sta). 2) Fill esp_netif_ip_info_t with IP, gateway, and netmask, then call esp_netif_set_ip_info(). 3) Initialize Wi‑Fi, set STA mode and credentials, start, and wait for connection events. The sample shows IP4_ADDR usage and event handling. You must set three fields: ip, gw, netmask. This prevents DHCP from overwriting your static configuration. [Elektroda, khoam, post #19491700]

Which ESP‑IDF API actually stops DHCP in STA mode?

Call esp_netif_dhcpc_stop() on the STA netif. That stops the DHCP client. If you peeked into esp_netif_loopback.c, you saw a stub that returns ESP_ERR_NOT_SUPPORTED. The real implementation is used via the esp_netif_lwip.c path. "The code you provided comes from the file esp_netif_loopback.c." [Elektroda, khoam, post #19491700]

Why must my static IP be outside the DHCP pool?

To avoid address conflicts with devices that receive leases from the router. If your static IP sits inside the pool, the DHCP server can hand out the same address to someone else. "So that there is no potential conflict with another network device operating on the same IP subnet." [Elektroda, khoam, post #19491960]

Can I set a static IP if I only know SSID and password?

Avoid doing that. Without the subnet mask and gateway, you risk misconfiguration and collisions. Use DHCP if you lack full network parameters. "If you only get the SSID and password you can then NOT give a static IP... Sooner or later there will be an IP conflict." [Elektroda, Anonymous, post #19493709]

How can two ESP32s find each other without hardcoding IPs?

Use mDNS. Each ESP32 advertises a hostname, so peers can resolve names without static IPs or DNS on the router. This also works regardless of STA or AP mode. "In ESP you can run the mDNS service and broadcast your own name to the network." [Elektroda, khoam, post #19492699]

How do I access ESP32 by name from a PC?

Enable mDNS on the ESP32 nodes. Then resolve them using Bonjour on Windows or Zeroconf on Linux. Name-based discovery removes the need to manage static addresses. The approach works when both ESP32s announce consistent mDNS names on the same subnet. [Elektroda, khoam, post #19493865]

How do I convert string IPs into esp_netif_ip_info_t fields?

Use esp_netif_str_to_ip4() to parse dotted-quad strings into ip4_addr_t, then assign to ip_info.ip, ip_info.gw, and ip_info.netmask. After populating fields, call esp_netif_set_ip_info() on your STA netif. The posted working code shows converting all three strings before starting Wi‑Fi. [Elektroda, miszczo997, post #20015014]

Why did esp_netif_dhcpc_stop() return ESP_ERR_NOT_SUPPORTED in my code search?

You likely opened the loopback stub implementation, which returns ESP_ERR_NOT_SUPPORTED. The actual DHCP client stop path is invoked through esp_netif_lwip.c. Use the esp_netif_create_default_wifi_sta() netif, then call esp_netif_dhcpc_stop(my_sta). [Elektroda, khoam, post #19491700]

Should I disable Wi‑Fi power save for low-latency streaming?

The final working setup disables power save with esp_wifi_set_ps(WIFI_PS_NONE). This favors responsiveness over energy savings during audio transport. Apply it after configuring STA and before starting streaming tasks. The rest of the static IP process remains unchanged. [Elektroda, miszczo997, post #20015014]

How do I confirm the ESP32 actually uses my static IP?

Register for WIFI_EVENT and IP_EVENT_STA_GOT_IP, then log the assigned info. The example waits on WIFI_CONNECTED_BIT or a failure bit. On success, print the SSID and verify interface details. This confirms your static configuration is active after the STA connects. [Elektroda, khoam, post #19491700]

Do you have a complete working example to copy?

Yes. The thread includes a final wifi_init() that stops the DHCP client, parses strings to ip_info, sets IP/gw/netmask, and starts STA mode. It also disables power save and uses event bits for connection confirmation. Use it as a template for your project. [Elektroda, miszczo997, post #20015014]

Which ESP‑IDF version did the OP use when testing?

They reported using ESP‑IDF v4.2.1 during investigation. The approach still worked after adjusting to the correct DHCP client stop function and setting ip_info before starting Wi‑Fi. [Elektroda, miszczo997, post #19491948]

What if my router uses multiple subnets or VLANs?

Manual static IP can fail if you pick the wrong subnet or gateway. Use DHCP or coordinate addressing with the network admin. The OP noted multiple subnets can cause confusion and break connectivity. mDNS avoids hardcoding addresses in such environments. [Elektroda, miszczo997, post #19493944]

Is a DHCP reservation better than hardcoding a static IP on the ESP32?

Yes, when you can manage the router. Keep the DHCP client enabled and assign a permanent lease to the device’s MAC. A local DNS or mDNS setup improves maintainability even further. This simplifies fleet management and avoids conflicts. [Elektroda, ex-or, post #19492329]
ADVERTISEMENT