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

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:

  • system_event_id_t enumerated value (aliased as WiFiEvent_t, as can be seen here)
  • 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.

Output of the program on the Arduino IDE serial monitor.
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.

2 Replies to “ESP32 soft AP: Getting connecting station MAC address from WiFi event”

  1. Hi! As a subscriber to your posts for over a year, for a long time I’ve felt the need to say this. Initially I subscribed after you had posted details on ESP32 Bluetooth serial communication if I remember correctly. The feature has just been added to ESP32 Arduino core at that til time so it was new and very useful.

    Since then I’ve noticed you’ve written about a lot of other useful articles but I’ve noticed a pattern of very often publishing separate posts based on something that I believe might be based on a single example code included with a library .

    This leads to the problem that a single post rarely give enough information to do anything with. It would be much more useful post less often but to cover for example handling all the different supported events in a single article. I believe it could also drive more traffic to your site in the long run term when people would use then as a referenc

    1. Hi!

      Thanks for your feedback and for being with TechTutorialsX for so long 🙂

      I follow this approach of smaller posts to avoid having too much information in the same article, which might be confusing or too long to read.

      I try to build the post in an iterative way, with know how from new posts building on top of the previous ones.

      I follow this from my own experience while looking for information in the internet.

      I tend to skip tutorials / articles that show too much information, and try to find the ones that get me to the “Hello World” as fast as possible, and then I start building my knowledge on top of that and adapt the code to what I want to do.

      But what you mention also makes total sense, I think it really depends on the preferences of each reader.

      I try to balance content to be appealing to both people that land on a post without context of the other articles, and also for people who follow the site, but maybe in some tutorials this balance has not been achieved correctly.

      Thanks again for your feedback, I’ll try to keep an eye on this in future posts 🙂

      Best regards,
      Nuno Santos

Leave a Reply