How to get MAC and IP address of devices connected to ESP8266 ?

This post explains getting MAC address and IP address of devices connected to ESP8266. Typically, the MAC address is only visible on each end of a “hop” in a packet so if you are going from some device via WiFi through the router to some other device, the MAC address you would see would be from your device and the router. The router will typically have a table of IP addresses and associated MAC addresses for those devices that it has handed out an IP address to via DHCP.

A media access control address (MAC address) of a device is a unique identifier assigned to network interfaces for communications. In this Example we will get MAC address of all connected devices to the ESP8266. ESP8266 acts as WiFi access point.

ESP8266 Code to Get MAC and IP Address of Connected Devices

This program shows IP and MAC address of connected devices to ESP8266.

/*
 * Getting MAC and IP Address of Connected devices to ESP8266 
 * with Soft AP Mode
 */

#include <ESP8266WiFi.h>

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

/* configuration  wifi */
const char *ssid = "circuits4you.com";
const char *pass = "password";

void setup() {
  delay(1000);
  Serial.begin(115200);
  Serial.println();
  Serial.print("Configuring access point...");
  
  WiFi.softAP(ssid,pass);
  
  IPAddress myIP = WiFi.softAPIP();
  
  Serial.print("AP IP address: ");
  Serial.println(myIP);
}

void loop() {
  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 Clients 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(" ");
      Serial.print(stat_info->bssid[1],HEX);Serial.print(" ");
      Serial.print(stat_info->bssid[2],HEX);Serial.print(" ");
      Serial.print(stat_info->bssid[3],HEX);Serial.print(" ");
      Serial.print(stat_info->bssid[4],HEX);Serial.print(" ");
      Serial.print(stat_info->bssid[5],HEX);Serial.print(" ");
      
      stat_info = STAILQ_NEXT(stat_info, next);
      i++;
      Serial.println();
    }
    
  delay(500);
}

Results

Open serial monitor to see the connected devices and IP, MAC address.

ESP8266 IP and MAC Address of Connected clients

To get MAC address of ESP8266 read this.

3 thoughts on “How to get MAC and IP address of devices connected to ESP8266 ?

Leave a Reply