Difficulty in assigning static IPs in softAP mode
Posted: Sat Sep 23, 2023 4:24 pm
Hello everyone,
my ESP32 is operating in SoftAP mode, which obtains the IP 192.168.4.1 in the AP. DHCP is now used to assign IPs to clients. This works quite well, but unfortunately, IP assignment depends on the order in which clients connect. I want to change this and assign static IPs to various clients based on their MAC addresses.
For this purpose, I listen for the WIFI_EVENT_AP_STACONNECTED event and attempt to assign the IPs in this way:
To do this, I stop the DHCP and then restart it, i.e ( the IP 192.168.4.20 depends on the configured one by Mac):
This works as long as I set the same IP for the interface and the subnet mask; otherwise, the client has no connection or disconnects automatically. In addition, the IP of the ESP32 changes during this process. What I'm trying to achieve is that multiple clients receive static IPs (e.g., 192.168.4.20 and 192.168.4.25) while the ESP32 remains reachable under its original address (192.168.4.1). Unknown clients should still receive their IP via DHCP. Is this even possible? I couldn't find anything about this in the documentation, and all my attempts have been unsuccessful so far.
I have already tried to keep the gateway address the same:
However, it didn't work, as the client only wanted to connect briefly and then immediately disconnected. Even a reset (essentially initializing DHCP with 192.168.4.20 first and then again with 192.168.4.1) only resulted in the client always getting 192.168.4.2. My question now is, how can this be achieved? Have I forgotten something or do I need to do something else?
Thank you for your help.
Kind regards,
Danny
my ESP32 is operating in SoftAP mode, which obtains the IP 192.168.4.1 in the AP. DHCP is now used to assign IPs to clients. This works quite well, but unfortunately, IP assignment depends on the order in which clients connect. I want to change this and assign static IPs to various clients based on their MAC addresses.
For this purpose, I listen for the WIFI_EVENT_AP_STACONNECTED event and attempt to assign the IPs in this way:
Code: Select all
if (event_base == WIFI_EVENT && event_id == WIFI_EVENT_AP_STACONNECTED)
{
ESP_LOGI(TAG, "Station connected");
wifi_event_ap_staconnected_t *event = (wifi_event_ap_staconnected_t *)event_data;
handleNewClient(event->mac);
}
Code: Select all
esp_netif_ip_info_t ipInfo_ap_new;
ipInfo_ap_new.ip.addr = ipaddr_addr("192.168.4.20");
ipInfo_ap_new.gw.addr = ipaddr_addr("192.168.4.20");
ipInfo_ap_new.netmask.addr = ipaddr_addr("255.255.255.0");
ESP_ERROR_CHECK(esp_netif_dhcps_stop(wifiAP));
esp_netif_set_ip_info(wifiAP, &ipInfo_ap_new);
ESP_ERROR_CHECK(esp_netif_dhcps_start(wifiAP));
I have already tried to keep the gateway address the same:
Code: Select all
ipInfo_ap_new.ip.addr = ipaddr_addr("192.168.4.20");
ipInfo_ap_new.gw.addr = ipaddr_addr("192.168.4.1");
Thank you for your help.
Kind regards,
Danny