How to change ESP8266 MAC Address

This tutorial explains how to change mac address of ESP8266? ESP8266 Arduino core does not provide an API to change ESP8266’s WiFi MAC address. While there is a WiFi.macAddress() function, it actually retrieves the current WiFi MAC address, instead of setting it. However, Espressif SDK offers an API to change the WiFi STA MAC address.

All esp8266 comes with unique address which is factory programmed. A MAC address is given to a network adapter when it is manufactured. It is hardwired or hard-coded onto your network interface card (NIC) or chip and is unique to it. Something called the ARP (Address Resolution Protocol) translates an IP address into a MAC address.

For this reason, the MAC address is sometimes referred to as a networking hardware address, the burned-in address (BIA), or the physical address. Here’s an example of a MAC address for an Ethernet NIC: 00:0a:95:9d:68:16.

In ESP8266 you can get the MAC address using simple command

Serial.println(WiFi.macAddress());

To change the MAC address you need core libraries of Espressif SDK that we can access in Arduino IDE using extern C function.

Example Program to change MAC address of ESP8266

/*
 * Circuits4you.com
 * Change MAC Address of ESP8266 in Arduino IDE
*/
#include <ESP8266WiFi.h>

//Call to ESpressif SDK
extern "C" {
  #include <user_interface.h>
}

  const char* wifiName = "circuits4you.com";
  const char* wifiPass = "your_password";
  
  uint8_t mac[6] {0xA8, 0xD9, 0xB3, 0x0D, 0xAA, 0xCE};    //MAC Address you want to set

// the setup function runs once when you press reset or power the board
void setup() {
  Serial.begin(115200);
  delay(10);
  // We start by connecting to a WiFi network
  Serial.println();
  
  Serial.print("OLD ESP8266 MAC: ");
  Serial.println(WiFi.macAddress()); //This will read MAC Address of ESP
  
  wifi_set_macaddr(0, const_cast<uint8*>(mac));   //This line changes MAC adderss of ESP8266

  Serial.print("NEW ESP8266 MAC: ");
  Serial.println(WiFi.macAddress()); //This will read MAC Address of ESP
  
  Serial.print("Connecting to ");
  Serial.println(wifiName);

  WiFi.begin(wifiName, wifiPass);

  while (WiFi.status() != WL_CONNECTED) {
    delay(500);
    Serial.print(".");
  }

  Serial.println("");
  Serial.println("WiFi connected");
  Serial.println("IP address: ");
  Serial.println(WiFi.localIP());   //You can get IP address assigned to ESP
}

// the loop function runs over and over again forever
void loop() {
}

This program prints the OLD and NEW MAC address of ESP8266 on Serial terminal, After uploading open serial monitor to see ESP8266 MAC address.

chaning esp mac address
MAC Addresss

Leave a Reply