logo elektroda
logo elektroda
X
logo elektroda

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

miszczo997 1689 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
    khoam
    Level 42  
    miszczo997 wrote:
    The problem will arise in that I found a function that stops the DHCP server
    .
    This function does not stop the DHCP server, but the DHCP client as long as it was previously enabled.

    miszczo997 wrote:
    Apparently everything looks ok, but when you enter its body all it does is return ESP_ERR_NOT_SUPPORTED
    .
    It depends which version of this function. It is implemented in the file esp_netif_lwip.c :
    Code: C / C++
    Log in, to see the code
    .
    The code you have provided comes from the file esp_netif_loopback.c

    Example initialisation in STA mode with static address:
    Code: C / C++
    Log in, to see the code
    .

    miszczo997 wrote:
    I write the code using the latest esp-idf.
    .
    So version 4.3?
  • ADVERTISEMENT
  • Helpful post
    #3 19491853
    krzbor
    Level 27  
    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
    khoam
    Level 42  
    miszczo997 wrote:
    Btw, why does the address need to be from outside the DHCP address pool?
    .
    So that there is no potential conflict with another network device operating on the same IP subnet that would take the address via DHCP.
  • 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.
  • ADVERTISEMENT
  • #7 19492699
    khoam
    Level 42  
    ex-or wrote:
    An even better option IMHO is local DNS and using the device name.
    .
    In ESP you can run the mDNS service and broadcast your own name to the network.
  • #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
    khoam
    Level 42  
    If the mDNS service is initiated on both ESP32s, the question of how the IP address is assigned to the ESP32 is a secondary matter - it is important that it matches the IP subnet address managed by the AP router. There is also no need for a local DNS server on the IP subnet or additional configuration of the AP router. ESP32 devices can only 'detect' each other based on established names. On the other hand, detection and access to the ESP32 from a PC station can be done via Bonjour (Windows) or Zeroconf (Linux), also based on ESP32 names. It also does not matter whether the ESP32 is operating in STA mode, AP mode or both.
  • #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.
Summary generated by the language model.
ADVERTISEMENT