Obtaining and setting the MAC address of ESP32/ESP8266

First, Lingshun Lab (lingshunlab.com) introduces how to use Arduino IDE programming to obtain and set (modify) the MAC address of ESP32/ESP8266.

WiFi.macAddress()

Usage 1: WiFi.macAddress()

In obtaining the MAC address, the WiFi.macAddress() function is mainly used

In fact, the macAddress method calls the esp_wifi_get_mac function, and you can view relevant information. Before obtaining the MAC address, we do not need to connect to any WIFI.

But when checking esp_wifi_get_mac , I saw that there was an error message in this function ESP_ERR_WIFI_NOT_INIT: WiFi is not initialized by esp_wifi_init: WIFI was not initialized.

In actual applications, whether or not the following WIFI initialization code is added, the MAC address can still be obtained in ESP32.

1
WiFi.mode(WIFI_MODE_STA);  // If you can't get a MAC address, try adding this line of code for WIFI initialisation

Usage 2: WiFi.macAddress(macAddr)

This function has another usage WiFi.macAddress(macAddr), where macAddr is optional. If the parameter macAddr is input, the mac address of the ESP32/ESP8266 development board will be stored in the variable macAddr. macAddr must be an array of uint8_t type, which contains 6 elements.

Sample code:

1
2
3
uint8_t macAddr[6];
WiFi.macAddress(macAddr); // The MAC address is stored in the macAddr array.
Serial.printf("Getting a MAC address by dumping it into an array: %02x:%02x:%02x:%02x:%02x:%02x\n", macAddr[0], macAddr[1], macAddr[2], macAddr[3], macAddr[4], macAddr[5]);

Get MAC address code

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
// welcome to lingshunlab.com
// https://lingshunlab.com/book/esp32/esp32-mac-address-get-and-set

#ifdef ESP32
#include <WiFi.h>
#else
#include <ESP8266WiFi.h>
#endif

void setup(){
Serial.begin(115200); // Setting the baud rate
Serial.println(); // Serial Output Line Feed
// WiFi.mode(WIFI_MODE_STA); // If you can't get a MAC address, try adding this line of code for WIFI initialisation
Serial.print("ESP Board MAC Address: ");
Serial.println(WiFi.macAddress()); // Serial output of current ESP32/ESP8266 MAC address

uint8_t macAddr[6]; // Define macAddr as an array of type uint8_t that contains 6 elements.
WiFi.macAddress(macAddr); // The MAC address is stored in the macAddr array.
Serial.printf("Getting a MAC address by dumping it into an array: %02x:%02x:%02x:%02x:%02x:%02x\n", macAddr[0], macAddr[1], macAddr[2], macAddr[3], macAddr[4], macAddr[5]);
}

void loop(){
}

Upload the code, open the serial monitor, set the baud rate to 115200, and you can see the following information:

esp32-get-mac-address-serial-output

Modify MAC Address

The MAC address is assigned by the manufacturer. You can modify it, but it will not overwrite the original MAC address. Every time you reset the board or upload new code, it will revert to the default MAC address assigned by the manufacturer. Therefore, if you modify the MAC address, you need to add the code to modify the MAC address in each project code.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
// welcome to www.esptutorial.com
// https://lingshunlab.com/book/esp32/esp32-mac-address-get-and-set

#ifdef ESP32
#include <WiFi.h>
#include <esp_wifi.h>
#else
#include <ESP8266WiFi.h>
#endif

uint8_t newMACAddress[] = {0xAA, 0xBB, 0xCC, 0x11, 0x22, 0x33}; // Set the new MAC address, the newMACAddress variable is an array of type uint8_t

void setup(){
Serial.begin(115200); // Setting the baud rate
WiFi.mode(WIFI_STA); // Set the WIFI mode to WIFI_STA
Serial.print("defualt MAC Address:MAC Address: ");
Serial.println(WiFi.macAddress()); // Serial output of the original MAC address

// ESP32
esp_wifi_set_mac(WIFI_IF_STA, &newMACAddress[0]);

// ESP8266 END
// wifi_set_macaddr(STATION_IF, &newMACAddress[0]);

// ESP8266 END ========

Serial.print("New MAC Address:MAC Address: ");
Serial.println(WiFi.macAddress());
}

void loop(){

}

Upload the code, open the serial monitor, set the baud rate to 115200, and you can see the following information:

esp32-get-mac-address-serial-set-output

References:

https://randomnerdtutorials.com/get-change-esp32-esp8266-mac-address-arduino/
http://www.taichi-maker.com/homepage/iot-development/iot-dev-reference/esp8266-c-plus-plus-reference/esp8266wifista/macaddress/

中文版:
ESP32/ESP8266 MAC地址的获取与设定