简体   繁体   English

Python 中的 MQTT,订阅者无响应

[英]MQTT in Python, subscriber not responding

Following this great tutorial on implementing MQTT in Python.按照这篇关于在 Python 中实现 MQTT 的精彩教程 Works fine when I run the publisher script but hangs on the subscriber script.当我运行发布者脚本但挂在订阅者脚本上时工作正常。 I am running both scripts at the same time in separate command lines.我在不同的命令行中同时运行这两个脚本。 Here are both my codes:这是我的两个代码:

# pub.py # pub.py

import paho.mqtt.client as mqtt
import logging
import json
import time
import random
logging.basicConfig(level=logging.INFO)
# use DEBUG, INFO, WARNING

username="xxxxx"
password="xxxxx"
broker_url ='xxxxx'
broker_port=0000
client_id=f"client-{random.randint(0, 100)}"
to_wait=5
topic='hey-hey'
publish_count = 3
msg= 'Hey'

def on_log(client, userdata, level, buf):
    logging.info(buf)
    client.on_log = on_log

def connect_mqtt():
    def on_connect(client, userdata, flags, rc):
        if rc == 0:
            logging.info("Connected to Broker!: {}".format(rc))
        else:
            logging.info("Failed to Connect with code: "+str(rc))
    
    client = mqtt.Client(client_id)
    client.username_pw_set(username, password)
    client.on_connect = on_connect
    client.connect(broker_url, broker_port)
    return client

def on_publish(client):
    count = 1
    while count <= publish_count:
        time.sleep(to_wait)
        message = str(msg)
        result = client.publish(topic, message, 1, 1)
        status = result[0]
        if status == 0:
            published= client_id+ ' sent the message: ' +message)
            print(published)
        else:
            print(f"Failed to send the message")
        count +=1
        
def run():
    device1 = connect_mqtt()
    device1.loop_start
    on_publish(device1)
    device1.loop_stop()
    
if __name__ == '__main__':
    run()

And I get:我得到:

client-27 sent the message: Hey
client-27 sent the message: Hey
client-27 sent the message: Hey

However, for sub.py :但是,对于sub.py

import paho.mqtt.client as mqtt
import logging
import json
import time
import random
logging.basicConfig(level=logging.INFO)
from datetime import datetime 
# use DEBUG, INFO, WARNING

username="xxxx"
password="xxxxxx"
broker_url ='xxxxx'
broker_port=0000
client_id=f"client-{random.randint(0, 100)}"
to_wait=5
topic='hey-hey'
publish_count = 3

def on_log(client, userdata, level, buf):
    logging.info(buf)
    client.on_log = on_log

def connect_mqtt():
    def on_connect(client, userdata, flags, rc):
        if rc == 0:
            logging.info("Connected to Broker!: {}".format(rc))
        else:
            print("Failed to Connect with code: "+str(rc))
            client.loop_stop()
    
    client = mqtt.Client(client_id)
    client.username_pw_set(username, password)
    client.on_connect = on_connect
    client.connect(broker_url, broker_port, 60)
    return client

def subscribe(client):
    def on_subscribe(client,userdata, mid, granted_qos):
        logging.info('subscribed')
        
    client.subscribe(topic)
    client.subscribe = on_subscribe


def process_message(client, userdata, message):
    msgr=str(message.payload.decode('utf-8'))
    msgr='Message Received' + msgr
    logging.info(msgr)
        
def run():
    device2 = connect_mqtt()
    device2.on_message = process_message
    device2.loop_forever()
    
    
if __name__ == '__main__':
    run()

It just hangs and times out.它只是挂起并超时。 Am I missing something?我错过了什么吗?

You never call subscribe() in your subscriber code, so it never tells the broker what topics it wants to receive.您永远不会在订阅者代码中调用subscribe() ,因此它永远不会告诉代理它想要接收哪些主题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM