ESP32 Arduino: Get WiFi station interface MAC address

In this tutorial we will check how to get the MAC address of the WiFi station interface of the ESP32, using the Arduino core. The tests shown on this tutorial were performed using an ESP32 board from DFRobot.

Introduction

In this tutorial we will check how to get the MAC address of the WiFi station interface of the ESP32, using the Arduino core.

Although we don’t need to connect the ESP32 to a WiFi network before we get the MAC address, we will need to initialize the WiFi interface first. We will do this by explicitly setting the WiFi mode, which will initialize the interface under the hood.

The tests shown on this tutorial were performed using an ESP32 board from DFRobot.

If you prefer a video version of this tutorial, please check my YouTube channel below.

The code

The code for this tutorial will be very simple. We will start by including the WiFi.h library, so we have access to the WiFi extern variable, which exposes the method we need to obtain the MAC address of the station interface.

#include "WiFi.h"

Moving on to the setup function, we will start by initializing the serial interface, so we can later output the obtained MAC address.

 
Serial.begin(115200);

Then we need to call the mode method on our WiFi variable. This method receives as input a value of the enum type wifi_mode_t and sets the WiFi mode of the ESP32.

In our case, since we want the ESP32 station interface MAC, we will pass as input the value WIFI_MODE_STA.

Note that, if we analyze the implementation of the mode method, we can confirm that it will initialize the WiFi interface in case it hasn’t been done yet. So, the esp_wifi_init will be called under the hood to perform this initialization.

WiFi.mode(WIFI_MODE_STA);

Finally, to obtain the MAC address of the station interface, we simply need to call the  macAddress method of the WiFi variable. This method takes no arguments and returns the MAC as a string.

It’s important to take in consideration that, in its implementation, the macAddress method calls the esp_wifi_get_mac function. As can be seen on the documentation of the function, if it is called without a previous call to the esp_wifi_init function, then it will return an error [1].

This is why, as already mentioned, we need to call the mode method before we try to get the MAC, so the WiFi interface is previously initialized. Otherwise, we would obtain an empty string.

Serial.println(WiFi.macAddress());

The final code can be seen below.

#include "WiFi.h"

void setup(){
  Serial.begin(115200);

  WiFi.mode(WIFI_MODE_STA);

  Serial.println(WiFi.macAddress());
}

void loop(){}

Testing the code

To test the code, simply compile it and upload it to your device using the Arduino IDE. After the procedure finishes, open the serial monitor.

You should get an output similar to figure 1, which shows the MAC address of the station interface of the ESP32 getting printed.

Getting MAC address of ESP32 WiFi station interface, on the Arduino core

Figure 1 – Output of the program, with the MAC address getting printed.

References

[1] https://docs.espressif.com/projects/esp-idf/en/latest/api-reference/network/esp_wifi.html#_CPPv216esp_wifi_get_mac16wifi_interface_tA6_7uint8_t

1 thought on “ESP32 Arduino: Get WiFi station interface MAC address”

  1. Pingback: ESP32 Arduino: Get WiFi soft AP interface MAC address – techtutorialsx

  2. Pingback: ESP32 Arduino: Get WiFi soft AP interface MAC address – techtutorialsx

Leave a Reply