I have been trying to push the data of temp, humidity, and pressure from a sensor to MQTT using ESP 32Cam, and was able to do using the following code:
pressure = bme.readPressure();
//Convert the value to a char array
char preString[16];
dtostrf(pressure, 1, 2, preString);
Serial.print("Pressure: ");
Serial.println(preString);
client.publish("esp32/pressure", preString);
When I started off with the following code for getting the mac address:
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.
But when I tried to publish the Chip id, that I was getting on the serial monitor, to the MQTT, an error came up with this:
call of overloaded 'String(uint64_t)' is ambiguous
Here's my code that I tried:
char macValue[16];
String(ESP.getEfuseMac()).toCharArray(macValue,16);
client.publish("esp32/ChipID", macValue);
1 Answer 1
You would be better off formatting it nicely instead of relying on String...
char macValue[17]; // Don't forget one byte for the terminating NULL...
uint64_t mac = ESP.getEFuseMac();
sprintf(macValue, "%016x", mac);
client.publish("esp32/ChipID", macValue);
However, I'm not certain that sprintf can cope with a 64 bit value like that directly. If not, you can split it:
char macValue[17]; // Don't forget one byte for the terminating NULL...
uint64_t mac = ESP.getEFuseMac();
uint32_t hi = mac >> 32;
uint32_t lo = mac;
sprintf(macValue, "%08x%08x", hi, lo);
client.publish("esp32/ChipID", macValue);
-
1since a MAC address is 6 bytes long, you only need 12 hex digits, so
macValue[13]
is enough and either%012x
or%04%08x
as the sprintf formatJaromanda X– Jaromanda X2019年11月16日 00:15:21 +00:00Commented Nov 16, 2019 at 0:15