How to set multiple static IP addresses in ESP32 Station mode?

zhong9270
Posts: 1
Joined: Tue May 21, 2024 3:28 am

How to set multiple static IP addresses in ESP32 Station mode?

Postby zhong9270 » Tue May 21, 2024 3:43 am

I can set a static IP address according to the following document.
https://docs.espressif.com/projects/esp ... station-ip

I now want to set multiple static IP addresses, do not know how to achieve?

I tried to do this below, but only the last IP will work.
  1.     esp_netif_t *esp_netif = esp_netif_create_default_wifi_sta();
  2.     if (esp_netif_dhcpc_stop(esp_netif) != ESP_OK)
  3.     {
  4.         ESP_LOGE(TAG, "dhcpc stop failed");
  5.         return;
  6.     }
  7.  
  8.     esp_netif_ip_info_t ip;
  9.  
  10.     memset(&ip, 0 , sizeof(esp_netif_ip_info_t));
  11.     ip.ip.addr = ipaddr_addr("10.0.0.111");
  12.     ip.netmask.addr = ipaddr_addr("255.255.255.0");
  13.     ip.gw.addr = ipaddr_addr("10.0.0.1");
  14.     if (esp_netif_set_ip_info(esp_netif, &ip) != ESP_OK) {
  15.         ESP_LOGE(TAG, "Failed to set ip info");
  16.         return;
  17.     }
  18.  
  19.     memset(&ip, 0 , sizeof(esp_netif_ip_info_t));
  20.     ip.ip.addr = ipaddr_addr("192.168.0.111");
  21.     ip.netmask.addr = ipaddr_addr("255.255.255.0");
  22.     ip.gw.addr = ipaddr_addr("192.168.0.1");
  23.     if (esp_netif_set_ip_info(esp_netif, &ip) != ESP_OK) {
  24.         ESP_LOGE(TAG, "Failed to set ip info");
  25.         return;
  26.     }

Who is online

Users browsing this forum: No registered users and 76 guests