paho-mqtt 发布-订阅无法在 Python 中的单独文件中工作

paho-mqtt publish-subscribe not working from separate files in Python

我有两个单独的文件用于发布和订阅以测试 mqtt 协议。我维护一个配置文件来存储常用参数,如 client_idbroker name 和 topics.

config.py

broker = "iot.eclipse.org"

# Random alphanumeric string
uid = "id-sfgjrs45ys4jQPlk"

appliances = {
    "room1": [
        "appliance1",
        "appliance2",
        "appliance3",
        "appliance4",
        "appliance5"
    ],
    "room2": [
        "appliance1",
        "appliance2",
        "appliance3"
    ]
}

subscriber.py

import time
import paho.mqtt.client as mqtt
import config

broker = config.broker
client_id = config.uid

topics = list()
for room, appliances in config.appliances.items():
    for appliance in appliances:
        topics.append(room + "/" + appliance)

def on_message(client, userdata, message):
    print("Topic: " + message.topic)
    print("Message: " + message.payload.decode('utf-8'))

client = mqtt.Client(client_id=client_id)
client.on_message = on_message

print("Connecting to broker... " + broker)
client.connect(broker)

print("Subscribing ")
for topic in topics:
    client.subscribe(topic)

print("Listening")
client.loop_forever()

publisher.py

import time
import paho.mqtt.client as mqtt
import config

broker = config.broker
client_id = config.uid

topic = "room1/appliance1"
message = "off"

client = mqtt.Client(client_id=client_id)

print("Connecting to broker... " + broker)
client.connect(broker)

print("Publishing ")
client.publish(
    topic=topic,
    payload=message
)
time.sleep(1)
client.disconnect()

如果 subscriber.pypublisher.py 保存在同一个文件中,它们似乎工作正常,喜欢 :

client.loop_start()
client.publish(
    topic=topic,
    payload=message
)
time.sleep(10)
client.loop_stop()

以下是两者执行时的日志:

登录publisher.py

(运行 而 subscriber.py 是 运行)

Connecting to broker... iot.eclipse.org
Publishing 
Sending PUBLISH (d0, q0, r0, m1), 'b'room1/appliance1'', ... (3 bytes)
Sending DISCONNECT

登录subsciber.py

Connecting to broker... iot.eclipse.org
Sending CONNECT (u0, p0, wr0, wq0, wf0, c1, k60) client_id=b'id-sfgjrs45ys4jQPlk'
Subscribing 
Sending SUBSCRIBE (d0, m1) [(b'room1/appliance1', 0)]
Sending SUBSCRIBE (d0, m2) [(b'room1/appliance2', 0)]
Sending SUBSCRIBE (d0, m3) [(b'room1/appliance3', 0)]
Sending SUBSCRIBE (d0, m4) [(b'room1/appliance4', 0)]
Sending SUBSCRIBE (d0, m5) [(b'room1/appliance5', 0)]
Sending SUBSCRIBE (d0, m6) [(b'room2/appliance1', 0)]
Sending SUBSCRIBE (d0, m7) [(b'room2/appliance2', 0)]
Sending SUBSCRIBE (d0, m8) [(b'room2/appliance3', 0)]
Listening
Received CONNACK (0, 0)
Received SUBACK
Received SUBACK
Received SUBACK
Received SUBACK
Received SUBACK
Received SUBACK
Received SUBACK
Received SUBACK
Sending PINGREQ
Received PINGRESP
Sending PINGREQ
Received PINGRESP
Sending CONNECT (u0, p0, wr0, wq0, wf0, c1, k60) client_id=b'id-sfgjrs45ys4jQPlk'
Received CONNACK (0, 0)

我不知道我做错了什么以至于订阅者听不到我的消息。

这是因为您正尝试从具有相同客户端 ID uid 的两个不同代码进行连接。

根据规范here

Each Client connecting to the Server has a unique ClientId.

尝试使用不同的客户端 ID,代码应该可以正常工作