Get ESP32/ESP8266 MAC address and change it (Arduino IDE)

This guide explains how to use the Arduino IDE to obtain the MAC address of the ESP32 or ESP8266 development board. We also show how to change the MAC address of the development board.

What is a MAC address?

A MAC address stands for Media Access Control Address, which is a hardware unique identifier that identifies each device on a network.

A MAC address consists of six groups of two-digit hexadecimal digits separated by colons, for example: 32:AE:A7:04:6D:66 .

The MAC address is assigned by the manufacturer, but you can also provide a custom MAC address for your development board. However, every time the board resets, it returns to its original MAC address. Therefore, you need to include the code to set the custom MAC address in each sketch.

Get ESP32 or ESP8266 MAC address

To get your board's MAC address, simply upload the following code to the ESP32 or ESP8266. The code is compatible with both boards.

 

// Complete Instructions: https://RandomNerdTutorials.com/get-change-esp32-esp8266-mac-address-arduino/

#include <WiFi.h>
#include <esp_wifi.h>

// Set your new MAC Address
uint8_t newMACAddress[] = {0x32, 0xAE, 0xA4, 0x07, 0x0D, 0x66};

void setup(){
  Serial.begin(115200);
  Serial.println();
  
  WiFi.mode(WIFI_STA);
  
  Serial.print("[OLD] ESP32 Board MAC Address:  ");
  Serial.println(WiFi.macAddress());
  
  // ESP32 Board add-on before version < 1.0.5
  //esp_wifi_set_mac(ESP_IF_WIFI_STA, &newMACAddress[0]);
  
  // ESP32 Board add-on after version > 1.0.5
  esp_wifi_set_mac(WIFI_IF_STA, &newMACAddress[0]);
  
  Serial.print("[NEW] ESP32 Board MAC Address:  ");
  Serial.println(WiFi.macAddress());
}
 
void loop(){

}

You can set a custom MAC address in the following line:

uint8_t newMACAddress[] = {0x32, 0xAE, 0xA4, 0x07, 0x0D, 0x66};

After uploading the code, open the serial monitor at a baud rate of 115200. Restart the ESP32 and you should get its old MAC address and new MAC address.

Change ESP8266 MAC address (Arduino IDE)

The following code sets a custom MAC address for the ESP8266 development board.

// Complete Instructions: https://RandomNerdTutorials.com/get-change-esp32-esp8266-mac-address-arduino/

#include <ESP8266WiFi.h>

// Set your new MAC Address
uint8_t newMACAddress[] = {0x32, 0xAE, 0xA4, 0x07, 0x0D, 0x66};

void setup(){
  Serial.begin(115200);
  Serial.println();
  
  WiFi.mode(WIFI_STA);
  
  Serial.print("[OLD] ESP8266 Board MAC Address:  ");
  Serial.println(WiFi.macAddress());

  // For Soft Access Point (AP) Mode
  //wifi_set_macaddr(SOFTAP_IF, &newMACAddress[0]);
  // For Station Mode
  wifi_set_macaddr(STATION_IF, &newMACAddress[0]);
  
  Serial.print("[NEW] ESP8266 Board MAC Address:  ");
  Serial.println(WiFi.macAddress());
}
 
void loop(){

}

 Set your custom MAC address in the following lines:

uint8_t newMACAddress[] = {0x32, 0xAE, 0xA4, 0x07, 0x0D, 0x66};

After uploading the code, open the serial monitor at a baud rate of 115200. Reboot the ESP8266 and you should get its old MAC address and new MAC address.

Summarize

In this quick guide, we show you how to get the MAC address of the ESP32 and ESP8266 manufacturer using the Arduino IDE. You also learned how to set a custom MAC address for your development board.

 

Guess you like

Origin blog.csdn.net/hhqidi/article/details/131008135