我正在实施一个程序,该程序可以收听特定主题并在我的 ESP8266 发布新消息时对其做出反应。当收到来自 ESP8266 的新消息时,我的程序将触发回调并执行一组任务。我在我的回调函数中发布两条消息回到 Arduino 正在侦听的主题。但是,只有在函数退出后才会发布消息。
提前感谢您的所有时间。
我尝试在回调函数中使用超时为 1 秒的 loop(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 …Run Code Online (Sandbox Code Playgroud)