-
Notifications
You must be signed in to change notification settings - Fork 230
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
More Advanced Publish #33
Conversation
Cool! I like the sign of the |
I have the feeling the current signature is a little complicated for amateurs. So rather than: void messageReceived(String topic, String payload, char * bytes, unsigned int length) {
Serial.print("incoming: ");
Serial.print(topic);
Serial.print(" - ");
Serial.print(payload);
Serial.println();
} We could have something like this: void messageReceived(MQTTMessage msg) {
Serial.print("incoming: ");
Serial.print(msg.topic());
Serial.print(" - ");
Serial.print(msg.payload());
Serial.println();
} Then we would also have the ability to add more convenience functions like: if (msg.topicBeginsWith("/namespace")) {
// do something
} |
563a7e6
to
9fcc25d
Compare
Hi all! I need to send a message with retained flag, but whatever I try fails and my ESP8266 goes into boot loop. What have I done so far: |
You need allocate the message on the stack and then pass a pointer to Here is a corrected example (not tested tough, but should work): MQTTMessage message; // <- no "*" here
message->topic = devices_topic;
message->payload = JSONBuffer;
message->length = sizeof( JSONBuffer ) / sizeof( char );
message->retained = true;
client.publish(&message); // <- but add "&" here |
I had to change the "->" to "." for the compiler to not show an error and after that it looks like this
The ESP8266 doesn't reboot itself but nothing is sent to the broker. The other code works because with
it sends the message but with no |
You'll need to adjust the buffers sizes. But, I would recommend that you calculate the properly length for the JSON Buffer and not send the whole buffer as it most likely includes old data from potentially longer JSON objects. One thing: In contrast to the topic the payload does not handle any null termination. |
Thanks! BTW I had read the README.md file several times(including the maximum packet size), but not realized that... 😆 |
This adds the method
void publish(MQTTMessage * message);
that allows publishing messages using the newMQTTMessage
struct. Currently the struct only allows to set theretained
flag but may also allow setting the quality of service in later versions.I didn't want add another normal functions as these are considered short-hand functions and setting the retained flag is more advanced.
Solves #27.
@gibix I created a custom struct rather than using the internal structure to expose a nicer API. In the future we might use the same structure also in the
messageReceived
callback and maybe add convenience functions likeString string()
etc.