获取esp8266连接的客户端地址Mac

Get Mac address of client connected with esp8266

我把我的esp8266变成了接入点,这样移动设备就可以连接到它了。想要获取连接到它的设备的 macAddress。我怎样才能得到它?

我得到了答案from here

有效

#include <ESP8266WiFi.h>
#include <ESP8266WebServer.h>

extern "C" {
  #include<user_interface.h>
}

/* configuration  wifi */
const char *ssid = "COblaster";

ESP8266WebServer server(80);

void handleRoot() { 
    server.send(200, "text/html", "<h1>You are connected</h1>");
    String addy = server.client().remoteIP().toString();
    Serial.println(addy);
}

void setup() {
    delay(1000);
    Serial.begin(115200);
    Serial.println();
    Serial.print("Configuring access point...");
    WiFi.softAP(ssid);
    IPAddress myIP = WiFi.softAPIP();
    Serial.print("AP IP address: ");
    Serial.println(myIP);
    server.on("/", handleRoot);
    server.begin();
    Serial.println("HTTP server started");  
}
 
void loop() {
    server.handleClient();    
    delay(5000);
    client_status();
    delay(4000);
}

void client_status() {
    unsigned char number_client;
    struct station_info *stat_info;

    struct ip_addr *IPaddress;
    IPAddress address;
    int i=1;

    number_client= wifi_softap_get_station_num();
    stat_info = wifi_softap_get_station_info();

    Serial.print(" Total connected_client are = ");
    Serial.println(number_client);

    while (stat_info != NULL) {
        IPaddress = &stat_info->ip;
        address = IPaddress->addr;

        Serial.print("client= ");

        Serial.print(i);
        Serial.print(" ip adress is = ");
        Serial.print((address));
        Serial.print(" with mac adress is = ");

        Serial.print(stat_info->bssid[0],HEX);
        Serial.print(stat_info->bssid[1],HEX);
        Serial.print(stat_info->bssid[2],HEX);
        Serial.print(stat_info->bssid[3],HEX);
        Serial.print(stat_info->bssid[4],HEX);
        Serial.print(stat_info->bssid[5],HEX);

        stat_info = STAILQ_NEXT(stat_info, next);
        i++;
        Serial.println();
    }
    delay(500);
}

如果我使用代码会出现此错误:

error: invalid conversion from 'ip4_addr*' to 'ip_addr*' [-fpermissive]
IPaddress = &stat_info->ip;

由于类型转换无效,我用以下方法解决了它:

ipv4_addr *IPaddress = &stat_info->ip;
address = IPaddress->addr;

Adrian McEwen 对核心 ESP8266 IPAdress.h 的更改也可以解决此问题。

@ABI 给出的答案有效,但存在内存泄漏问题。

wifi_softap_get_station_info() should be used with wifi_softap_free_station_info() to avoid memory leak.

以下是没有内存泄漏问题的示例代码:

#include <ESP8266WiFi.h>

void showConnectedDevices()
{
    auto client_count = wifi_softap_get_station_num();
    Serial.printf("Total devices connected = %d\n", client_count);

    auto i = 1;
    struct station_info *station_list = wifi_softap_get_station_info();
    while (station_list != NULL) {
        auto station_ip = IPAddress((&station_list->ip)->addr).toString().c_str();
        char station_mac[18] = {0};
        sprintf(station_mac, "%02X:%02X:%02X:%02X:%02X:%02X", MAC2STR(station_list->bssid));
        Serial.printf("%d. %s %s", i++, station_ip, station_mac);
        station_list = STAILQ_NEXT(station_list, next);
    }
    wifi_softap_free_station_info();
}

可以在 loop() 中包含以下行以监视空闲堆内存:

void loop()
{
    Serial.printf("Free Heap: %d Bytes\n", ESP.getFreeHeap());
    // other code
}