0

Trying to print out a single temperature value when I receive a MQTT message. Whats happening at the moment is that only the full MQTT message is printed but not the value. "Test" is also not printed.

Code:

import context
import paho.mqtt.client as mqtt
from influxdb import InfluxDBClient
import json

def on_message(mqttc, obj, msg):
    print(msg.payload)
    stuff = json_dumps(str(msg.payload))
    print(msg.payload['HTU21']['Temperature'])
    print ("Test")

Output:

{"Time":"2018-12-15T03:02:34","HTU21":{"Temperature":18.8,"Humidity":71.2},"TempUnit":"C"}

1 Answer 1

0

It's stopping because it's throwing an error (and the MQTT library is catching and hiding the error) when you try to access fields in the msg.payload (which is still a string).

If you use the stuff json object then it should work.

def on_message(mqttc, obj, msg):
    print(msg.payload)
    stuff = json_dumps(str(msg.payload))
    print(stuff['HTU21']['Temperature'])
    print ("Test")

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.