How to send binary data?

Mercury picture Mercury · May 10, 2016 · Viewed 11.1k times · Source

I am using Mosquito MQTT server to broadcast messages.

How can I send a binary data (not text)?

Example:

mosquitto_pub -t test -m 0x452343

Should be received as:

0100 0101 0010 011 0100 0011

Answer

njh picture njh · May 11, 2016

If you literally want to send that binary sequence of characters, then you can use echo to convert from a string to binary using:

echo -ne "\x45\x23\x43" | mosquitto_pub -h test.mosquitto.org -t 'test/binary' -s

This also works for the output of binary commands, such as capturing an image on a Raspberry Pi:

raspistill -o - | mosquitto_pub -h test.mosquitto.org -t 'webcam/' -s