MAC Address of ESP32_DevKitC_V4

How to retrieve MAC Address of ESP32_DevKitC_V4?

#include "esp_system.h"

void setup() {
  // Start serial communication
  Serial.begin(115200);

  // Get the MAC address of the ESP32
  uint8_t mac[6];
  esp_read_mac(mac, ESP_MAC_WIFI_STA);

  // Print the MAC address
  Serial.print("MAC Address: ");
  for (int i = 0; i < 6; i++) {
    if (i > 0) Serial.print(":");
    Serial.printf("%02X", mac[i]);
  }
  Serial.println();

  // You can add your main code here, if needed
}

void loop() {
  // Your main code (if any) goes here
}

In this code:

  1. We include the “esp_system.h” library to access system-related functions.
  2. In the setup() function:
    • Serial communication is initialized at a baud rate of 115200 for communication with the serial monitor.
    • The MAC address of the ESP32 is obtained using esp_read_mac function with ESP_MAC_WIFI_STA as the argument. This function retrieves the MAC address associated with the ESP32’s Wi-Fi station interface.
  3. The retrieved MAC address is printed in the format XX:XX:XX:XX:XX:XX, where each XX represents a hexadecimal byte.
  4. The loop() function is left empty for you to add your own code if needed.

Now, you can upload this corrected code to your ESP32-DevKitC_V4, and it should work without any issues, giving you the MAC address via the serial monitor.