-
Notifications
You must be signed in to change notification settings - Fork 237
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
Sending floating point and integer data through MQTT #336
Comments
Here is an answer the might work. Use JSON libraries to make a JSON object and then convert to binary to send. The top answer here looks promissing. Let us know what does and does not work. |
I ended up converting to a string - yes you can use JSON but its more overhead (code size) on small processors. |
Regarding, "I ended up converting to a string" For what it is worth I have no idea how memory safe the JASON solutions might be. I have read that Strings in Arduino are not safe under some programing methods because of limited memory. Problem is more sever on an UNO of course. |
My own code is a lot more complex, because its also dealing with things like queuing messages to avoid calling publish within messageReceived (a warning in the library). The part you want I think is below. It sends a float by converting to a string. There are probably slightly simpler ways to write
The full code is in the FrugalIoT repo; but this handles a lot more complexity that most situations won't need, including using MQTT within a single device to communicate between different code modules. |
@mitra42
helps Just to be clear, since I see no conditional tests, you are sending every time a float and then an int. Am I right? And thanks again. |
Sorry, no the width variable is misnamed, should be "decimalplaces" so https://docs.arduino.cc/language-reference/en/variables/data-types/stringObject/ |
I've got some sensors generating floating point and integer data. I'd like to send this over MQTT, and ideally without converting back and forth to the Ascii.
The docs have bool publish(const char topic[], const String &payload);
bool publish(const char topic[], const char payload[]);
I'm wondering if there is an accepted method of sending numbers (floating or int) over MQTT, and if so what it is.... I've tried searching for the answer in all the usual places, but I'm not finding it.
I also think it would be useful to have this in at least one of the examples.
The text was updated successfully, but these errors were encountered: