Convert MAC Address to String

My basic question is how can I convert the units MAC address (from ESP.getEfuseMac() and/or WiFi.macAddress() ) into a string?

I'm pretty sure that someone is going to say, "Why are you doing this? You shouldn't use a string." So here's my situation:

My Arduino (technically an ESP32) sends some json data to my MagicMirror using HTTP POST. The code for sending the data is:

int returnCodeMagicMirror = clientHTTPMagicMirror.POST( postAttributeFirmware + firmwareCode + postAttributeEfuseMac + efuseMac + postAttributeWiFiMac + wifiMac + postAttributeSensorID + sensorID + postEnd );
String payloadMagicMirror = clientHTTPMagicMirror.getString();

I'm no expert, but I believe this is concatenating a bunch of string to form the post. Unless someone can suggest another workaround, I think I need efuseMac and wifiMac to be strings as well.

I found one possible lead with this code (https://forum.arduino.cc/index.php?topic=356979.msg2461266#msg2461266) but I don't understand it well enough to know how to adjust it to input ESP.getEfuseMac() and get out a string called efuseMac.

Any help or pointers in the right direction would be appreciated.

cweinhofer:
My Arduino (technically an ESP32) sends some json data to my MagicMirror using HTTP POST. The code for sending the data is:

int returnCodeMagicMirror = clientHTTPMagicMirror.POST( postAttributeFirmware + firmwareCode + postAttributeEfuseMac + efuseMac + postAttributeWiFiMac + wifiMac + postAttributeSensorID + sensorID + postEnd );

String payloadMagicMirror = clientHTTPMagicMirror.getString();




I'm no expert, but I believe this is concatenating a bunch of string to form the post. Unless someone can suggest another workaround, I think I need efuseMac and wifiMac to be strings as well.

They are likely Strings indeed ut don’t post snippets (Snippets R Us!)

what is clientHTTPMagicMirror? which library is being used? is there a way to post params independently? what is postAttributeFirmware, firmwareCode etc ?


I won’t comment on the use of the String class, you know the pitfalls.

When you do something like

uint64_t espChipID = ESP.getEfuseMac();

you use a 64 bits (8 bytes) long integer. The compiler does not know how to convert those into Strings or cStrings, so you need to do this yourself.
The actual values is only 6 bytes long if I remember well, so it’s a matter of reading each byte individually and converting into its ASCII hexadecimal representation. this is what the code posted in the link you gave does. This is how you would use it

String mac2String(byte ar[]) {
  String s;
  for (byte i = 0; i < 6; ++i)
  {
    char buf[3];
    sprintf(buf, "%02X", ar[i]); // J-M-L: slight modification, added the 0 in the format for padding 
    s += buf;
    if (i < 5) s += ':';
  }
  return s;
}


uint64_t EfuseMac = 0xEFBEADDEAD0B;

void setup() {
  Serial.begin(115200);
  Serial.println(mac2String((byte*) &EfuseMac));
}

void loop() {}

You should see in the Serial console opened at 115200 bauds

[color=purple]0B:AD:DE:AD:BE:EF[/color]

(it’s reversed because of the little endian architecture, so LSB comes first)

→ the String you need is what is returned by the call mac2String((byte*) &EfuseMac)

Sorry, I was trying to keep the question shorter and fell into the classic trap of not providing enough information. My complete code for this project would be a lot to wade through, but the code I stole from provides a pretty clear example:

Thanks also for your example code. I think the thing that threw me off was was that the function was named "mac2String". But if I understand your example right, that initial code I doesn't have anything specifically to do with the MAC address. It's just code to use a byte array to convert a 64-bit integer (which could be any 64-bit integer) to a string. You have to feed in the MAC address separately after the initial code. Am I understanding that right?

Also, from my experimenting, it appears that you only need this for the for the EfuseMac.

For WiFi.macAddress() you can just use:

String wifiMacString = WiFi.macAddress();
1 Like

cweinhofer:
I think the thing that threw me off was was that the function was named "mac2String". But if I understand your example right, that initial code I doesn't have anything specifically to do with the MAC address. It's just code to use a byte array to convert a 64-bit integer (which could be any 64-bit integer) to a string.

The code expects a pointer to a place in memory where you have 6 bytes to be converted in Hexadecimal, separated by ':'. So it's not really a 64 bit integer, could be an array of 6 bytes for example