Home / References / ESP32 Library / WiFi API / Station
Description
The macAddress
method in the ESP32 WiFi library is used to retrieve the MAC (Media Access Control) address of the ESP32. The MAC address is a unique hardware address assigned to the network interface of the ESP32.
Syntax and Usage
The macAddress
method can be used in the following ways:
– Without Arguments:
WiFi.macAddress()
Call the method without arguments to retrieve the MAC address of the station interface (used for client connections).
– With Arguments:
WiFi.macAddress(macBuffer)
Call the method with a buffer argument to store the MAC address as a byte array. This can be useful for advanced applications requiring raw MAC data.
Argument(s)
The macAddress
method accepts the following argument:
- macBuffer: (Optional, array of 6 bytes) A buffer to store the MAC address in raw byte format. If provided, the MAC address is written to this buffer. If omitted, the method returns the MAC address as a string.
Return Value
The macAddress
method returns the MAC address in one of the following formats:
- String: If no argument is provided, the method returns the MAC address as a string in the format
XX:XX:XX:XX:XX:XX
. - void: If the
macBuffer
argument is provided, the method does not return a value but writes the MAC address to the buffer as raw bytes.
Example Codes
Below are examples demonstrating how to use the macAddress
method:
Example 1: Retrieve MAC Address as a String
This code demonstrates how to use the ESP32 WiFi Library’s macAddress()
method to retrieve and display the MAC address of the ESP32’s WiFi station interface. After connecting to a WiFi network using WiFi.begin()
, the code fetches the MAC address using WiFi.macAddress()
and prints it to the Serial Monitor. This is useful for identifying the unique hardware address of the ESP32 on the network.
To use this code, replace “your_SSID” and “your_PASSWORD” with your WiFi network’s SSID and password. Upload the code to your ESP32 board and open the Serial Monitor at 115200 baud. The code will connect to the WiFi network and display the MAC address of the ESP32. Ensure the device is within range of the WiFi signal for a successful connection.
/*
* Author: Avant Maker
* Date: February 8, 2025
* Version: 1.0
* Description: This code demonstrates how to retrieve the MAC address
* of the ESP32's WiFi interface using the ESP32 WiFi library's WiFi.macAddress().
*
* License: MIT
*
* Code Source: This example code is sourced from the Comprehensive Guide
* to the ESP32 Arduino Core Library, accessible on AvantMaker.com.
* For additional code examples and in-depth documentation related to
* the ESP32 Arduino Core Library, please visit:
*
* https://avantmaker.com/references/esp32-arduino-core-index/
*
* AvantMaker.com, your premier destination for all things DIY, AI, IoT,
* Smart Home, and STEM projects. We are dedicated to empowering makers,
* learners, and enthusiasts with the resources they need to bring their
* innovative ideas to life.
*/
#include <WiFi.h>
const char* ssid = "your_SSID";
const char* password = "your_PASSWORD";
void setup() {
Serial.begin(115200);
WiFi.begin(ssid, password);
Serial.println("Connecting to WiFi...");
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
}
Serial.println("\nConnected to WiFi!");
// Print the station MAC address
Serial.print("Station MAC Address: ");
Serial.println(WiFi.macAddress());
}
void loop() {
// Your code here
}
Example 2: Retrieve MAC Address as Raw Bytes
This code demonstrates how to use the ESP32 WiFi Library’s macAddress()
method to retrieve the MAC address of the ESP32’s WiFi station interface in raw byte format. After connecting to a WiFi network using WiFi.begin()
, the code fetches the MAC address as six raw bytes using WiFi.macAddress(mac)
and prints it in a formatted way. This is useful for obtaining the unique hardware identifier of the ESP32 in a customizable format.
To use this code, replace “your_SSID” and “your_PASSWORD” with your WiFi network’s SSID and password. Upload the code to your ESP32 board and open the Serial Monitor at 115200 baud. The code will connect to the WiFi network and display the MAC address in raw format (e.g., “A8:03:2A:4F:1B:7C”). Ensure the ESP32 is within range of the WiFi signal for a successful connection.
/*
* Author: Avant Maker
* Date: February 8, 2025
* Version: 1.0
* Description: This code demonstrates how to connect an ESP32 to
* a WiFi network and retrieve the MAC address of the ESP32 with the *
* ESP32 WiFi library's WiFi.macAddress method.
*
* License: MIT
*
* Code Source: This example code is sourced from the Comprehensive Guide
* to the ESP32 Arduino Core Library, accessible on AvantMaker.com.
* For additional code examples and in-depth documentation related to
* the ESP32 Arduino Core Library, please visit:
*
* https://avantmaker.com/references/esp32-arduino-core-index/
*
* AvantMaker.com, your premier destination for all things DIY, AI, IoT,
* Smart Home, and STEM projects. We are dedicated to empowering makers,
* learners, and enthusiasts with the resources they need to bring their
* innovative ideas to life.
*/
#include <WiFi.h>
const char* ssid = "your_SSID";
const char* password = "your_PASSWORD";
void setup() {
Serial.begin(115200);
WiFi.begin(ssid, password);
Serial.println("Connecting to WiFi...");
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
}
Serial.println("\nConnected to WiFi!");
// Create a buffer to store the MAC address
uint8_t mac[6];
// Get the station MAC address as raw bytes
WiFi.macAddress(mac);
// Print the MAC address in raw format
Serial.print("Station MAC Address (raw): ");
for (int i = 0; i < 6; i++) {
Serial.printf("%02X", mac[i]);
if (i < 5) Serial.print(":");
}
Serial.println();
}
void loop() {
// Your code here
}
AvantMaker Tutorial Suggestion
If you want to learn more about the ESP32’s WiFi capabilities, we’ve created a beginner-friendly guide that explains them in detail. Just click the link below, and you’ll be teleported to that page. It will clear up any confusion you may have while connecting your ESP32 to WiFi or setting it up as a WiFi access point (AP).
ESP32 Library Index
- ESP32 WiFiClient Library
- ESP32 HTTPClient Library
- ESP32 WiFiClientSecure Library
- ESP32 WebServer Library
- ESP32 WiFi Library
- Which ESP32 Boards are Recommended for Learners
- How to Copy Codes from AvantMaker.com
- What is SPIFFS and how to upload files to it?
- What is LIttleFS and how to upload files to it?
Ready to experiment and explore more about ESP32? Visit our website’s All About ESP32 Resources Hub, packed with tutorials, guides, and tools to inspire your maker journey. Experiment, explore, and elevate your skills with everything you need to master this powerful microcontroller platform!