# How to send a binary file to MQTT

**URL:** <https://discourse.nodered.org/t/how-to-send-a-binary-file-to-mqtt/69780>\
**Category:** General\
**Created:** [29 October 2022 20:27 UTC](https://discourse.nodered.org/t/how-to-send-a-binary-file-to-mqtt/69780 "2022-10-29T20:27:13Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![jsail](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/jsail/32/20176_2.png) [@jsail](https://discourse.nodered.org/u/jsail)\
**Post date:** [29 October 2022 20:27 UTC](https://discourse.nodered.org/t/how-to-send-a-binary-file-to-mqtt/69780/1 "2022-10-29T20:27:13Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![Colin](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/colin/32/17040_2.png) [@Colin](https://discourse.nodered.org/u/Colin)\
**Post date:** [29 October 2022 20:51 UTC](https://discourse.nodered.org/t/how-to-send-a-binary-file-to-mqtt/69780/2 "2022-10-29T20:51:34Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![jsail](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/jsail/32/20176_2.png) [@jsail](https://discourse.nodered.org/u/jsail)\
**Post date:** [29 October 2022 21:08 UTC](https://discourse.nodered.org/t/how-to-send-a-binary-file-to-mqtt/69780/3 "2022-10-29T21:08:14Z")

</div>

I tried that with the attached photo.

[snow|282x302]  
 ![snow](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/b/6/b6426d2fe8d23b49b42bc177044c3658d80f3db8.png)

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

---

<div class="post-metadata">

**Author:** ![Colin](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/colin/32/17040_2.png) [@Colin](https://discourse.nodered.org/u/Colin)\
**Post date:** [29 October 2022 21:26 UTC](https://discourse.nodered.org/t/how-to-send-a-binary-file-to-mqtt/69780/4 "2022-10-29T21:26:33Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![jsail](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/jsail/32/20176_2.png) [@jsail](https://discourse.nodered.org/u/jsail)\
**Post date:** [29 October 2022 21:48 UTC](https://discourse.nodered.org/t/how-to-send-a-binary-file-to-mqtt/69780/5 "2022-10-29T21:48:50Z")

</div>

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

 ![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/1/2/12be43f4741c8dff73f87466be8aaaee02632b96.png)

---

<div class="post-metadata">

**Author:** ![zenofmud](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/zenofmud/32/316_2.png) [@zenofmud](https://discourse.nodered.org/u/zenofmud)\
**Post date:** [29 October 2022 23:22 UTC](https://discourse.nodered.org/t/how-to-send-a-binary-file-to-mqtt/69780/6 "2022-10-29T23:22:13Z")

</div>

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:

 ![Screen Shot 2022-10-29 at 7.26.10 PM](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/f/9/f967cffe6bbb783f636bfb2245dd6b2bac02a1eb.png)  
and here is the flow:

```auto
[{"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.

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/1X/d073cd938eafa2e558d7c2cd59003b3ef4963033.png) [@system](https://discourse.nodered.org/u/system)\
**Post date:** [12 November 2022 23:22 UTC](https://discourse.nodered.org/t/how-to-send-a-binary-file-to-mqtt/69780/7 "2022-11-12T23:22:32Z")

</div>

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