【发布时间】:2019-12-09 19:46:15
【问题描述】:
您好,我有一些在 python 中用于 mqtt 发布/订阅的代码。我想更改 qos 值和消息保留,但我不知道如何更改,因为目前 qos 值始终只打印 0,我想将消息保留标志更改为 true 或 false 而不是 0。感谢所有帮助.
import paho.mqtt.client as mqtt
import time
class laser(mqtt.Client):
def on_connect(self, mqttc, obj, flags, rc):
print("rc: "+str(rc))
print("Subscribing to topic","microscope/light_sheet_microscope/laser")
mqttc.subscribe("microscope/light_sheet_microscope/laser")
def on_message(self, mqttc, userdata, message):
print("message received " ,str(message.payload.decode("utf-8")))
print("message topic=",message.topic)
print("message qos=",message.qos)
print("message retain flag=",message.retain)
def on_publish(self, mqttc, obj, mid):
print("mid: "+str(mid))
def on_subscribe(self, mqttc, obj, mid, granted_qos):
print("Subscribed: "+str(mid)+" "+str(granted_qos))
def on_log(self, mqttc, userdata, level, buf):
print("log: ",buf)
def run(self):
self.connect("broker.hivemq.com", 1883, 60)
print("creating new instance")
client = laser("Laser")
client.run()
client.loop_start() #start the loop
time.sleep(2)
print("Publishing message to topic","microscope/light_sheet_microscope/laser")
client.publish("microscope/light_sheet_microscope/laser","Hello World Im a laser!")
time.sleep(2) # wait
client.loop_stop() #stop the loop
谢谢
【问题讨论】:
标签: python-3.x mqtt paho