ESP32 Arduino: Get WiFi soft AP interface MAC address

The objective of this tutorial is to explain how to obtain the MAC address of the soft AP interface of the ESP32, using the Arduino core. The tests shown on this tutorial were performed using an ESP32 board from DFRobot.

Introduction

The objective of this tutorial is to explain how to obtain the MAC address of the soft AP interface of the ESP32, using the Arduino core.

Note that, on this previous tutorial, we already checked how to obtain the MAC address for the WiFi station interface. The code we will see here will be very similar.

As covered on the mentioned tutorial, before we try to obtain the MAC address of the WiFi interfaces of the device, we first need to initialize them. We will do this by setting the WiFi mode, since this procedure will implicitly initialize the interface.

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

The code

We will start the code by including the WiFi.h library, so we can have access to the WiFi extern variable. We will use this variable to set the operating mode and to obtain the MAC address of the soft AP interface.

#include "WiFi.h"

Moving to the setup function, we will first perform the initialization of the serial interface, so we can output the result of our program.

 
Serial.begin(115200);

Then, we will set the WiFi mode of our device by calling the mode method on the WiFi extern variable. As already explained in more detail on the previous tutorial, this method call will initialize the WiFi interface under the hood, which is a pre-requisite before we obtain the MAC address of the soft AP interface.

This function receives as argument an enumerated value of type wifi_mode_t, representing the WiFi mode we want to set. In our case, we will use the value WIFI_MODE_AP, in order to set the WiFi mode to soft AP.

WiFi.mode(WIFI_MODE_AP);

After this, we simply need to call the softAPmacAddress method of the WiFi variable to obtain the MAC address of the soft AP WiFi interface.

This method takes no arguments and returns a string with the mentioned MAC address. Thus, we can directly print the result of this method call to the serial port.

Serial.println(WiFi.softAPmacAddress());

The final source code can be seen below.

#include "WiFi.h"

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

  WiFi.mode(WIFI_MODE_AP);

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

void loop() {}

Testing the code

To test the code, simply compile it and upload it to your device using the Arduino IDE. Once the procedure finishes, open the IDE serial monitor. You should get an output similar to figure 1, which shows the soft AP MAC address getting printed.

esp32 arduino get wifi soft ap interface mac address

Figure 1 – Output of the program.

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s