Arduino Forum

Development => Other Hardware Development => Topic started by: cweinhofer on May 02, 2019, 10:52 am

Title: ESP8266 style chipId for ESP32
Post by: cweinhofer on May 02, 2019, 10:52 am
This is not actually a question, but rather a solution I had previously tried searching this forum for and wanted to post for the benefit of other newbies like me.

I have been using ESP8266 boards (LOLIN D1 mini Pro's specifically) for a project and recently started using ESP32 boards (LOLIN D32 Pro's) as well in the same project. When I set up the project originally, I needed a way for the boards to self-identify and found a solution using ESP8266's ESP.getChipId() along with a switch...case. (You can read the background here (http://forum.arduino.cc/index.php?topic=571236.0).)

The chip ID works great for this because it is relatively unique, but still provides an easy to work with integer. However, when moving to the ESP32, I ran into a problem because it doesn't have getChipId. Fortunately, the chip ID for ESP8266 is just a decimal output of the last three bytes of the MAC address. So, you can extract those by truncating using a bitshift after calling the ESP32's MAC address with ESP.getEfuseMac(). You use a 40-bit shift because in the ESP32, the MAC address is defined as a 64-bit integer, but you just want to keep the last 24 bits (aka 3 bytes). Also, I believe ESP.getEfuseMac() is preferred over WiFi.macAddress(), because it's available even before starting the wifi.

Here's the code I came up with:

Code: [Select]
uint64_t macAddress = ESP.getEfuseMac();
uint64_t macAddressTrunc = macAddress << 40;
chipID = macAddressTrunc >> 40;


The code assumes you have 'uint32_t chipID;' defined in your universal variables at the beginning of the sketch.

If you want to load the same sketch code on both ESP8266s and ESP32s, you can use #ifdef

Code: [Select]
#ifdef ESP8266
 
 chipID = ESP.getChipId();
 
#endif

#ifdef ESP32
 
 uint64_t macAddress = ESP.getEfuseMac();
 uint64_t macAddressTrunc = macAddress << 40;
 chipID = macAddressTrunc >> 40;
 
#endif
Title: Re: ESP8266 style chipId for ESP32
Post by: pert on May 03, 2019, 12:33 am
Thanks for sharing the solution you found cweinhofer!