gxor
gxor

Reputation: 363

ESP32 Arduino-ide how to get unique id

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.

Upvotes: 2

Views: 16794

Answers (1)

Dr.Random
Dr.Random

Reputation: 500

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();
}

Upvotes: 4

Related Questions