Hi and big thanks to your latest “Build a home automation system for 100$”
I dont understand the details in the pubsubclient… Page 152. Do I really need to read character by character when get data from mqtt?
void callback(String topic, byte* message, unsigned int length) {
Serial.print(“Message arrived on topic: “);
Serial.print(topic); Serial.print(“. Message: “);
String messageTemp;
for (int i = 0; i < length; i++) { Serial.print((char)message[i]);
messageTemp += (char)message[i];
}
Serial.println();
Hi Peter that’s a great question.
The callback() function receives the MQTT messages in the byte* format. That’s how the callback function is built in the backend.
The best way I’ve found to work with the data received from the MQTT message was to convert from byte* to char. Because all the other if statements in the rest of the code will be easier to do and more comprehensive.
In my examples the message is easy to understand:
if(messageTemp == “1“){
But if you had a longer message, it would be hard to compare byte* to a String.
if(messageTemp == “anymessage“){
I hope this helps,
Rui