获取 ESP32/ESP8266 MAC 地址并更改 (Arduino IDE)

本指南介绍如何使用 Arduino IDE 获取 ESP32 或 ESP8266 开发板的 MAC 地址。我们还展示了如何更改开发板的 MAC 地址。

什么是 MAC 地址?

MAC 地址代表媒体访问控制地址,它是识别网络上每个设备的硬件唯一标识符。

MAC 地址由六组两位十六进制数字组成,以冒号分隔,例如:32:AE:A7:04:6D:66.

MAC 地址由制造商分配,但您也可以为开发板提供自定义 MAC 地址。但是,每次板子重置时,它都会返回到其原始 MAC 地址。因此,您需要在每个草图中包含设置自定义 MAC 地址的代码。

获取 ESP32 或 ESP8266 MAC 地址

要获取您的开发板 MAC 地址,只需将以下代码上传到 ESP32 或 ESP8266。该代码与两个板兼容。

// Complete Instructions: https://RandomNerdTutorials.com/get-change-esp32-esp8266-mac-address-arduino/

#include <WiFi.h>
#include <esp_wifi.h>

// Set your new MAC Address
uint8_t newMACAddress[] = {0x32, 0xAE, 0xA4, 0x07, 0x0D, 0x66};

void setup(){
  Serial.begin(115200);
  Serial.println();
  
  WiFi.mode(WIFI_STA);
  
  Serial.print("[OLD] ESP32 Board MAC Address:  ");
  Serial.println(WiFi.macAddress());
  
  // ESP32 Board add-on before version < 1.0.5
  //esp_wifi_set_mac(ESP_IF_WIFI_STA, &newMACAddress[0]);
  
  // ESP32 Board add-on after version > 1.0.5
  esp_wifi_set_mac(WIFI_IF_STA, &newMACAddress[0]);
  
  Serial.print("[NEW] ESP32 Board MAC Address:  ");
  Serial.println(WiFi.macAddress());
}
 
void loop(){

}

您可以在以下行中设置自定义 MAC 地址:

uint8_t newMACAddress[] = {0x32, 0xAE, 0xA4, 0x07, 0x0D, 0x66};

上传代码后,以 115200 的波特率打开串口监视器。重新启动 ESP32,你应该得到它的旧 MAC 地址和新 MAC 地址。

更改 ESP8266 MAC 地址(Arduino IDE)

以下代码为 ESP8266 开发板设置自定义 MAC 地址。

// Complete Instructions: https://RandomNerdTutorials.com/get-change-esp32-esp8266-mac-address-arduino/

#include <ESP8266WiFi.h>

// Set your new MAC Address
uint8_t newMACAddress[] = {0x32, 0xAE, 0xA4, 0x07, 0x0D, 0x66};

void setup(){
  Serial.begin(115200);
  Serial.println();
  
  WiFi.mode(WIFI_STA);
  
  Serial.print("[OLD] ESP8266 Board MAC Address:  ");
  Serial.println(WiFi.macAddress());

  // For Soft Access Point (AP) Mode
  //wifi_set_macaddr(SOFTAP_IF, &newMACAddress[0]);
  // For Station Mode
  wifi_set_macaddr(STATION_IF, &newMACAddress[0]);
  
  Serial.print("[NEW] ESP8266 Board MAC Address:  ");
  Serial.println(WiFi.macAddress());
}
 
void loop(){

}

 在以下行中设置您的自定义 MAC 地址:

uint8_t newMACAddress[] = {0x32, 0xAE, 0xA4, 0x07, 0x0D, 0x66};

上传代码后,以 115200 的波特率打开串行监视器。重新启动 ESP8266,你应该得到它的旧 MAC 地址和新 MAC 地址。

总结

在本快速指南中,我们向您展示了如何使用 Arduino IDE 获取 ESP32 和 ESP8266 制造商的 MAC 地址。您还学习了如何为开发板设置自定义 MAC 地址。

猜你喜欢

转载自blog.csdn.net/hhqidi/article/details/131008135