ESP8266: Get MAC address

The objective of this short post is to explain how to get the MAC address of the ESP8266.


Introduction

The objective of this short post is to explain how to get the MAC address of the ESP8266.

Explaining in detail what is a MAC address is outside of the scope of this post. But, just as a quick explanation, the MAC address is a unique value associated with a network adapter [1]. So, MAC addresses are hardware addresses that uniquely identify a network adapter [1].

Taking in consideration the OSI model, MAC addressing works at a layer (layer 2) lower than IP addressing (layer 3) [1].

In terms of format, MAC addresses are 48-bit in length [2] and typically represented in hexadecimal format, with each two hexadecimal digits separated by “:”.

The first 24 bits of the MAC are the identifier number of the manufacturer and the second 24 bits are a serial number assigned by the manufacturer [2].

You can read more about MAC addresses here.

The tests were performed using a NodeMCU board, a very cheap and easy to use ESP8266 board. The board can be bought at eBay here.


The code

The code for this tutorial is very simple, since we already have a function to get the MAC of the ESP8266.

Since we only want to print the MAC, we will do all the coding on the setup function and leave the main loop empty.

First, we include the ESP8266WiFi library, which we typically use to access all the functionality needed for the ESP8266 to connect to a WiFi network. Then, in our setup function, we open the serial port, so we can print the output of our program.

To get the MAC dress of the ESP8266, we simply call the macAdress method on the WiFi global variable, which will return the MAC address in the hexadecimal format mentioned early.

You can check the full working code bellow, which also includes the empty loop function.

#include <ESP8266WiFi.h>

void setup(){

   Serial.begin(115200);
   delay(500);

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

}

void loop(){}


Testing the code

To test the code, just upload it to the ESP8266 and open the Arduino IDE console. You should get something similar to figure 1.

ESP8266 Get MAC

Figure 1 – Output of the function to get the MAC of the ESP8266.

Just to confirm what was explained in the introduction section, we can check if the first 24 bits of the MAC correspond to the manufacturer of the ESP8266, which is Espressif. You can check yours here. It should indicate Espressif, as shown i figure 2.

ESP8266 MAC vendor lookup

Figure 2 – Result of vendor lookup from the ESP8266 MAC.


References

[1] https://people.richland.edu/dkirby/141macaddress.htm

[2] https://www.iplocation.net/mac-address


Technical details

ESP8266 libraries: v2.3.0

23 thoughts on “ESP8266: Get MAC address”

  1. The major flaw with this tutorial is 8t assumes the reader knows precisely how and where the ESP8266 should be connected. The reader is left to make guesses and wild azsumptions leav8ng this tutorial of little value to those new to the world of arduino

    1. Hi,

      Sorry if you found the tutorial too hard to follow.

      I always try to make content easy to follow for beginners and keep it interesting for more advanced users, but I hope you can understand that I can’t explain every detail of the Arduino language in each post I do.

      So, I assume that the readers who are following these tutorials have some basic knowledge on programming and on the Arduino language, since trying to work with more complicated devices such as the ESP32 and WiFi may be difficult without knowing the basics.

      But this tutorial is just calling a function and printing some stuff to the serial port, so you just need to power on your ESP8266, compile and run the code.

      Can you explain where you are having problems, so I can try to help?

      Best regards,
      Nuno Santos

  2. The major flaw with this tutorial is 8t assumes the reader knows precisely how and where the ESP8266 should be connected. The reader is left to make guesses and wild azsumptions leav8ng this tutorial of little value to those new to the world of arduino

    1. Hi,
      Sorry if you found the tutorial too hard to follow.
      I always try to make content easy to follow for beginners and keep it interesting for more advanced users, but I hope you can understand that I can’t explain every detail of the Arduino language in each post I do.
      So, I assume that the readers who are following these tutorials have some basic knowledge on programming and on the Arduino language, since trying to work with more complicated devices such as the ESP32 and WiFi may be difficult without knowing the basics.
      But this tutorial is just calling a function and printing some stuff to the serial port, so you just need to power on your ESP8266, compile and run the code.
      Can you explain where you are having problems, so I can try to help?
      Best regards,
      Nuno Santos

  3. Actually randy is right.

    For example, will we upload the code to the arduino or to the ESP8266?
    How did you get the ESP8266WiFi.h library? Even if I’ve downloaded it, it says it is still missing some other libraries like “queue.h”.

    1. Hi!

      I think your confusion may be arising for using the ESP8266 connected to an Arduino board right?

      If so, let me try to clarify. Basically, the ESP866 is a microcontroller that can run independently of any Arduino board.

      So, you can program an ESP8266 pretty much like you can program an Arduino uno, due, mega, etc..

      And it happens that one of the “languages” that you can use to program the ESP8266 is the Arduino one.

      Note that the term Arduino is more vast that the boards we use. Arduino is an ecosystem composed by the boards, the language and the IDE.

      So, in short, you upload the code to the ESP8266 and you don’t need any Arduino board to follow this tutorial.

      In fact, unless clearly stated otherwise, all the ESP8266 and ESP32 tutorials I’m writing don’t need any Arduino board.

      Note that uploading the code to your ESP8266 may be easy or dificult, depending on the ESP board that you are using.

      For instance, if you are using an ESP-01, then it will be painful, since that board doesn’t have a USB interface.

      If that’s the board you are using, then I have an old tutorial that you can try:
      https://techtutorialsx.com/2016/02/28/esp8266-uploading-code-from-arduino-ide/

      Note however that it was written a lot of time ago, so I cannot confirm that the procedure is still working.

      If you are a beginner, then I would recommend you to use an ESP board with USB interface, since uploading code to those is much simpler (only need to connect a cable).

      Regarding the problems with the libraries, you need to install the Arduino core for the ESP8266. You can check that on the GitHub repository of the project:
      https://github.com/esp8266/Arduino

      It should solve your problems, unless there has been any major change to the libraries that invalidates this tutorial.

      Let me know if this helps.

      Best regards,
      Nuno Santos

  4. Actually randy is right.
    For example, will we upload the code to the arduino or to the ESP8266?
    How did you get the ESP8266WiFi.h library? Even if I’ve downloaded it, it says it is still missing some other libraries like “queue.h”.

    1. Hi!
      I think your confusion may be arising for using the ESP8266 connected to an Arduino board right?
      If so, let me try to clarify. Basically, the ESP866 is a microcontroller that can run independently of any Arduino board.
      So, you can program an ESP8266 pretty much like you can program an Arduino uno, due, mega, etc..
      And it happens that one of the “languages” that you can use to program the ESP8266 is the Arduino one.
      Note that the term Arduino is more vast that the boards we use. Arduino is an ecosystem composed by the boards, the language and the IDE.
      So, in short, you upload the code to the ESP8266 and you don’t need any Arduino board to follow this tutorial.
      In fact, unless clearly stated otherwise, all the ESP8266 and ESP32 tutorials I’m writing don’t need any Arduino board.
      Note that uploading the code to your ESP8266 may be easy or dificult, depending on the ESP board that you are using.
      For instance, if you are using an ESP-01, then it will be painful, since that board doesn’t have a USB interface.
      If that’s the board you are using, then I have an old tutorial that you can try:
      https://techtutorialsx.com/2016/02/28/esp8266-uploading-code-from-arduino-ide/
      Note however that it was written a lot of time ago, so I cannot confirm that the procedure is still working.
      If you are a beginner, then I would recommend you to use an ESP board with USB interface, since uploading code to those is much simpler (only need to connect a cable).
      Regarding the problems with the libraries, you need to install the Arduino core for the ESP8266. You can check that on the GitHub repository of the project:
      https://github.com/esp8266/Arduino
      It should solve your problems, unless there has been any major change to the libraries that invalidates this tutorial.
      Let me know if this helps.
      Best regards,
      Nuno Santos

  5. I don’t really agree with the criticism of the tutorial. It is not unreasonable to assume some basic knowledge when you are focusing on a topic such as a MAC address. Would a true beginner be that focused on the MAC address without knowing how to load libraries etc?

    1. Hi! Thanks 🙂

      That’s precisely what I assume. For most of my tutorials, I assume that the user has a previous basic knowledge of the framework used and has everything already setup.

      Describing the whole setup process and the basics of the language in every post would be impractical.

      Just as an additional note, when I started playing with the ESP8266, it was being sold at eBay as a “Arduino to Serial WiFi Converter” and the little documentation that existed was mostly in Chinese. At that time, that was really difficult to do something with the chip 🙂

      Best regards,
      Nuno Santos

  6. I don’t really agree with the criticism of the tutorial. It is not unreasonable to assume some basic knowledge when you are focusing on a topic such as a MAC address. Would a true beginner be that focused on the MAC address without knowing how to load libraries etc?

    1. Hi! Thanks 🙂
      That’s precisely what I assume. For most of my tutorials, I assume that the user has a previous basic knowledge of the framework used and has everything already setup.
      Describing the whole setup process and the basics of the language in every post would be impractical.
      Just as an additional note, when I started playing with the ESP8266, it was being sold at eBay as a “Arduino to Serial WiFi Converter” and the little documentation that existed was mostly in Chinese. At that time, that was really difficult to do something with the chip 🙂
      Best regards,
      Nuno Santos

Leave a Reply