Send data as single "packet" instead of multiple

This commit is contained in:
Pierrick C 2018-08-04 19:21:08 +02:00
parent 01dd30d2cf
commit b7205f397f
1 changed files with 9 additions and 11 deletions

View File

@ -80,19 +80,17 @@ with serial.Serial(uart_name, uart_baud_rate, timeout=2) as uart:
# Formatting data for sending to MQTT broker (one source at a time)
for s in data.items():
mqtt_data = []
for d in s[1].items():
payload = json.dumps({"date": reception_date,
"value": d[1],
"unit": data_unit[d[0]],
"type": data_type[d[0]]})
mqtt_data.append({'topic': "{}/{}".format(mqtt_topic, s[0]),
'payload': payload,
'qos': 2,
'retain': True})
pprint.pprint(mqtt_data)
mqtt.multiple(mqtt_data,
hostname= mqtt_host ,
port= mqtt_port ,
client_id= mqtt_client_id ,
auth= mqtt_auth )
print(payload)
mqtt.single("{}/{}".format(mqtt_topic, s[0]),
payload= payload ,
qos= 2 ,
retain= True ,
hostname= mqtt_host ,
port= mqtt_port ,
client_id= mqtt_client_id ,
auth= mqtt_auth )