Difficulty in assigning static IPs in softAP mode

danny89
Posts: 4
Joined: Mon Jan 31, 2022 5:01 pm

Difficulty in assigning static IPs in softAP mode

Postby danny89 » 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:

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);
    }
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):

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));
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:

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");
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

danny89
Posts: 4
Joined: Mon Jan 31, 2022 5:01 pm

Re: Difficulty in assigning static IPs in softAP mode

Postby danny89 » Mon Oct 02, 2023 7:10 am

Really nobody? Are my explanations not clear enough? Is it even possible to assign different IPs, or is it simply not feasible?

Who is online

Users browsing this forum: No registered users and 82 guests