我採取的是聽一個特定的主題,當一個新的消息由我ESP8266公佈的反應,因此程序。當收到來自ESP8266的新消息時,我的程序將觸發回調並執行一組任務。我在回調函數中發佈了兩條消息,回到了Arduino正在聽的話題。但是,只有在函數退出後纔會發佈消息。Python的泛美衛生組織MQTT:無法立即發佈在功能
謝謝你提前的所有時間。
我曾嘗試使用環路(1)與所述回調函數內1秒的超時。該程序會立即發佈消息,但它似乎陷入循環。有人能給我一些指針,我怎樣才能在我的回調函數中立即執行每個發佈函數,而不是整個回調完成並返回到主loop_forever()?
import paho.mqtt.client as mqtt
import subprocess
import time
# The callback for when the client receives a CONNACK response from the server.
def on_connect(client, userdata, flags, rc):
print("Connected with result code "+str(rc))
# Subscribing in on_connect() means that if we lose the connection and
# reconnect then subscriptions will be renewed.
client.subscribe("ESP8266")
# The callback for when a PUBLISH message is received from the server.
def on_message(client, userdata, msg):
print(msg.topic+" "+str(msg.payload))
client.publish("cooking", '4')
client.loop(1)
print("Busy status published back to ESP8266")
time.sleep(5)
print("Starting playback.")
client.publish("cooking", '3')
client.loop(1)
print("Free status published published back to ESP8266")
time.sleep(5)
print("End of playback.")
client = mqtt.Client()
client.on_connect = on_connect
client.on_message = on_message
client.connect("192.168.1.9", 1883, 60)
#client.loop_start()
# Blocking call that processes network traffic, dispatches callbacks and
# handles reconnecting.
# Other loop*() functions are available that give a threaded interface and a
# manual interface.
client.loop_forever()
你不應該在回調中調用loop()。 – ralight
謝謝你的回覆。對此,我真的非常感激!我認爲我與on_message回調的目的有些混淆。 – Zen