Get IP address of DHCP clients in ESP32

I am using ESP32 as AP and STA and trying to get the list of DHCP clients.
Tried this code, ESP32 Arduino Soft AP: Obtaining IP address of connected stations - techtutorialsx . Pasted below

#include <WiFi.h>
#include "esp_wifi.h"
 
void setup() {
 
  Serial.begin(115200);
 
  WiFi.softAP("MyESP32AP");
 
}
 
void loop() {
 
  wifi_sta_list_t wifi_sta_list;
  tcpip_adapter_sta_list_t adapter_sta_list;
 
  memset(&wifi_sta_list, 0, sizeof(wifi_sta_list));
  memset(&adapter_sta_list, 0, sizeof(adapter_sta_list));
 
  esp_wifi_ap_get_sta_list(&wifi_sta_list);
  tcpip_adapter_get_sta_list(&wifi_sta_list, &adapter_sta_list);
 
  for (int i = 0; i < adapter_sta_list.num; i++) {
 
    tcpip_adapter_sta_info_t station = adapter_sta_list.sta[i];
 
    Serial.print("station nr ");
    Serial.println(i);
 
    Serial.print("MAC: ");
 
    for(int i = 0; i< 6; i++){
      
      Serial.printf("%02X", station.mac[i]);  
      if(i<5)Serial.print(":");
    }
 
    Serial.print("\nIP: ");  
    Serial.println(ip4addr_ntoa(&(station.ip)));    
  }
 
  Serial.println("-----------");
  delay(5000);
}

but got compilation errors

p.arduinoIDE-unsaved2024511-12196-ylz55i.3mje\sketch_jun11a\sketch_jun11a.ino: In function 'void loop()':
C:\Users\Rosario\AppData\Local\Temp.arduinoIDE-unsaved2024511-12196-ylz55i.3mje\sketch_jun11a\sketch_jun11a.ino:37:3: error: 'tcpip_adapter_sta_list_t' was not declared in this scope
37 | tcpip_adapter_sta_list_t adapter_sta_list;
| ^~~~~~~~~~~~~~~~~~~~~~~~
C:\Users\Rosario\AppData\Local\Temp.arduinoIDE-unsaved2024511-12196-ylz55i.3mje\sketch_jun11a\sketch_jun11a.ino:40:11: error: 'adapter_sta_list' was not declared in this scope
40 | memset(&adapter_sta_list, 0, sizeof(adapter_sta_list));
| ^~~~~~~~~~~~~~~~
C:\Users\Rosario\AppData\Local\Temp.arduinoIDE-unsaved2024511-12196-ylz55i.3mje\sketch_jun11a\sketch_jun11a.ino:43:3: error: 'tcpip_adapter_get_sta_list' was not declared in this scope
43 | tcpip_adapter_get_sta_list(&wifi_sta_list, &adapter_sta_list);
| ^~~~~~~~~~~~~~~~~~~~~~~~~~
C:\Users\Rosario\AppData\Local\Temp.arduinoIDE-unsaved2024511-12196-ylz55i.3mje\sketch_jun11a\sketch_jun11a.ino:47:5: error: 'tcpip_adapter_sta_info_t' was not declared in this scope
47 | tcpip_adapter_sta_info_t station = adapter_sta_list.sta[i];
| ^~~~~~~~~~~~~~~~~~~~~~~~
C:\Users\Rosario\AppData\Local\Temp.arduinoIDE-unsaved2024511-12196-ylz55i.3mje\sketch_jun11a\sketch_jun11a.ino:56:29: error: 'station' was not declared in this scope; did you mean 'static'?
56 | Serial.printf("%02X", station.mac[i]);
| ^~~~~~~
| static
C:\Users\Rosario\AppData\Local\Temp.arduinoIDE-unsaved2024511-12196-ylz55i.3mje\sketch_jun11a\sketch_jun11a.ino:61:35: error: 'station' was not declared in this scope; did you mean 'static'?
61 | Serial.println(ip4addr_ntoa(&(station.ip)));
| ^~~~~~~
| static

Any feedback?

I've started getting the same compile-time error message after migrating from IDF 4.x to IDF 5.x. This is the root cause of the problem ( tcpip_adapter_sta_list_t is no longer supported). Now you can get more information from wifi_sta_list, but not the IP address. I tried several different solutions from the internet but none really worked for me. Now I'm reading IP addresses from the ARP table but I'm not fully satisfied with this solution since ARP entries only last for 20 minutes ...

Please, let me know if you figure something out.

Found it in the examples provided in the Arduino IDE . Go to to Wifi and then events. This is a minimum code to get the ipp add of clients

#include <WiFi.h>

void setup() {
Serial.begin(115200);
  // delete old config
WiFi.disconnect(true);
delay(1000);
WiFi.onEvent(WiFiStationConnected, WiFiEvent_t::ARDUINO_EVENT_WIFI_AP_STAIPASSIGNED);
WiFi.softAP("mywireless");

}

void loop() {
  delay(1000);

}
void WiFiStationConnected(WiFiEvent_t event, WiFiEventInfo_t info){
  Serial.println("Station connected IP Add = ");
  Serial.println(IPAddress(info.got_ip.ip_info.ip.addr)); 
  Serial.println();
}

And this is what you see

21:23:45.406 -> Station connected IP Add = 
21:23:45.406 -> 192.168.4.2
21:23:45.406 -> 
21:24:33.151 -> Station connected IP Add = 
21:24:33.151 -> 192.168.4.3
21:24:33.230 -> 
 

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.