Sending general files, json files, images over MQTT between different Node-Red devices

Is this possible to send files over MQTT using the Node-Red MQTT nodes?

I have tried using FTP to send files between different devices running Node-Red on same network but can't get it working.

I however can send data over MQTT without drama. I can send a basic text file over MQTT, but items like a .jpg file are not opening on the other end.

Any advice appreciated.

A jpeg is not text, as you have discovered.
You would read the file and output a buffer, you then send the buffer over mqtt. You may want to send filename (where you want to save and name this file) also, so you would move things into payload to send, and move them back to msg properties to save.
here is an example assuming both machines have same folder directory.

[{"id":"f61b661169542bd7","type":"inject","z":"b9860b4b9de8c8da","name":"","props":[{"p":"filename","v":"/data/data/com.termux/files/home/.node-red/static/files/test.jpg","vt":"str"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","x":270,"y":360,"wires":[["ec6ef0512f78d97d"]]},{"id":"ec6ef0512f78d97d","type":"file in","z":"b9860b4b9de8c8da","name":"","filename":"filename","filenameType":"msg","format":"","chunk":false,"sendError":false,"encoding":"none","allProps":false,"x":420,"y":360,"wires":[["0f6be3e427a24f98"]]},{"id":"0f6be3e427a24f98","type":"change","z":"b9860b4b9de8c8da","name":"","rules":[{"t":"move","p":"payload","pt":"msg","to":"payload.data","tot":"msg"},{"t":"set","p":"payload.filename","pt":"msg","to":"filename","tot":"msg"}],"action":"","property":"","from":"","to":"","reg":false,"x":600,"y":360,"wires":[["df1432381b72c116"]]},{"id":"df1432381b72c116","type":"mqtt out","z":"b9860b4b9de8c8da","name":"","topic":"my/file/topic","qos":"","retain":"","respTopic":"","contentType":"","userProps":"","correl":"","expiry":"","broker":"e8ba3ef5.22f4a8","x":810,"y":360,"wires":[]},{"id":"a4dc60a831051397","type":"mqtt in","z":"b9860b4b9de8c8da","name":"","topic":"my/file/topic","qos":"2","datatype":"auto-detect","broker":"e8ba3ef5.22f4a8","nl":false,"rap":true,"rh":0,"inputs":0,"x":388.0000114440918,"y":416.0000047683716,"wires":[["241be0cb3fde7568"]]},{"id":"241be0cb3fde7568","type":"change","z":"b9860b4b9de8c8da","name":"","rules":[{"t":"set","p":"filename","pt":"msg","to":"payload.filename","tot":"msg"},{"t":"set","p":"payload","pt":"msg","to":"payload.data","tot":"msg"}],"action":"","property":"","from":"","to":"","reg":false,"x":578.0000114440918,"y":416.0000047683716,"wires":[["111b1952a7ffcb1f","8f90a19e23210d78"]]},{"id":"8f90a19e23210d78","type":"function","z":"b9860b4b9de8c8da","name":"function 22","func":"msg.payload = Buffer.from(msg.payload)\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","libs":[],"x":690,"y":460,"wires":[["6954ea05bcf9b9f6"]]},{"id":"111b1952a7ffcb1f","type":"debug","z":"b9860b4b9de8c8da","name":"debug 339","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"true","targetType":"full","statusVal":"","statusType":"auto","x":830,"y":440,"wires":[]},{"id":"6954ea05bcf9b9f6","type":"file","z":"b9860b4b9de8c8da","name":"","filename":"filename","filenameType":"msg","appendNewline":false,"createDir":false,"overwriteFile":"true","encoding":"none","x":800,"y":520,"wires":[["206ef230aa8a551e"]]},{"id":"e8ba3ef5.22f4a8","type":"mqtt-broker","name":"testb","broker":"192.168.1.25","port":"1883","clientid":"","autoConnect":true,"usetls":false,"compatmode":false,"protocolVersion":"4","keepalive":"60","cleansession":true,"birthTopic":"","birthQos":"0","birthRetain":"false","birthPayload":"","birthMsg":{},"closeTopic":"","closeQos":"0","closePayload":"","closeMsg":{},"willTopic":"","willQos":"0","willPayload":"","willMsg":{},"userProps":"","sessionExpiry":""}]

[edit] fixed write file to overwrite, not append.
Also fixed converting the data back to a buffer .

I use this MQTT technique to send messages from one NR on one machine to another NR on another machine

I have two function nodes with a bit of JavaScript in them to let me cleanly send/receive messages between them

delete msg['_msgid'];
return ({ "payload": msg });

for sending

var data;
data = (JSON.parse((msg['payload'])));
return data;

for receiving

A couple of points to save you typing in the future

That could be
return { payload: msg };
You don't need the quotes unless the key has special characters in it, and the parentheses are not required.

Similarly that could be
data = JSON.parse(msg.payload);

Or the whole thing could just be
return JSON.parse(msg.payload);

I actually use Blockly - this is just the JS it produces :slight_smile:

2 Likes

This topic was automatically closed 60 days after the last reply. New replies are no longer allowed.