我看不到任何发布消息

时间:2019-07-09 23:18:16

标签: python mqtt paho

嗨,我有一些mqtt python代码。我想发布和订阅消息,但是以下代码不输出任何发布或订阅消息。如何修复代码,以便输出发布和订阅消息?任何帮助将不胜感激。

import context  # Ensures paho is in PYTHONPATH
import paho.mqtt.client as mqtt

class MyMQTTClass(mqtt.Client):

    def on_connect(self, mqttc, obj, flags, rc):
        print("rc: "+str(rc))

    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, obj, level, string):
        print(string)

    def run(self):
        self.connect("m2m.eclipse.org", 1883, 60)

        rc = 0
        while rc == 0:
            rc = self.loop()
        return rc

# If you want to use a specific client id, use
# mqttc = MyMQTTClass("client-id")
# but note that the client id must be unique on the broker. Leaving the client
# id parameter empty will generate a random id for you.
mqttc = MyMQTTClass()
rc = mqttc.run()
print("rc: "+str(rc))

broker_address="broker.hivemq.com"
#broker_address="iot.eclipse.org"
print("creating new instance")
client = mqtt.Client("P1") #create new instance
client.on_message=on_message #attach function to callback
print("connecting to broker")
client.connect(broker_address) #connect to broker
client.loop_start() #start the loop
print("Subscribing to topic","house/bulbs/bulb1")
client.subscribe("house/bulbs/bulb1")
print("Publishing message to topic","house/bulbs/bulb1")
client.publish("house/bulbs/bulb1","OFF")
time.sleep(4) # wait
client.loop_stop() #stop the loop

输出:

Sending CONNECT (u0, p0, wr0, wq0, wf0, c1, k60) client_id=b''
Received CONNACK (0, 0)
rc: 0

1 个答案:

答案 0 :(得分:0)

尝试此代码。添加了连接后的等待时间,以便有时间在发布之前建立连接。另外,我认为您可以使用iot.eclipse.org或broker.hivemq.com中的任何一个经纪人

import paho.mqtt.client as mqtt
import time
class MyMQTTClass(mqtt.Client):
    def on_connect(self, mqttc, obj, flags, rc):
        print("rc: "+str(rc))
        print("Subscribing to topic","house/bulbs/bulb1")
        mqttc.subscribe("house/bulbs/bulb1")

    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 run(self):
        self.connect("broker.hivemq.com", 1883, 60)

print("creating new instance")
client = MyMQTTClass()
client.run()

client.loop_start() #start the loop
time.sleep(2)
print("Publishing message to topic","house/bulbs/bulb1")
client.publish("house/bulbs/bulb1","OFF")
time.sleep(2) # wait
client.loop_stop() #stop the loop