4

It's just a few days since I started using Arduino IDE ( programming an ESP32 ). As I learn, I am trying to write some snippets. I wanted to store the Chip ID of ESP32 into a variable (preferably string?) so that I can generate an unique device name. I looked through the ESP32 example programs and came across this program which just prints the Chip ID to the serial console.

  uint64_t chipid;

  chipid = ESP.getEfuseMac(); //The chip ID is essentially its MAC address(length: 6 bytes).
  Serial.printf("ESP32 Chip ID = %04X", (uint16_t)(chipid >> 32)); //print High 2 bytes
  Serial.printf("%08X\n", (uint32_t)chipid); //print Low 4bytes.

Now this works fine but I needed to store it into a variable in the following format "ESP32-0A1B3C4D5E6F" ( the last part of which is the Chip/MAC ID which is 6 bytes ). I went about some reading and currently reading and managed to get this far

  char ssid1[15];
  char ssid2[15];

  uint64_t chipid = ESP.getEfuseMac(); // The chip ID is essentially its MAC address(length: 6 bytes).
  uint16_t chip = (uint16_t)(chipid >> 32);

  snprintf(ssid1, 15, "%04X", chip);
  snprintf(ssid2, 15, "%08X", (uint32_t)chipid);

  Serial.print(ssid1);
  Serial.println(ssid2);

I am trying to read this which seems to be an excellent article on handling Arduino strings but just being a few days old with Arduino/c++ I am having some difficulty in wrapping my head around some of the concepts. I come from a Python, PHP, Javascript background so I guess this is gona take a little bit of a time. I am getting the output that I want in the serial console but now I would need this in this format (MCUDEVICE-0A1B3C4D5E6F). I am sure this code can be written in a much nicer, cleaner manner but for now I need this to proceed with another snippet (which relies on device name in the above mentioned format). Can anyone provide some pointers while I try check out the different ways I could achieve this? I just want to be sure that I am not writing ugly (and dangerous code - crashes, buffer overruns etc). Greatly appreciate any pointers. I guess I am starting to get a little paranoid about handling strings ( especially on low memory devices like typical mcus ).

R.W
  • 155
  • 1
  • 1
  • 7

3 Answers3

5

You can combine all your work into one single line with the snprintf. You aren't limited to just one variable, and you can add plain text:

char ssid[23];

snprintf(ssid, 23, "MCUDEVICE-%04X%08X", chip, (uint32_t)chipid);
Majenko
  • 105,851
  • 5
  • 82
  • 139
1

Why don't you simply use:

char ssid[23];
snprintf(ssid, 23, "MCUDEVICE-%llX", ESP.getEfuseMac());

This way no need to bit-shift and extra middle variables...

Just watch out for capital X for uppercase result and small 'l' for type indication. (meaning use either llX or llx and not LLX).

AKTanara
  • 191
  • 6
0

This works on an ESP32-S3 WROOM on Arduino 1.8.19:

void setup() {
  // put your setup code here, to run once:
  Serial.begin(115200);

String MCUtype = "ESP32s3" ; char ssid[14]; snprintf(ssid, 14, "%llX", ESP.getEfuseMac()); Serial.println(ssid); // String ssid1 = ssid ; // convert from char to String Serial.println(ssid1); int StrLen = ssid1.length(); Serial.printf("Length of the string %i characters \r\n",StrLen); String MAC =""; int i = 0; while ( i < 6) {
MAC = MAC + ssid1.substring(StrLen-2-2i,StrLen-2i)+":"; i++ ; } MAC = MAC.substring(0,17); // remove last : Serial.println(MAC); String Serialnumber = MCUtype+"-" ; int j = 3; while ( j < 6) {
Serialnumber = Serialnumber + ssid1.substring(StrLen-2-2j,StrLen-2j); j++ ; } Serial.println(Serialnumber); }

void loop() { // put your main code here, to run repeatedly:

}

VE7JRO
  • 2,515
  • 19
  • 27
  • 29
Marius
  • 1