Skip to content
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

How do I publish a retained message #60

Closed
nordicblue opened this issue Feb 23, 2017 · 2 comments
Closed

How do I publish a retained message #60

nordicblue opened this issue Feb 23, 2017 · 2 comments

Comments

@nordicblue
Copy link

Sorry, I know that this is not really an issue with your code and more with my stupidity or lack of knowledge. What I cannot figure out is how do I publish a retained message.

I know how to publish a normal message, as:

client.publish(stateSpeedTopic + String(invId), (fanSpeedStatus));

Both stateSpeedTopic and fanSpeedStatus are of type string.

How do I alter this to send a retained message instead?

Thank for so much for any help, I have been scratching my head for hours and not getting anywhere:(

P.S. Thank you so much for the code, it has been working excellent and is easier for me to folllow than the other alternative for esp8266 mqtt client available. I just need to get the state of my fan to be retained in my MQTT server. Otherwise, my fan decides to turn on randomly...lol.

@256dpi
Copy link
Owner

256dpi commented Feb 23, 2017

The current API does not provide a convenience function to publish a message with the retained flag. You have to use the more low-level function:

boolean publish(MQTTMessage * message);

The following is a snippet that explains the usage:

String topic;
String payload;

MQTTMessage message;

message.topic = (char*)topic.c_str();
message.payload = (char*)topic.c_str();
message.length = topic.length();
message.retained = true;

client.publish(&message);

As this is a little complicated to figure out I will conside to simplify it in v2.

@nordicblue
Copy link
Author

Thanks for the quick reply. I'll try to play with that to get it working and I look forward to any future enhancements:)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants