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

Give an example of publish with retained bit set #68

Open
raenrfm opened this issue Oct 27, 2017 · 24 comments
Open

Give an example of publish with retained bit set #68

raenrfm opened this issue Oct 27, 2017 · 24 comments

Comments

@raenrfm
Copy link

raenrfm commented Oct 27, 2017

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
Copy link
Owner

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

@raenrfm
Copy link
Author

raenrfm commented Nov 6, 2017

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
Copy link
Owner

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

@raenrfm
Copy link
Author

raenrfm commented Nov 6, 2017

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
Copy link
Owner

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.

@onsimon
Copy link

onsimon commented Dec 1, 2017

@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
Copy link

onsimon commented Dec 1, 2017

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
Copy link

onsimon commented Dec 1, 2017

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
Copy link
Author

raenrfm commented Dec 2, 2017 via email

@onsimon
Copy link

onsimon commented Dec 2, 2017

Yep @raenrfm

@raenrfm
Copy link
Author

raenrfm commented Dec 6, 2017 via email

@raenrfm
Copy link
Author

raenrfm commented Dec 6, 2017 via email

@raenrfm
Copy link
Author

raenrfm commented Dec 6, 2017 via email

@onsimon
Copy link

onsimon commented Dec 6, 2017

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
Copy link

onsimon commented Dec 6, 2017

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

@raenrfm
Copy link
Author

raenrfm commented Dec 6, 2017 via email

@raenrfm
Copy link
Author

raenrfm commented Dec 6, 2017 via email

@onsimon
Copy link

onsimon commented Dec 6, 2017

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
Copy link
Author

raenrfm commented Dec 6, 2017 via email

@raenrfm
Copy link
Author

raenrfm commented Dec 6, 2017 via email

@raenrfm
Copy link
Author

raenrfm commented Dec 6, 2017 via email

@dakl
Copy link
Contributor

dakl commented Feb 12, 2019

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
Copy link
Owner

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

@dakl
Copy link
Contributor

dakl commented Feb 13, 2019

#77 should fix this. @raenrfm @hirotakaster

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

4 participants