IoT/ESP32 B.Tech/Diploma Intermediate

Get ESP32/ESP8266 MAC Address and Change It (Arduino IDE)

Learn how to get the ESP32 or ESP8266 boards MAC Address using Arduino IDE. You'll also learn how to change your board's MAC Address. MAC Address stands for Media Access Control Address

  • Suitable forB.Tech/Diploma IoT/ESP32 students
  • Build time1-2 weeks
  • LevelIntermediate
  • Available atAjvika Labs, Kakinada

About this project

This guide shows how to get the ESP32 or ESP8266 NodeMCU boards MAC Address using Arduino IDE. We also show how to change your board’s MAC Address.
What’s a MAC Address?
MAC Address stands for
M
edia
A
ccess
C
ontrol Address and it is a hardware unique identifier that identifies each device on a network.
MAC Addresses are made up of six groups of two hexadecimal digits, separated by colons, for example:
30:AE:A4:07:0D:64
MAC Addresses are assigned by manufacturers, but you can also give a custom MAC Address to your board. However, every time the board resets, it will return to its original MAC Address. So, you need to include the code to set a custom MAC Address in every sketch.
Table of Contents
Get ESP32 Mac Address
Set a Custom MAC Address for the ESP32
Get ESP8266 MAC Address
Set a Custom MAC Address for the ESP8266
Get ESP32 MAC Address
To get your ESP32 board MAC Address, upload the following code.
/*
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files.
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
*/
#include <WiFi.h>
#include <esp_wifi.h>

void readMacAddress(){
uint8_t baseMac[6];
esp_err_t ret = esp_wifi_get_mac(WIFI_IF_STA, baseMac);
if (ret == ESP_OK) {
Serial.printf("%02x:%02x:%02x:%02x:%02x:%02x\n",
baseMac[0], baseMac[1], baseMac[2],
baseMac[3], baseMac[4], baseMac[5]);
} else {
Serial.println("Failed to read MAC address");
}
}

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

WiFi.mode(WIFI_STA);
WiFi.STA.begin();

Serial.print("[DEFAULT] ESP32 Board MAC Address: ");
readMacAddress();
}

void loop(){

}
Demonstration
After uploading the code, open the Serial Monitor at a baud rate of 115200. Press the on-board RESET or EN button.
The MAC Address should be printed in the Serial Monitor as shown in the following figure.
That’s it! Now, you know how to get your ESP32 MAC Address.
Set a Custom MAC Address for the ESP32
In some applications, it might be useful to give your boards a custom MAC Address. However, as explained previously,
this doesn’t overwrite the MAC Address set by the manufacturer
. So, every time you reset the board, or upload a new code, it will get back to its default MAC Address.
Change ESP32 MAC Address (Arduino IDE)
The following code sets a custom MAC Address for the ESP32 board.
/*
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files.
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
*/
#include <WiFi.h>
#include <esp_wifi.h>

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

// Replace with your network credentials
const char* ssid = "REPLACE_WITH_YOUR_SSID";
const char* password = "REPLACE_WITH_YOUR_PASSWORD";

void readMacAddress(){
uint8_t baseMac[6];
esp_err_t ret = esp_wifi_get_mac(WIFI_IF_STA, baseMac);
if (ret == ESP_OK) {
Serial.printf("%02x:%02x:%02x:%02x:%02x:%02x\n",
baseMac[0], baseMac[1], baseMac[2],
baseMac[3], baseMac[4], baseMac[5]);
} else {
Serial.println("Failed to read MAC address");
}
}

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

WiFi.mode(WIFI_STA);
WiFi.STA.begin();

Serial.print("[DEFAULT] ESP32 Board MAC Address: ");
readMacAddress();

// Change ESP32 Mac Address
esp_err_t err = esp_wifi_set_mac(WIFI_IF_STA, &newMACAddress[0]);
if (err == ESP_OK) {
Serial.println("Success changing Mac Address");
}

// Read the new MAC address
Serial.print("[NEW] ESP32 Board MAC Address: ");
readMacAddress();

// Connect to Wi-Fi
WiFi.begin(ssid, password);
Serial.println("Connecting to WiFi...");
while (WiFi.status() != WL_CONNECTED) {
Serial.print(".");
delay(1000);
}
Serial.println("Connected");
}

void loop(){

}
You can set a custom MAC Address in the following line:
uint8_t newMACAddress[] = {0x32, 0xAE, 0xA4, 0x07, 0x0D, 0x66};
Attention:
the bit 0 of the first byte of MAC address can not be 1. For example, the MAC address can set to be “1a:XX:XX:XX:XX:XX”, but can not be “15:XX:XX:XX:XX:XX”.
After uploading the code, open the Serial Monitor at a baud rate of 115200. Restart the ESP32 and you should get its default and new MAC Address.
Get ESP8266 NodeMCU MAC Address
To get your ESP8266 board Mac Address, use the following code.
/*
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files.
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
*/
#include <ESP8266WiFi.h>

void setup(){
Serial.begin(115200);
Serial.println();
Serial.print("ESP Board MAC Address: ");
Serial.println(WiFi.macAddress());
}

void loop(){

}
Demonstration
After uploading the code, open the Serial Monitor at a baud rate of 115200. Press the on-board RESET or EN button. The MAC Address should be printed in the Serial Monitor.
Change the ESP8266 NodeMCU MAC Address (Arduino IDE)
The following code sets a custom MAC Address for the ESP8266 board.
// Complete Instructions: https://ajvikalabs.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 on 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 ESP8266 and you should get its default and new MAC Address.
Wrapping Up
In this quick guide, we’ve shown you how to get your ESP32 and ESP8266 manufacturer MAC Address with Arduino IDE. You’ve also learned how to set a custom MAC Address for your boards.
Learn more about the ESP32 and ESP8266 boards:
More ESP32 resources…
Home Automation using ESP8266 (eBook)
More ESP8266 resources…

Technologies used

ESP32ESP8266ArduinoRaspberry PiMQTTWiFiBLEArduino IDE

Hardware components

  • ESP32
  • ESP8266
  • Arduino
  • Raspberry Pi

Software & tools

  • Arduino IDE
  • Node-RED
  • MQTT

What you get

  • Working project / hardware kit, tested before delivery
  • Complete source code with explanation
  • Project demo and guidance for your viva
  • Project report, PPT and IEEE paper support (on request)
Delivery & Payment
Delivery takes a minimum of two weeks from the date your inquiry is confirmed. Payment is in two parts: • 50% advance to start the project • Remaining 50% before delivery Note: • Documentation and PPT are not included by default • If you need documentation/PPT, it can be arranged for extra time and cost We'll confirm the exact timeline and cost with you after reviewing your requirements.