如何更改 qos 级别和消息保留?
How do I change the qos level and message retain?
您好,我在 python 中有一些 mqtt publish/subscribe 的代码。我想更改 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
谢谢
来自帕霍Pythondocs
PUBLISH()
publish(topic, payload=None, qos=0, retain=False)
This causes a message to be sent to the broker and subsequently from
the broker to any clients subscribing to matching topics. It takes the
following arguments:
- topic the topic that the message should be published on
- payload the actual message to send. If not given, or set to None a zero length message will be used. Passing an int or float will result
in the payload being converted to a string representing that number.
If you wish to send a true int/float, use struct.pack() to create the
payload you require
- qos the quality of service level to use
- retain if set to True, the message will be set as the “last known good”/retained message for the topic.
要将 QOS 设置为 2 并将保留标志设置为 true,请将发布行更改为以下内容:
client.publish("microscope/light_sheet_microscope/laser","Hello World Im a laser!",2,True)
您好,我在 python 中有一些 mqtt publish/subscribe 的代码。我想更改 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
谢谢
来自帕霍Pythondocs
PUBLISH()
publish(topic, payload=None, qos=0, retain=False)
This causes a message to be sent to the broker and subsequently from the broker to any clients subscribing to matching topics. It takes the following arguments:
- topic the topic that the message should be published on
- payload the actual message to send. If not given, or set to None a zero length message will be used. Passing an int or float will result in the payload being converted to a string representing that number. If you wish to send a true int/float, use struct.pack() to create the payload you require
- qos the quality of service level to use
- retain if set to True, the message will be set as the “last known good”/retained message for the topic.
要将 QOS 设置为 2 并将保留标志设置为 true,请将发布行更改为以下内容:
client.publish("microscope/light_sheet_microscope/laser","Hello World Im a laser!",2,True)