Skip to main content
Visitor II
May 31, 2023
Question

Ethernet DHCP and static IP mode

  • May 31, 2023
  • 3 replies
  • 3134 views

Hi all

Currently, I am working on a project in which I have to design an application in such a way that it should support DHCP and static IP mode based on user choice. And this would be in FREERTOS. Users can back and forth between static and DHCP modes. Should it be possible?

If possible please suggest me a way.

@Piranha​  could you please help me?

    This topic has been closed for replies.

    3 replies

    Super User
    June 2, 2023

    This is not an STM32-specific issue; you need to refer to documentation to whatever TCP/IP stack you intend to use. Basically, you would need to stop/restart the link layer, whener you want to change the IP number; and you would need to run a DHCP client (which supposedly starts the link layer for you when it acquires an IP lease).

    Don't expect this to be something simply clickable in CubeMX. You need to read the documentation and act accordingly.

    JW

    spate.2Author
    Visitor II
    June 2, 2023

    Following the things I did and it works for me.

    1. Start FW in DHCP mode.
    2. When the user switches to static mode follow code I run.
    ip4_addr_t ipaddr;
     ip4_addr_t netmask;
     ip4_addr_t gw;
     // convert IP,subnet mast and gateway
     IP4_ADDR(&ipaddr, ipData->ip[0], ipData->ip[1], ipData->ip[2], ipData->ip[3]);
     
     IP4_ADDR(&netmask, ipData->netMask[0], ipData->netMask[1] , ipData->netMask[2], ipData->netMask[3]);
     
     IP4_ADDR(&gw,ipData->getway[0], ipData->getway[1], ipData->getway[2], ipData->getway[3]);
     
     netif_set_addr(&gnetif, &ipaddr, &netmask, &gw);

    I took inspiration from this ttps://community.st.com/s/question/0D53W00000zHH9PSAW/changing-ip-address-mac-address-during-runtimee

    Super User
    June 2, 2023

    As I've said, you'd need to restart the link layer, and essentially the whole stack. There's more in it than just setting the IP addresses.

    For non-STM32-specific lwip questions such as this, you may try the lwip-users mailing list.

    JW