MQTT is by design payload content agnostic. So encoding multiple payload parts is up to the application. As you did decide to JSON, I do also encode most of my valuable MQTT payloads in JSON strings.
As for a use-case...
I have an ESP8266 based sensor network spread across our house. The sensor nodes are publishing several sensor readings (temperature, humidity and supply voltage) to the "/raw_sensors/<sensor-ID>" topic on my MQTT broker. These sensor nodes do publish simple strings containing multiple tab separated sensor readings.
A python scripts is running on my Linux server, which is subscribed to that MQTT topic containing raw sensor payloads. Its only purpose is to enhance the payloads on "/raw_sensors/#" with the timestamp of the original MQTT message, recode them into JSON strings and publish the enhanced message onto another MQTT topic called "/sensors/<sensor-ID>".
The next step on my to-do list is to spread the sensor readings across multiple topics:
- "/sensors/temperature/<sensor-ID>"
- "/sensors/humidity/<sensor-ID>"
- "/sensors/vcc/<sensor-ID>"
And later collect the sensor readings in a "[url=
http://graphite.readthedocs.org/en/latest/[/url] bucket".
The following is a small capture of MQTT messages on my broker
Code: Select all
$ mosquitto_sub -v -t "/sensors/#"
/raw_sensors/2A0EA7D7D Humidity=51.82, Temperature=20.23, VCC=3.80
/sensors/2A0EA7D7D {"humidity": "51.82", "temperature": "20.23", "timestamp": "2015-10-22T17:41:36.100254", "vcc": "3.80"}
/raw_sensors/2A0EA7FFD Humidity=49.68, Temperature=21.69, VCC=3.82
/sensors/2A0EA7FFD {"humidity": "49.68", "temperature": "21.69", "timestamp": "2015-10-22T17:41:58.422221", "vcc": "3.82"}
/sensors/2A0EA7DFD {"humidity": "56.15", "temperature": "21.29", "timestamp": "2015-10-22T17:42:19.353905"}
("/sensors/2A0EA7DFD" is a locally attached Arduino board with no local battery in need of power monitoring. Another stupid python script does publish sensor readings read from "/dev/ttyACM0")
The sensor messages are all marked with the "retain" flag. This is a very easy way to let the MQTT broker make the last message on a topic to be persistent. Subscribing to such a topics does always deliver the last retained message, even when the subscriber was not connected while the message got sent.
There are several other "home automation" projects doing this kind of stuff, but it is more fun to plug a few software components together, throw in a few hundred lines of python code and see such things evolve.
Sadly there is no MicroPython involved in this project yet. Another reason why I am waiting for my WiPys to arrive. The ESP8266 port has no I2C yet which I need for my sensors :-/
And i hope to get reasonable ultra-low power performance of the WiPy as well.