The Paho python MQTT client will accept a string payload and also a binary payload in the msg field.
The receiver will need to know what type of data is in the payload. and decode it accordingly.
When sending JSON data the data is a string and is passed directly into the publish function see How to Send and Receive JSON Data Over MQTT with Python
Regardless of whether you pass a string data or binary data to the publish function the data sent to the network in binary format.
Conversely received data is binary format.
JSON data is simply string data encodes as a JSON object.
Working with Integers and Floats
Sending and receiving integer and float data is common on PLCs (programmable Logic controllers). To send this data requires a little more work as you need to convert it.
The best way of covering this is with examples and below is a screen shot showing how we use the struct module to do this on the command line.
Paho Python Examples –
The following code snippet show how we do this using the Paho MQTT client.
Sending a 16bit integer
We assume our integer is in a variable called our_int
from struct import * client.publish(topic,our_int)
Notice that we don’t need to really do anything as the data is already in binary format
Sending a Float
We assume our float is in a variable called our_float
from struct import * client.publish(topic,our_float)
Notice that we don’t need to really do anything as the data is already in binary format
Sending a 16bit integer + Float
We assume our integer is in a variable called our_int and float in our_float
from struct import * msg=our_int+our_float #add buffers client.publish(topic,msg)
Notice that we don’t need to really do anything as the data is already in binary format
Receiving a 16 bit integer
We assume our integer is in a variable called our_int
from struct import * our_int= unpack(>h,msg.payload)
This time we need to convert the data from bytes to the python representation.
Receiving a Float
We assume our float is in a variable called our_float
from struct import * our_float= unpack(>h,msg.payload) client.publish(topic,msg)
This time we need to convert the data from bytes to the python representation.
Receiving a 16bit integer + Float
We assume our integer is in a variable called our_int and float in our_float
from struct import * Our_payload=unpack(>hf,msg.payload)
This time we need to convert the data from bytes to the python representation.
Notes:
1.The >h is used as the data is in Big endian format on a network.
2 h is short integer (16 bits)
Worked examples
Below are some worked examples to help you better understand how to use the struct module
Example 1: send 4*long Integers (4 bytes)+string
msg=pack(“>IIII 4s”,Int1,int2,int3,int4,b”test”)
Example 2: send 1 short integer +1long Integer (4 bytes)+string
msg=pack(“>hI4s”,Int1,int2,b”test”)
Demo Script
The demo script illustrate the above including worker examples.
Resources and Related tutorials:
- Python Struct Module
- Checking Active MQTT Client Connections
- How to Send and Receive JSON Data Over MQTT with Python
- How to Send a File Using MQTT and Python
- How to Encrypt MQTT Payloads with Python – Example Code
- Converting JSON to CSV with Python