Introduction to ESP32 Access Point
The ESP32 is a microcontroller that is commonly used in Internet of Things (IoT) projects due to its versatility and low cost. ESP32 includes different features such as the ability to operate in access point (AP) mode. In this mode, the ESP32 acts as a wireless access point, allowing other devices to connect to it and access the internet.
It can be configured to create a secure wireless network, allowing devices to communicate with each other and the internet without the need for a physical connection. The ESP32 AP mode is useful for creating a local network for IoT devices, allowing them to communicate with each other and a central hub or server. It can be easily configured using the Arduino Integrated Development Environment (IDE).
For more detailed descriptive tutorial on ESP32 different WiFi modes see the tutorials:
- How to Set an ESP32 Access Point (AP) Using Arduino IDE
- Scan Wi-Fi Networks in ESP32 Using Arduino IDE
- How to Connect ESP32 with WiFi Using Arduino IDE
What Is a MAC Address?
Media Access Control or MAC address is a unique number assigned to devices inside a network, such as computers, routers, and printers. It is a physical address that consists of a series of numbers and letters that helps devices to communicate inside a network.
MAC addresses are essential for networking and are used in the networking protocols of the internet, such as TCP/IP. Every device inside a network has its own MAC addresses that distinguish it from other devices in the same network. MAC addresses are generally assigned by device manufacturer and stored inside the device hardware which makes it difficult to change permanently. MAC addresses are an important part of device security over the internet.
Obtaining MAC Address of Stations Connected to ESP32
Now we will connect some devices with ESP32 access points and obtain the MAC address of those devices. For that we need to configure ESP32 in access point mode first.
Code
Open Arduino IDE and upload given code in ESP32 board.
#include "esp_wifi.h"
const char *ssid = "ESP32AP"; /*SSID of ESP32 Access point*/
const char *password = "123456789"; /*password for Access point*/
void PrintStations()
{
wifi_sta_list_t stationList; /*Number of connected stations*/
esp_wifi_ap_get_sta_list(&stationList);
Serial.print("N of connected stations: ");
Serial.println(stationList.num);
for(int i = 0; i < stationList.num; i++) {
wifi_sta_info_t station = stationList.sta[i];
for(int j = 0; j< 6; j++){
char str[3];
sprintf(str, "%02x", (int)station.mac[j]); /*prints MAC address of connected station*/
Serial.print(str);
if(j<5){
Serial.print(":");
}
}
Serial.println();
}
Serial.println("-----------------");
}
void setup() {
Serial.begin(115200);
WiFi.softAP(ssid, password); /*initialize ESP32 Access point*/
Serial.println();
Serial.print("IP address: ");
Serial.println(WiFi.softAPIP()); /*prints Access point IP address*/
}
void loop() {
PrintStations(); /*prints number of station*/
delay(5000); /*wait 5 sec to check for new stations MAC address*/
}
Code started by including the necessary WiFi libraries. After that we defined the SSID and password for the ESP32 access point. Here any of the desired SSID and password can be set.
Next PrintStations() function is initialized inside which ESP32 will calculate the available connected station.
After that in the setup part ESP32 access point is configured. Once the ESP32 is configured in access point mode we can print the ESP32 IP address of the Arduino serial monitor. After printing the soft IP address of ESP32, the board will continuously check for the available connected devices or stations every 5 sec.
After any device is connected to the ESP32 access point, the Arduino IDE will print the MAC address on the serial monitor.
Connecting Device to ESP32 Access Point
Open WiFi settings on your smartphone or any other device and look for the ESP32 access point with the SSID we defined inside the code. Here we can see the ESP32AP network in WiFi settings:
Write password for ESP32AP and click Connect:
Output
After the device is connected to the ESP32 access point we can see the MAC address of the device on the serial monitor of the Arduino IDE. It shows only 1 device connected to the ESP32 access point:
We have successfully obtained the MAC address of a connected station device.
Conclusion
Here in this article, we covered the code which configures ESP32 in access point mode and prints the MAC address of devices connected to it. Using the given code any device MAC address can be obtained easily. For more detail read the given article.