这是我的代码。无论我如何尝试,我都无法让回调函数正常工作。当客户端连接或收到消息时,什么也不会发生。我已经尝试了所有不同的循环选项。我能找到的都是我已经尝试过的东西。我已经尝试了网上找到的所有方法,但仍然不起作用。回调函数不会做任何事情。
import json
import time
import paho.mqtt.client as mqtt
# Callback function when the client connects to the MQTT broker
def on_connect(client, userdata, flags, rc):
print("Connected with result code " + str(rc))
# Subscribe to a topic when connected (optional)
client.subscribe("test/topic")
# Callback function when a message is received from the MQTT broker
def on_message(client, userdata, msg):
print("test")
print("Received message: " + msg.payload.decode())
client.disconnect()
# Create an MQTT client instance
client = mqtt.Client()
# Set the callback functions
client.on_connect = on_connect
client.on_message = on_message
# Connect to the MQTT broker (replace broker_address with your broker's IP/hostname)
client.connect("broker_adress", 1234, 60)
print("connected")
# Start the MQTT network loop to process incoming and outgoing messages
client.loop()
print("loop started")
client.subscribe("topic")
print("subscribed")
time.sleep(1)
# Publish a message to a topic (replace test/topic with your desired topic)
client.publish("topic", "Hello, MQTT!")
print("published")
# Wait for a few seconds to receive any messages (you can use time.sleep() for testing purposes)
# For a more robust implementation, you can use client.loop_forever() instead of time.sleep
time.sleep(2)
# Disconnect from the MQTT broker
client.loop_stop()```
根据文档,使用
client.loop()
时,您必须:
定期调用处理网络事件。
给出的例子是:
run = True
while run:
mqttc.loop()
因此
client.loop()
设计用于已经有循环的情况(例如检查传感器读数),并在每次迭代时调用 client.loop()
(意味着定期调用它)。
我认为您打算使用
client.loop_start()
(基于代码末尾对 client.loop_stop()
的调用)。进行此更改(将 client.loop()
替换为 client.loop_start()
)可能会解决您的问题。
另一种方法是将
client.subscribe("topic")
和 client.publish("topic", "Hello, MQTT!")
移至 on_connect
(subscribe
呼叫的推荐位置)并使用 client.loop_forever()
client.connect()
调用中使用的“broker_address”。确保它是您的 MQTT 代理的正确 IP/主机名和端口。client.loop()
代替 client.loop_start()
。这将为 MQTT 网络循环启动一个单独的线程,这可以帮助处理传入和传出的消息。client.connect()
后,添加一个短暂的延迟,以允许在继续之前建立连接。您可以使用 time.sleep(1)
等待一秒钟。client.loop_forever()
,而不是在几秒钟后手动停止循环。这将使客户端无限期地保持连接并连续处理传入的消息。
....
现在尝试一下这段代码...import json
import time
import paho.mqtt.client as mqtt
# Callback function when the client connects to the MQTT broker
def on_connect(client, userdata, flags, rc):
print("Connected with result code " + str(rc))
# Subscribe to a topic when connected (optional)
client.subscribe("test/topic")
# Callback function when a message is received from the MQTT broker
def on_message(client, userdata, msg):
print("test")
print("Received message: " + msg.payload.decode())
client.disconnect()
# Create an MQTT client instance
client = mqtt.Client()
# Set the callback functions
client.on_connect = on_connect
client.on_message = on_message
# Connect to the MQTT broker (replace broker_address with your broker's IP/hostname)
client.connect("broker_adress", 1234, 60)
print("connected")
# Start the MQTT network loop to process incoming and outgoing messages
client.loop_start()
print("loop started")
client.subscribe("topic")
print("subscribed")
time.sleep(1)
# Publish a message to a topic (replace test/topic with your desired topic)
client.publish("topic", "Hello, MQTT!")
print("published")
# Keep the client running and handling incoming messages indefinitely
client.loop_forever()
尝试运行此更新的代码,看看当您连接并接收消息时是否会触发回调函数。如果仍然存在任何问题,请确保您的 MQTT 代理设置正确并且可以通过您的设备进行访问。 另外,检查是否有任何可能阻止连接的潜在防火墙或安全配置。