0

$USD

$USD
TUTORIALS ESP32

ESP32 soft AP: Getting connecting station MAC address from WiFi event

DFRobot Dec 16 2019 2851

In this tutorial we will check how to setup a soft AP on the ESP32 and handle the “station connected” event by printing the MAC address of the connecting device. The tests shown here were performed using an ESP32 board from DFRobot.


Introduction

In this tutorial we will check how to setup a soft AP on the ESP32 and handle the “station connected” event by printing the MAC address of the connecting device.

As we already covered here, the event handling functions of some WiFi events receive a data structure with additional information regarding the event.

In the case of the event we will be catching (SYSTEM_EVENT_AP_STACONNECTED), the MAC address can be obtained from this data structure, as we will be seeing below.

The tests shown here were performed using an ESP32 board from DFRobot.


The code

As usual, we start our code by including the WiFi.h library.

#include <WiFi.h>

Then we will move on to the Arduino setup function, where we will start by opening a serial connection.

Serial.begin(115200);
After this, we will setup the ESP32 to work in soft AP mode. We do this by calling the softAP method on the WiFi extern variable, passing as input the name (SSID) we want to assign to the network.

Note that we could optionally pass as second argument a password for the network. Nonetheless, for this simple example, we don’t need it.

WiFi.softAP("MyESP32AP");

After that, we will register a function called WiFiStationConnected to handle the event triggered when a station connects to the soft AP. Recall from the previous tutorial that the identifier of this event is called SYSTEM_EVENT_AP_STACONNECTED.

WiFi.onEvent(WiFiStationConnected, SYSTEM_EVENT_AP_STACONNECTED);

The full setup function can be seen below.

void setup() {
  
  Serial.begin(115200);
   
  WiFi.softAP("MyESP32AP");
 
  WiFi.onEvent(WiFiStationConnected, SYSTEM_EVENT_AP_STACONNECTED);
 
}

Finally, we will take care of defining the event handling function. Recall from this tutorial that the handling function needs to return void and receive the following two parameters:

A system_event_id_t enumerated value (aliased as WiFiEvent_t, as can be seen here)

A system_event_info_t union (aliased as WiFiEventInfo_t, as can be seen here)

The MAC address of the station that connected to the soft AP is contained in the system_event_info_t  union.

WiFiStationConnected(WiFiEvent_t event, WiFiEventInfo_t info){
  // function implementation
}

In our case, we need to access the union member called sta_connected, which is a struct of type system_event_ap_stadisconnected_t.

This struct contains a member called mac, which is an array of 6 bytes containing the MAC address of the station.

So, we will simply iterate through all the elements of the mac array and print them in hexadecimal format, separated by colons (the typical format for representing MAC addresses).

for(int i = 0; i< 6; i++){
     
    Serial.printf("%02X", info.sta_connected.mac[i]);  
    if(i<5)Serial.print(":");
}

The complete handling function code can be seen below. Note that we have added some additional prints.

void WiFiStationConnected(WiFiEvent_t event, WiFiEventInfo_t info){
   
  Serial.println("Station connected");
   
  for(int i = 0; i< 6; i++){
     
    Serial.printf("%02X", info.sta_connected.mac[i]);  
    if(i<5)Serial.print(":");
  }
 
  Serial.println();
}
The final code can be seen below.

#include <WiFi.h>
 
void WiFiStationConnected(WiFiEvent_t event, WiFiEventInfo_t info){
   
  Serial.println("Station connected");
   
  for(int i = 0; i< 6; i++){
     
    Serial.printf("%02X", info.sta_connected.mac[i]);  
    if(i<5)Serial.print(":");
  }
 
  Serial.println();
}
  
void setup() {
  
  Serial.begin(115200);
   
  WiFi.softAP("MyESP32AP");
 
  WiFi.onEvent(WiFiStationConnected, SYSTEM_EVENT_AP_STACONNECTED);
 
}
  
void loop() {}


Testing the code

To test the code, compile it and upload it to your device, using the Arduino IDE. After this finishes, wait for the network to be setup. You should be able to see it from any WiFi enabled device.

After the network is available, open the Arduino IDE serial monitor and then connect to the network with some WiFi enabled device. You should get an output similar to figure 1, which shows the MAC address of the station getting printed.

If you used a windows machine as station and want to confirm if the MAC address printed matches the one from the device, you can check here how to obtain it using the command line.

Note: At the time of writing, there’s an issue on the Arduino core that makes the additional debug messages seen in figure 1 getting printed when a station connects. You can track the progress of the issue here.


Figure 1 – Output of the program on the Arduino IDE serial monitor. The actual bytes of the device used are hidden due to privacy reasons.