• Skip to main content
  • Skip to primary sidebar

RNTLab.com

The Ultimate Shortcut to Learn Electronics and Programming with Open Source Hardware and Software

  • Courses
  • Forum
    • Forum
    • Ask Question
  • Shop
  • Account
  • Blog
  • Login

MQTT callback

Q&A Forum › Category: Other › MQTT callback
0 Vote Up Vote Down
Peter Rhodin asked 9 years ago

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();

Question Tags: MQTT
1 Answers
0 Vote Up Vote Down
Rui Santos Staff answered 9 years ago

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

Primary Sidebar

Login to Ask or Answer Questions

This Forum is private and it’s only available for members enrolled in our Courses.

Login »

Latest Course Updates

  • [New Edition] Build ESP32-CAM Projects eBook – 2nd Edition April 16, 2025
  • [eBook Updated] Learn ESP32 with Arduino IDE eBook – Version 3.2 April 16, 2025

You must be logged in to view this content.

Contact Support - Refunds - Privacy - Terms - MakerAdvisor.com - Member Login

Copyright © 2013-2025 · RandomNerdTutorials.com · All Rights Reserved

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.