I have been through a painful journey with MQTT and sending messages.
If I want to send a message with multiple parts, they aren't sent over MQTT as is.
What I need to do is move them all into msg.payload
.
At the other end of the MQTT line, I was told to use a JSON
node to deconstruct the message back to its original parts.
Example flow:
[{"id":"704f811f.d9e7f8","type":"inject","z":"b95543c8.1478f8","name":"","topic":"","payload":"TEST_Payload","payloadType":"str","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":110,"y":120,"wires":[["d736dbcb.6d2bc8"]]},{"id":"d736dbcb.6d2bc8","type":"change","z":"b95543c8.1478f8","name":"","rules":[{"t":"set","p":"device","pt":"msg","to":"TEST_Device","tot":"str"},{"t":"set","p":"topic","pt":"msg","to":"TEST_Topic","tot":"str"},{"t":"set","p":"state","pt":"msg","to":"Test_OnLine","tot":"str"}],"action":"","property":"","from":"","to":"","reg":false,"x":310,"y":120,"wires":[["23f518d6.fe0408","2b27f5e2.9ffa8a"]]},{"id":"23f518d6.fe0408","type":"function","z":"b95543c8.1478f8","name":"WAP msg construct","func":"\n//msg.payload = {payload: msg.payload + \" CHANGED \",device:msg.device};\nmsg.payload = {payload: msg.payload,device:msg.device,state:msg.state};\n\n//node.warn(msg.payload);\n//node.warn(msg.device);\n//node.warn(msg.payload.wap);\nreturn msg;\n","outputs":1,"noerr":0,"x":530,"y":120,"wires":[["552bd947.ef0db","c34fb03e.fe2be8"]]},{"id":"552bd947.ef0db","type":"mqtt out","z":"b95543c8.1478f8","name":"","topic":"TEST","qos":"","retain":"false","broker":"8a2e80be.f7c928","x":730,"y":120,"wires":[]},{"id":"d7c33418.01aea","type":"mqtt in","z":"b95543c8.1478f8","name":"","topic":"TEST","qos":"2","datatype":"auto","broker":"8a2e80be.f7c928","x":160,"y":260,"wires":[["d06fbdf0.2c3148","1f9e8c62.b1dd74"]]},{"id":"c34fb03e.fe2be8","type":"debug","z":"b95543c8.1478f8","name":"1","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"true","targetType":"full","x":730,"y":80,"wires":[]},{"id":"d06fbdf0.2c3148","type":"debug","z":"b95543c8.1478f8","name":"2","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"true","targetType":"full","x":290,"y":210,"wires":[]},{"id":"1f9e8c62.b1dd74","type":"json","z":"b95543c8.1478f8","name":"","property":"payload","action":"","pretty":false,"x":290,"y":260,"wires":[["195e78d7.76fb77"]]},{"id":"195e78d7.76fb77","type":"debug","z":"b95543c8.1478f8","name":"3","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"true","targetType":"full","x":460,"y":210,"wires":[]},{"id":"2b27f5e2.9ffa8a","type":"debug","z":"b95543c8.1478f8","name":"0","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"true","targetType":"full","x":490,"y":80,"wires":[]},{"id":"8a2e80be.f7c928","type":"mqtt-broker","z":"","name":"MQTT host","broker":"192.168.0.99","port":"1883","clientid":"","usetls":false,"compatmode":true,"keepalive":"60","cleansession":true,"birthTopic":"ARDUINO_STATUS","birthQos":"2","birthPayload":"connected","willTopic":"ARDUINO_STATUS","willQos":"0","willPayload":"disconnected"}]
So:
0
is the actual message to go through.
1
shows the message going into MQTT after constructing the payload..
2
shows what is received. All the data is there packed into the msg.payload
part.
3
should show the original message. It doesn't.
I've been through this and really got hung up on the fact that is is only the msg.payload
which is sent over MQTT. So it was explained that I needed to put them all into the msg.payload
.
That is done by the function
node and the opposite is done by the JSON
node. Which is kind of weird. Why not use a JSON
at both ends? (I'm not going to say. I don't know.)
But it workED! Note: Past tense.
I can't exactly remember where I do this in the bigger picture, but I documented it as how to do it and this is the way I wrote it.
My usual question:
What have I done wrong?