How to send a binary file to MQTT

I would like to send a small photo to the MQTT broker.

Unfortunately I only find a CMD call for it: mosquitto_pub -t test -f file

Read the file using a File In node, then send it to the MQTT node.

I tried that with the attached photo.

[snow|282x302]
snow

Unfortunately only scrap arrives!
{�PNG
IHDR.QX�`iCCPICC Profile(�m�KBQƿW� j�h(phh��gCNj}

You need to set the file and MQTT In nodes to output Buffers.

How can I send the photo to a buffer? The node 'read file' is in 'single buffer object' mode.

In the file-in node, with the 'Output' option to a single Buffer object it sends the file as a buffer.
Here is an example using your image:


and here is the flow:

[{"id":"2d1cfef735e11320","type":"file in","z":"b2044f46e2eecabc","name":"","filename":"/Users/Paul/jsail.png","filenameType":"str","format":"","chunk":false,"sendError":false,"encoding":"none","allProps":false,"x":380,"y":80,"wires":[["3329ae5b84981c7c"]]},{"id":"8d43ae0aefafea40","type":"inject","z":"b2044f46e2eecabc","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"","payloadType":"date","x":160,"y":80,"wires":[["2d1cfef735e11320"]]},{"id":"3329ae5b84981c7c","type":"mqtt out","z":"b2044f46e2eecabc","name":"","topic":"myimage","qos":"","retain":"","respTopic":"","contentType":"","userProps":"","correl":"","expiry":"","broker":"feed7af93df575b7","x":620,"y":80,"wires":[]},{"id":"f70dc5a9c83b0acf","type":"mqtt in","z":"b2044f46e2eecabc","name":"","topic":"myimage","qos":"2","datatype":"buffer","broker":"feed7af93df575b7","nl":false,"rap":true,"rh":0,"inputs":0,"x":180,"y":200,"wires":[["1dc1881f4408587a"]]},{"id":"1dc1881f4408587a","type":"image viewer","z":"b2044f46e2eecabc","name":"","width":160,"data":"payload","dataType":"msg","active":true,"x":450,"y":200,"wires":[[]]},{"id":"feed7af93df575b7","type":"mqtt-broker","name":"","broker":"mqttpizw.local","port":"1883","clientid":"","autoConnect":true,"usetls":false,"protocolVersion":"4","keepalive":"60","cleansession":true,"birthTopic":"","birthQos":"0","birthRetain":"false","birthPayload":"","birthMsg":{},"closeTopic":"","closeQos":"0","closeRetain":"false","closePayload":"","closeMsg":{},"willTopic":"","willQos":"0","willRetain":"false","willPayload":"","willMsg":{},"userProps":"","sessionExpiry":""}]

you will have to install node-red-contrib-image-tools and set the mqtt broker credentials.

2 Likes

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