Send multiple sensor readings in one MQTT payload


#1

Hi,

I used the example code from this link to send data from a Python program using MQTT.

I was able to successfully send one sensor reading.

temperature1_value=110
raw_data='{"temperature1":'+str(temperature1_value)+'}'
client.publish(resource, raw_data, qos=0)

image

When I edited the same code to send reading from more than one sensor, it doesn’t work.

raw_data='{"t1":'+str(t1_value)+',"t2":'+str(t2_value)+',"t3":'+str(t3_value)+',"t4":'+str(t4_value)+'}'
client.publish(resource, raw_data, qos=0)

I see from the MQTT specs that the payload is limited to 268,435,456 bytes. I’m not hitting that limit. Is there something I’m missing here?

Kind regards,
Vijay.


#2

Hi Vijay,

Could you clarify for me what the issue is that you have run into? I see from your second bit of example code that you have a key/value for “t4”, however, that did not come through in what is logged for the Product. Is that the discrepancy you are wondering about?

As you noted, there is a payload limit, but one that you do not appear to be close to hitting. I just tried approximately the same request, with the sensor values hard-coded, and the publish was successful:

raw_data='{"t1":110,"t2":11.89,"t3":10.18,"t4":13.13}'
client.publish(resource, raw_data, qos=0)

Do you have multiple MQTT clients connecting to your Product? Could you run your Python program again and share the results? It may also be helpful to include the timestamps from the logs.