据说ESP32的mac地址可以作为设备的唯一ID,在esp-idf的esp_mac.h中定义了mac的几种类型
typedef enum {
ESP_MAC_WIFI_STA, /**< MAC for WiFi Station (6 bytes) */
ESP_MAC_WIFI_SOFTAP, /**< MAC for WiFi Soft-AP (6 bytes) */
ESP_MAC_BT, /**< MAC for Bluetooth (6 bytes) */
ESP_MAC_ETH, /**< MAC for Ethernet (6 bytes) */
ESP_MAC_IEEE802154, /**< if CONFIG_SOC_IEEE802154_SUPPORTED=y, MAC for IEEE802154 (8 bytes) */
ESP_MAC_BASE, /**< Base MAC for that used for other MAC types (6 bytes) */
ESP_MAC_EFUSE_FACTORY, /**< MAC_FACTORY eFuse which was burned by Espressif in production (6 bytes) */
ESP_MAC_EFUSE_CUSTOM, /**< MAC_CUSTOM eFuse which was can be burned by customer (6 bytes) */
ESP_MAC_EFUSE_EXT, /**< if CONFIG_SOC_IEEE802154_SUPPORTED=y, MAC_EXT eFuse which is used as an extender for IEEE802154 MAC (2 bytes) */
} esp_mac_type_t;
可以通过esp_err_t esp_read_mac(uint8_t *mac, esp_mac_type_t type);获取mac相应类型的mac地址,
也可通过esp_err_t esp_efuse_mac_get_default(uint8_t *mac);获取ESP_MAC_BASE
通过遍历mac枚举,循环输出相应类型的mac地址
#include <esp_mac.h>
void mac_address_(){
// 获取mac地址
uint8_t mac_address[6];
esp_err_t err;
for (esp_mac_type_t mac_type = ESP_MAC_WIFI_STA; mac_type <= ESP_MAC_EFUSE_EXT; mac_type = static_cast<esp_mac_type_t>(mac_type + 1))
{
printf(
"MAC Type: %d (%s)\n", mac_type,
mac_type == ESP_MAC_WIFI_STA ? "ESP_MAC_WIFI_STA"
: mac_type == ESP_MAC_WIFI_SOFTAP ? "ESP_MAC_WIFI_SOFTAP"
: mac_type == ESP_MAC_BT ? "ESP_MAC_BT"
: mac_type == ESP_MAC_ETH ? "ESP_MAC_ETH"
: mac_type == ESP_MAC_IEEE802154 ? "ESP_MAC_IEEE802154"
: mac_type == ESP_MAC_BASE ? "ESP_MAC_BASE"
: mac_type == ESP_MAC_EFUSE_FACTORY ? "ESP_MAC_EFUSE_FACTORY"
: mac_type == ESP_MAC_EFUSE_CUSTOM ? "ESP_MAC_EFUSE_CUSTOM"
: mac_type == ESP_MAC_EFUSE_EXT ? "ESP_MAC_EFUSE_EXT"
: "Unknown");
err = esp_read_mac(mac_address, mac_type);
if (err == ESP_OK)
{
printf("MAC Address: " MACSTR "\n\n", mac_address[0], mac_address[1], mac_address[2], mac_address[3], mac_address[4], mac_address[5]);
}
else
{
printf("Failed to read MAC address: %s\n\n", esp_err_to_name(err));
}
}
}
ESP32-S3 结果如下:
MAC Type: 0 (ESP_MAC_WIFI_STA)
MAC Address: e8:06:90:97:fa:50
MAC Type: 1 (ESP_MAC_WIFI_SOFTAP)
MAC Address: e8:06:90:97:fa:51
MAC Type: 2 (ESP_MAC_BT)
MAC Address: e8:06:90:97:fa:52
MAC Type: 3 (ESP_MAC_ETH)
MAC Address: e8:06:90:97:fa:53
MAC Type: 4 (ESP_MAC_IEEE802154)
E (4105) system_api: 4 mac type is incorrect (not found)
Failed to read MAC address: ESP_ERR_NOT_SUPPORTED
MAC Type: 5 (ESP_MAC_BASE)
MAC Address: e8:06:90:97:fa:50
MAC Type: 6 (ESP_MAC_EFUSE_FACTORY)
MAC Address: e8:06:90:97:fa:50
MAC Type: 7 (ESP_MAC_EFUSE_CUSTOM)
E (4130) system_api: eFuse MAC_CUSTOM is empty
E (4135) system_api: unsupported mac type
E (4140) system_api: MAC address generation error
Failed to read MAC address: ESP_ERR_NOT_SUPPORTED
MAC Type: 8 (ESP_MAC_EFUSE_EXT)
E (4153) system_api: 8 mac type is incorrect (not found)
Failed to read MAC address: ESP_ERR_NOT_SUPPORTED
可以看出 ESP_MAC_BASE、ESP_MAC_EFUSE_FACTORY、ESP_MAC_WIFI_STA是一样的
如果用Ardunio的<wifi.h>,也能获取地址,和ESP_MAC_WIFI_STA一样的
#include <WiFi.h>
void mac_address() {
uint8_t mac[6];
WiFi.macAddress(mac);
Serial.print("MAC Address: ");
for (int i = 0; i < 6; ++i) {
Serial.printf("%02X", mac[i]);
if (i != 5) Serial.print(":");
}
Serial.print("\n\n");
}
标签:ESP,ESP32,EFUSE,MAC,mac,地址,address,type
From: https://blog.csdn.net/cnnews/article/details/145065081