I'm using MQTT on an Arduino to send sensors data to a Javascript page.
Until now I've sent only simple data format: int or floats.
What format can I use to send more complex, structured data? for example:
{ "temperature": 32, "humidity": 67 }
I'd like to send it on a single MQTT message.
I could use Json, but I fear it's a bit heavy for an Arduino.
Update
Ok, seems to be a general consensus on JSON, however I'm a bit concerned about the memory requirement.
I've found two libraries for json encoding/decoding on an Arduino:
ArduinoJson claims to be less memory consuming, however doesn't support:
- reading from Stream (latest versions of the Arduino MQTT library supports receiving messages in streams)
- filtering of incoming json (to selectively parse only required Json fields)
Use the topics themselves to structure the data:
Publishing many simple messages is easier for a resource constrained Arduino. The alternative is constructing some large JSON string like you are thinking:
Also, I think the topics is the more useful design (API) for the both publisher and client. For the Arduino side, consider how the topic approach does not force every location to have both temperature and humidity sensors? For the client side, the topic approach allows a use case that shows just the humidity.