Home / References / ESP32 Library / WiFi API / AP
Description
The softAPmacAddress()
method in the ESP32 WiFi library retrieves the MAC (Media Access Control) address of the ESP32 device when it is in Soft Access Point (SoftAP) mode. This can be useful for network identification, debugging, or device tracking in IoT projects.
Syntax and Usage
The softAPmacAddress()
method can be used in two different ways, depending on whether or not an argument is provided.
– Without argument:
WiFi.softAPmacAddress()
This returns the MAC address of the ESP32 in SoftAP mode as a string formatted as “XX:XX:XX:XX:XX:XX”.
– With argument:
WiFi.softAPmacAddress(mac)
This requires a pointer to a byte array as an argument. The method stores the MAC address of the ESP32 in this array, and does not return any value.
For practical applications and examples of this method, please consult the “Example Code” section on this page. This section provides comprehensive guidance to help you better understand and apply the method effectively.
Argument(s)
- mac (optional): A pointer to a byte array where the MAC address will be stored. This array should be of size 6 bytes.
Return Value
The softAPmacAddress()
method returns a string representing the MAC address of the ESP32 in SoftAP mode, formatted as “XX:XX:XX:XX:XX:XX”. If an argument is passed (a byte array), no value is returned, but the MAC address is stored in the provided array.
Example Codes
Example 1: Using softAPmacAddress
without arguments
This example demonstrates how to use the ESP32’s softAPmacAddress()
method without arguments to retrieve the MAC address of the device when operating as a Wi-Fi Access Point. The method returns the default MAC address of the SoftAP interface, which is useful for network identification and device management.
To use this code, simply upload it to your ESP32 board. It will create an access point named “AvantMaker-ESP32-AP” with password “12345678”, then print the default SoftAP MAC address to the Serial Monitor at 115200 baud rate. Note that this shows the basic usage of softAPmacAddress()
without any parameters.
/*
* Author: Avant Maker
* Date: February 13, 2025
* Version: 1.0
* Description: This example demonstrates how to use the
* softAPmacAddress() method to print the MAC address of
* the ESP32 in SoftAP mode directly to the serial monitor.
*
* 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>
void setup() {
Serial.begin(115200);
// Start the ESP32 in SoftAP mode
WiFi.softAP("ESP32-AP", "password");
// Print the MAC address of the ESP32 in SoftAP mode
Serial.println(WiFi.softAPmacAddress());
}
void loop() {
// Main code here
}
Example 2: Using softAPmacAddress
with arguments
This example demonstrates how to use the ESP32’s softAPmacAddress()
method with a byte array parameter to retrieve the MAC address of the device in SoftAP (Access Point) mode. The code stores the MAC address in a byte array and formats it in the standard XX:XX:XX:XX:XX:XX hexadecimal notation for display.
To use this code, upload it to your ESP32 board. The device will create an access point named “AvantMaker-ESP32-AP” with password “12345678”, then print the properly formatted MAC address to the Serial Monitor at 115200 baud. This shows the advanced usage of softAPmacAddress()
where the MAC address is stored in a provided byte array for further processing.
/*
* Author: Avant Maker
* Date: February 13, 2025
* Version: 1.0
* Description: This example code demostrates how to the softAPmacAddress
* method with an argument (a byte array) to store the MAC address and
* then print it formatted as "XX:XX:XX:XX:XX:XX".
*
* 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>
void setup() {
Serial.begin(115200);
// Start the ESP32 in SoftAP mode
WiFi.softAP("ESP32-AP", "password");
byte mac[6];
// Store the MAC address in the byte array
WiFi.softAPmacAddress(mac);
// Print the MAC address from the byte array
Serial.print("MAC Address: ");
for (int i = 0; i < 6; i++) {
if (mac[i] < 16) {
Serial.print("0");
}
Serial.print(mac[i], HEX);
if (i < 5) {
Serial.print(":");
}
}
Serial.println();
}
void loop() {
// Main 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!