我正在尝试使用此库中的 message_callback_add() 函数按主题捕获消息。下面是我用来处理我的 mqtt 订阅和发布需求的整个模块。我已经能够测试发布是否有效,但我似乎无法捕获任何传入消息。没有任何类型的警告/错误,并且这些print("position")
语句仅适用于 1 和 2。
import sys
import os
import time
import Things
import paho.mqtt.client as paho
global mqttclient;
global broker;
global port;
broker = "10.64.16.199";
port = 1883;
mypid = os.getpid()
client_uniq = "pubclient_"+str(mypid)
mqttclient = paho.Client(client_uniq, False) #nocleanstart
mqttclient.connect(broker, port, 60)
mqttclient.subscribe("Commands/#")
def Pump_callback(client, userdata, message):
#print("Received message '" + str(message.payload) + "' on topic '"
# + message.topic + "' with QoS " + str(message.qos))
print("position 3")
Things.set_waterPumpSpeed(int(message.payload))
def Valve_callback(client, userdata, message):
#print("Received message '" + str(message.payload) + "' on topic '"
# + message.topic + "' with QoS " + str(message.qos))
print("position 4")
Things.set_valvePosition(int(message.payload))
mqttclient.message_callback_add("Commands/PumpSpeed", Pump_callback)
mqttclient.message_callback_add("Commands/ValvePosition", Valve_callback)
print("position 1")
mqttclient.loop_start()
print("position 2")
def pub(topic, value):
mqttclient.publish(topic, value, 0, True)