MQTT icon indicating copy to clipboard operation
MQTT copied to clipboard

Give an example of publish with retained bit set

Open raenrfm opened this issue 7 years ago • 24 comments

I'm trying to send payloads that I want retained, but I cannot figure out the syntax of the publish statement to allow that. I'm using QOS0 so don't care about that.

raenrfm avatar Oct 27 '17 14:10 raenrfm

@raenrfm you use this function. bool publish(const char *topic, const uint8_t *payload, unsigned int plength, bool retain);

hirotakaster avatar Nov 06 '17 01:11 hirotakaster

This is the modified callback function correct? But how is it used in the sketch? Is it just the client.publish(topic,payload); command? Do I have to pass anything else in this command or is modifying the callback function enough?

raenrfm avatar Nov 06 '17 14:11 raenrfm

@raenrfm If you want to use retain flag use this, but I don't know what you want to do.

hirotakaster avatar Nov 06 '17 15:11 hirotakaster

But how is it used in the sketch? Can you just show me a sketch that publishes with the retained bit set to true? I want my MQTT server to retain values even if they have been read by one device. I want the value to stay in the server until a new value replaces it. That is what the retained message is for.

raenrfm avatar Nov 06 '17 15:11 raenrfm

yes, you set "retain = true" like this publish("/topic", "hoge", 4, true); . This library is OSS(MIT), so you can see source code by yourself, and if you need more function or implementation please send a diff/pull request.

hirotakaster avatar Nov 06 '17 15:11 hirotakaster

@hirotakaster I've studied the source code but can't get this to work either, tried casting input values to different types without success. Particle compile service returns this:

call of overloaded 'publish(const char[18], const char[3], int, bool)' is ambiguous

onsimon avatar Dec 01 '17 22:12 onsimon

Use case for me is setting up an remote switch integration with Home Assistant (docs). The retain flag is used to feed back status after switching on/off.

Example topic="bedroom/light/status", payload="ON"

onsimon avatar Dec 01 '17 22:12 onsimon

Darn.. the curse of posting questions. After looking one more time at the source I noticed the small type difference of the payload, this works for me now:

client.publish("bedroom/light/status", (uint8_t*) "ON", 2, true);

Ping @raenrfm in case you haven't solved it

onsimon avatar Dec 01 '17 22:12 onsimon

Ok so the addition of the (uint8_t*) is what solved the issue?

On Fri, Dec 1, 2017, 5:53 PM Simon Hellbe, [email protected] wrote:

Darn.. the curse of posting questions. After looking one more time at the source I noticed the small type difference of the payload, this works for me now:

client.publish("bedroom/light/status", (uint8_t*) "ON", 2, true);

Ping @raenrfm https://github.com/raenrfm in case you haven't solved it

— You are receiving this because you were mentioned. Reply to this email directly, view it on GitHub https://github.com/hirotakaster/MQTT/issues/68#issuecomment-348635615, or mute the thread https://github.com/notifications/unsubscribe-auth/AX9UrvJ5IZw694cPAI7QzfUK4_piu0Yzks5s8INZgaJpZM4QJG6y .

--

raenrfm avatar Dec 02 '17 14:12 raenrfm

Yep @raenrfm

onsimon avatar Dec 02 '17 22:12 onsimon

Would this apply if your payloads are just unsigned integers?

On Sat, 2 Dec 2017 at 17:34 Simon Hellbe [email protected] wrote:

Yep @raenrfm https://github.com/raenrfm

— You are receiving this because you were mentioned. Reply to this email directly, view it on GitHub https://github.com/hirotakaster/MQTT/issues/68#issuecomment-348725089, or mute the thread https://github.com/notifications/unsubscribe-auth/AX9UrsqV4cq5PHV5yaEs2erE1m82rr6Pks5s8dBkgaJpZM4QJG6y .

--

raenrfm avatar Dec 06 '17 14:12 raenrfm

Sorry what I meant was that I'm using this on a particle photon and have the payloads set as unsigned int's

On Wed, 6 Dec 2017 at 09:48 Robert Mantel [email protected] wrote:

Would this apply if your payloads are just unsigned integers?

On Sat, 2 Dec 2017 at 17:34 Simon Hellbe [email protected] wrote:

Yep @raenrfm https://github.com/raenrfm

— You are receiving this because you were mentioned. Reply to this email directly, view it on GitHub https://github.com/hirotakaster/MQTT/issues/68#issuecomment-348725089, or mute the thread https://github.com/notifications/unsubscribe-auth/AX9UrsqV4cq5PHV5yaEs2erE1m82rr6Pks5s8dBkgaJpZM4QJG6y .

--

raenrfm avatar Dec 06 '17 14:12 raenrfm

I really wish he created an example of using the retained feature. I'm still having trouble getting this to work.

On Wed, 6 Dec 2017 at 09:51 Robert Mantel [email protected] wrote:

Sorry what I meant was that I'm using this on a particle photon and have the payloads set as unsigned int's

On Wed, 6 Dec 2017 at 09:48 Robert Mantel [email protected] wrote:

Would this apply if your payloads are just unsigned integers?

On Sat, 2 Dec 2017 at 17:34 Simon Hellbe [email protected] wrote:

Yep @raenrfm https://github.com/raenrfm

— You are receiving this because you were mentioned. Reply to this email directly, view it on GitHub https://github.com/hirotakaster/MQTT/issues/68#issuecomment-348725089, or mute the thread https://github.com/notifications/unsubscribe-auth/AX9UrsqV4cq5PHV5yaEs2erE1m82rr6Pks5s8dBkgaJpZM4QJG6y .

--

--

raenrfm avatar Dec 06 '17 15:12 raenrfm

I think you're ok as long as you're not overflowing it with a high number, uint8_t is short for an unsigned 8 bit integer (i.e. 0-255). But I'm kind of out of my depth here :)

onsimon avatar Dec 06 '17 15:12 onsimon

Check out this thread for converting high ints to uint8_t arrays: https://forum.arduino.cc/index.php?topic=361659.0

onsimon avatar Dec 06 '17 15:12 onsimon

yeah, it's working without the retained setup, but I don't know what callback function to use to publish a retained payload.

On Wed, 6 Dec 2017 at 10:12 Simon Hellbe [email protected] wrote:

I think you're ok as long as you're not overflowing it with a high number, uint8_t is short for an unsigned 8 bit integer (i.e. 0-255). But I'm kind of out of my depth here :)

— You are receiving this because you were mentioned. Reply to this email directly, view it on GitHub https://github.com/hirotakaster/MQTT/issues/68#issuecomment-349668629, or mute the thread https://github.com/notifications/unsubscribe-auth/AX9UruVQWEvi8_4ME55Zw2Qr7aukmnAoks5s9q7FgaJpZM4QJG6y .

--

raenrfm avatar Dec 06 '17 15:12 raenrfm

He has three example ino files but none of them show the use of the retained bit.

On Wed, 6 Dec 2017 at 10:15 Robert Mantel [email protected] wrote:

yeah, it's working without the retained setup, but I don't know what callback function to use to publish a retained payload.

On Wed, 6 Dec 2017 at 10:12 Simon Hellbe [email protected] wrote:

I think you're ok as long as you're not overflowing it with a high number, uint8_t is short for an unsigned 8 bit integer (i.e. 0-255). But I'm kind of out of my depth here :)

— You are receiving this because you were mentioned. Reply to this email directly, view it on GitHub https://github.com/hirotakaster/MQTT/issues/68#issuecomment-349668629, or mute the thread https://github.com/notifications/unsubscribe-auth/AX9UruVQWEvi8_4ME55Zw2Qr7aukmnAoks5s9q7FgaJpZM4QJG6y .

--

--

raenrfm avatar Dec 06 '17 15:12 raenrfm

You might need to clarify your use case more. The original question was just about publishing with retained flag but now you're talking about callbacks?

I provided an example how to publish with a retained bit in this comment. If you have a high int as the payload the recommendation from the arduino forum thread I linked before is to convert your int to a string and then back to int on the receiving end.

onsimon avatar Dec 06 '17 15:12 onsimon

If you don't use the correct callback function you can't use the retained bit capability, from what I understand.

On Wed, 6 Dec 2017 at 10:20 Simon Hellbe [email protected] wrote:

You might need to clarify your use case more. The original question was just about publishing with retained flag but now you're talking about callbacks?

I provided an example how to publish with a retained bit in this comment https://github.com/hirotakaster/MQTT/issues/68#issuecomment-348635615. If you have a high int as the payload you want to suggest the recommendation from the arduino forum thread I linked before is to convert your int to a string and then back to int on the receiving end.

— You are receiving this because you were mentioned. Reply to this email directly, view it on GitHub https://github.com/hirotakaster/MQTT/issues/68#issuecomment-349671027, or mute the thread https://github.com/notifications/unsubscribe-auth/AX9Urv0BiTutKaJJtvSnZmb17Kh4vTQlks5s9rCjgaJpZM4QJG6y .

--

raenrfm avatar Dec 06 '17 15:12 raenrfm

Here is the callback function used in his example:

void callback(char* topic, byte* payload, unsigned int length);

if you attempt to use this with the retained bit option in your publish statement it blows up.

On Wed, 6 Dec 2017 at 10:24 Robert Mantel [email protected] wrote:

If you don't use the correct callback function you can't use the retained bit capability, from what I understand.

On Wed, 6 Dec 2017 at 10:20 Simon Hellbe [email protected] wrote:

You might need to clarify your use case more. The original question was just about publishing with retained flag but now you're talking about callbacks?

I provided an example how to publish with a retained bit in this comment https://github.com/hirotakaster/MQTT/issues/68#issuecomment-348635615. If you have a high int as the payload you want to suggest the recommendation from the arduino forum thread I linked before is to convert your int to a string and then back to int on the receiving end.

— You are receiving this because you were mentioned. Reply to this email directly, view it on GitHub https://github.com/hirotakaster/MQTT/issues/68#issuecomment-349671027, or mute the thread https://github.com/notifications/unsubscribe-auth/AX9Urv0BiTutKaJJtvSnZmb17Kh4vTQlks5s9rCjgaJpZM4QJG6y .

--

--

raenrfm avatar Dec 06 '17 15:12 raenrfm

I'm converting floats to strings then publishing. I'm thinking that's the difference between your case and mine.

On Wed, 6 Dec 2017 at 10:26 Robert Mantel [email protected] wrote:

Here is the callback function used in his example:

void callback(char* topic, byte* payload, unsigned int length);

if you attempt to use this with the retained bit option in your publish statement it blows up.

On Wed, 6 Dec 2017 at 10:24 Robert Mantel [email protected] wrote:

If you don't use the correct callback function you can't use the retained bit capability, from what I understand.

On Wed, 6 Dec 2017 at 10:20 Simon Hellbe [email protected] wrote:

You might need to clarify your use case more. The original question was just about publishing with retained flag but now you're talking about callbacks?

I provided an example how to publish with a retained bit in this comment https://github.com/hirotakaster/MQTT/issues/68#issuecomment-348635615. If you have a high int as the payload you want to suggest the recommendation from the arduino forum thread I linked before is to convert your int to a string and then back to int on the receiving end.

— You are receiving this because you were mentioned. Reply to this email directly, view it on GitHub https://github.com/hirotakaster/MQTT/issues/68#issuecomment-349671027, or mute the thread https://github.com/notifications/unsubscribe-auth/AX9Urv0BiTutKaJJtvSnZmb17Kh4vTQlks5s9rCjgaJpZM4QJG6y .

--

--

--

raenrfm avatar Dec 06 '17 17:12 raenrfm

FYI, I added a method like so:

bool MQTT::publish(const char* topic, const char* payload, bool retain) {
    return publish(topic, (uint8_t*)payload, strlen(payload), retain, QOS0, NULL);
}

With this, I just do client.publish("someTopic", "ON", true); to send a message with the retain flag.

I'm happy to provide a PR if it's useful to the community @hirotakaster

dakl avatar Feb 12 '19 13:02 dakl

Hi @dakl , Thank you!! Of course, PR welcome:)

hirotakaster avatar Feb 13 '19 04:02 hirotakaster

#77 should fix this. @raenrfm @hirotakaster

dakl avatar Feb 13 '19 10:02 dakl