5

I turned my esp8266 as an access point, so that the mobile devices could connect to it. Want to get the macAddress of the devices connected to it. How could I get it?

ABI
  • 1,536
  • 18
  • 38

4 Answers4

7

I got the answer from here

and it works

#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);
}
ABI
  • 1,536
  • 18
  • 38
1

If i use the code this error comes up:

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

I resolved it with, due to invalid type conversion:

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

The change of the core ESP8266 IPAdress.h from Adrian McEwen could also fix this issue.

Zykrates
  • 61
  • 4
0

The answer given by @ABI works, but it has memory leak issue.

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

Following is the sample code which does not have memory leak issue:

#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();
}

Following line can be included in loop() to monitor the free heap memory:

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

I answered a similar question here

Content:

I found an example made using ESP8266. I also made one myself for ESP32 here.

To briefly summarise the 2 examples, below is a code snippet that can retrieve the corresponding MAC address an IP in STA_MODE:

#include <lwip/etharp.h>
...
eth_addr *get_sta_mac(const uint32_t &ip)
{
    ip4_addr requestIP{ip};
    eth_addr *ret_eth_addr = nullptr;
    ip4_addr const *ret_ip_addr = nullptr;
    etharp_request(netif_default, &requestIP);
    etharp_find_addr(netif_default, &requestIP, &ret_eth_addr, &ret_ip_addr);
    return ret_eth_addr;
}
...

The above code is tested with arduino-esp32.