hirotakaster / MQTT

MQTT for Photon, Spark Core
Other
216 stars 118 forks source link

Give an example of publish with retained bit set #68

Open raenrfm opened 6 years ago

raenrfm commented 6 years ago

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.

hirotakaster commented 6 years ago

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

raenrfm commented 6 years ago

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?

hirotakaster commented 6 years ago

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

raenrfm commented 6 years ago

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.

hirotakaster commented 6 years ago

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.

hellbe commented 6 years ago

@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

hellbe commented 6 years ago

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"

hellbe commented 6 years ago

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

raenrfm commented 6 years ago

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

On Fri, Dec 1, 2017, 5:53 PM Simon Hellbe, notifications@github.com 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 .

--

hellbe commented 6 years ago

Yep @raenrfm

raenrfm commented 6 years ago

Would this apply if your payloads are just unsigned integers?

On Sat, 2 Dec 2017 at 17:34 Simon Hellbe notifications@github.com 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 commented 6 years ago

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 raenrfm@gmail.com wrote:

Would this apply if your payloads are just unsigned integers?

On Sat, 2 Dec 2017 at 17:34 Simon Hellbe notifications@github.com 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 commented 6 years ago

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 raenrfm@gmail.com 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 raenrfm@gmail.com wrote:

Would this apply if your payloads are just unsigned integers?

On Sat, 2 Dec 2017 at 17:34 Simon Hellbe notifications@github.com 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 .

--

--

hellbe commented 6 years ago

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 :)

hellbe commented 6 years ago

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

raenrfm commented 6 years ago

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 notifications@github.com 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 commented 6 years ago

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 raenrfm@gmail.com 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 notifications@github.com 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 .

--

--

hellbe commented 6 years ago

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.

raenrfm commented 6 years ago

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 notifications@github.com 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 commented 6 years ago

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 raenrfm@gmail.com 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 notifications@github.com 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 commented 6 years ago

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 raenrfm@gmail.com 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 raenrfm@gmail.com 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 notifications@github.com 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 .

--

--

--

dakl commented 5 years ago

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

hirotakaster commented 5 years ago

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

dakl commented 5 years ago

77 should fix this. @raenrfm @hirotakaster