Changing your MAC address

Every ESP8266 device has a permanent and unique MAC address for the WiFi connection. This is the address that is used within your LAN to communicate with other devices in the same network and all MAC addresses in the same LAN must be unique. If you find that you have a duplicate address, or wish to change it for some other reason, you can change it programmatically in your sketch. The Espressif SDK provides an API to do this. Incidentally, the WiFi.macAddress() function only retrieves the current MAC and cannot be used to set it.

Step 1

You need to have the header file user_interface.h on your PC. To check whether it exists, you can either search for it or try to compile a sketch that includes it and look for an error message. If you do not have the file, you can download it from Github.

Step 2

In your ESP8266 sketch, add the following code. You need to wrap the #include because the SDK is written in C whereas the ESP8266 code is in C++.

extern "C"
{
#include <user_interface.h>
}

Step 3

Add the following two statements to your sketch. The first defines the variable to hold the MAC address and sets its values: replace the hexadecimal values with the ones that you wish to use. You should avoid using random values for the first three bytes because these comprise the "vendor" ID and you could cause problems later if a new device connects to your network. The second calls the function to set the new MAC address. The change of MAC is not stored permanently by the ESP8266 and you will need to call the function every time.

uint8_t MAC[6]={0x5c,0xcf,0x7f,0x03,0xae,0xfe};
wifi_set_macaddr(STATION_IF, MAC);

Note that you need to set the new MAC address before initiating the WiFi connection using WiFi.begin().