MQTT实战

2020-12-22  本文已影响0人  沿哲

实验目的

用python内置的mqtt.client订阅通过edgexfoundry平台发送的数字

实验测试

客户端环境:

客户端一:

客户端二:

import paho.mqtt.client as mqtt
import json

def on_connect(client, userdata, flags, rc):
    print("Connected with result code: " + str(rc))

def on_message(client, userdata, msg):
    print(msg.topic + " " + msg.payload.decode("utf-8"))
    data=msg.payload.decode("utf-8")
    data=json.loads(data)
    print(data['readings'][0]['value'])

client = mqtt.Client()
client.on_connect = on_connect
client.on_message = on_message
client.connect("broker.mqttdashboard.com", 1883, 600) # 600为keepalive的时间间隔
client.subscribe('HYZ', qos=0)
client.loop_forever()

on_message函数两个print结果分别为:

HYZ {"id":"a54555be-976a-4361-9113-95a899698916","device":"Simple-Device01","origin":1608601777858066215,"readings":[{"id":"07e8ca6c-79ef-49c9-8c54-15dc7f263d4c","origin":1608601777858002476,"device":"Simple-Device01","name":"Command","value":"2222","valueType":"Int32"}]}

2222
上一篇下一篇

猜你喜欢

热点阅读