2

I'm trying to set a unique id to each esp32 automatically. Before I was programming the devices using the ESP-IDF Framework that provides the method esp_efuse_mac_get_default() this will return a 8 byte value unique over all devices I had my hands on.

In the arduino ide all I see is the ESP.getEfuseMac() method. This only returns 6 bytes and is the same for all devices of the same batch (?).

Is there any way I can get a 8 byte UUID on a ESP32?

Libraries like ArduinoUniqueID also use the ESP.getEfuseMac() and thus are not usable.

gxor
  • 335
  • 3
  • 11

1 Answers1

3

getEfuseMac() returns a 64 bit integer.

uint64_t EspClass::getEfuseMac(void)
{
    uint64_t _chipmacid = 0LL;
    esp_efuse_mac_get_default((uint8_t*) (&_chipmacid));
    return _chipmacid;
}

It should return its MAC address which is unique to all esp.

On the ESP32 both ESP.getEfuseMac() and ESP.getChipId() returns the same MAC address.

Test it with:

Serial.printf("\nCHIP MAC: %012llx\n", ESP.getEfuseMac());
Serial.printf("\nCHIP MAC: %012llx\n", ESP.getChipId());

Or you could do this:

uint32_t low     = ESP.getEfuseMac() & 0xFFFFFFFF; 
uint32_t high    = ( ESP.getEfuseMac() >> 32 ) % 0xFFFFFFFF;
uint64_t fullMAC = word(low,high);

Serial.printf("Low: %d\n",low);
Serial.printf("High: %d\n",high);
Serial.printf("Full: %d\n",fullMAC);

You can also use IDF functions in Arduino because it was built on it. Check this:

void print_mac(const unsigned char *mac) {
    printf("%02X:%02X:%02X:%02X:%02X:%02X\n", mac[0],mac[1],mac[2],mac[3],mac[4],mac[5]);
}

void macTest(){
    unsigned char mac_base[6] = {0};
    esp_efuse_mac_get_default(mac_base);
    esp_read_mac(mac_base, ESP_MAC_WIFI_STA);
    unsigned char mac_local_base[6] = {0};
    unsigned char mac_uni_base[6] = {0};
    esp_derive_local_mac(mac_local_base, mac_uni_base);
    printf("Local Address: ");
    print_mac(mac_local_base); 
    printf("\nUni Address: ");
    print_mac(mac_uni_base);
    printf("MAC Address: ");
    print_mac(mac_base);
}

void setup(){
    Serial.begin(115200);
    delay(500);
    macTest();
}
aSemy
  • 5,485
  • 2
  • 25
  • 51
Dr.Random
  • 430
  • 3
  • 16
  • 1
    This still only returns a 6 bytes: four bytes for lower, 2 bytes for upper. I don't think the mac is unique to the device: https://esp32.com/viewtopic.php?t=17907 – gxor Aug 22 '22 at 09:34
  • As described in the Espressif documentation the function is returned different lengths: `mac – base MAC address, length: 6 bytes/8 bytes. length: 6 bytes for MAC-48 8 bytes for EUI-64(used for IEEE 802.15.4) ` https://docs.espressif.com/projects/esp-idf/en/latest/esp32/api-reference/system/misc_system_api.html#_CPPv425esp_efuse_mac_get_defaultP7uint8_t How would I switch to EUI-64? – gxor Aug 22 '22 at 09:44
  • I was going to reference this before you put it: length: 6 bytes for MAC-48 * 8 bytes for EUI-64(used for IEEE 802.15.4) – Dr.Random Aug 22 '22 at 09:45
  • You can use IDF functions with Arduino. Check my edit. – Dr.Random Aug 22 '22 at 09:57
  • 1
    Thanks for the edit. I think I will just use the default MAC and hope I don't get a collision. Your edits also returned 6 bytes only sadly. Probably the Arduino-IDE does not setup the device in EUI-64 mode such that these bytes are simply not used. – gxor Aug 22 '22 at 10:16
  • 8 byte MAC for EUI-64 IEEE 802.15.4 is only available on certain esp32 variants (H2 C6) https://github.com/espressif/esp-idf/blob/master/components/efuse/esp32h2/esp_efuse_table.c#L486 https://github.com/espressif/esp-idf/blob/master/components/efuse/esp32c6/esp_efuse_table.c#L542 normal esp32 has 6 bytes MAC + CRC https://github.com/espressif/esp-idf/blob/master/components/efuse/esp32/esp_efuse_table.c#L269 esp32s3 has only 6 bytes MAC https://github.com/espressif/esp-idf/blob/master/components/efuse/esp32s3/esp_efuse_table.c#L670 – Joram Jul 13 '23 at 15:47