Python paho mqtt客户端不会同时发布和订阅
Python paho mqtt client won't publish and subscribe at the same time
当我通过 MQTTLens 测试发布时,它有效。然而,当我按下一个按钮时,它确实触发了 "on_publish" 但在另一端的 on_message 没有收到任何东西;它没有被触发。
有两个Raspberry Pi的运行相同的脚本,唯一的区别是他们的经纪人IP和主题是相反的。
import RPi.GPIO as io
import os
import json
from time import sleep
import paho.mqtt.client as mqtt
import paho.mqtt.publish as publish
############### MQTT section ##################
Broker = "192.168.1.10"
rcv_topic = "home/groundfloor/livingroom/lights/lightx" # receive messages on this topic
snd_topic = "home/groundfloor/kitchen/lights/lightx" # send messages to this topic
def on_connect(mqttc, obj, flags, rc):
print("rc: "+str(rc))
mqttc.subscribe(rcv_topic) #receving/subscriber
#when receving a message:
def on_message(mqttc, obj, msg):
print("sub") #this is not being executed on button push, but it is when I publish through the MQTTLens
print(msg.topic+" "+str(msg.qos)+" "+str(msg.payload))
try:
p = msg.payload.decode("utf-8")
print("decoded payload: " + p)
x = json.loads(p)
set_leds(leds, tuple(x['leds'])) #set leds to received value
return
except Exception as e:
print(e)
# callback functie voor publish event
def on_publish(mqttc, obj, mid):
print("pub")
return
mqttc = mqtt.Client()
mqttc.on_message = on_message
mqttc.on_connect = on_connect
mqttc.on_publish = on_publish
mqttc.on_subscribe = on_subscribe
mqttc.connect(Broker, 1883, 60) #last could be a port too
mqttc.loop_start() #client.loop_forever()
############### led&button section ##################
def init_leds(leds):
io.setup(leds, io.OUT)
def set_leds(leds, states):
print("leds and states: " + str(leds) + " " + str(states))
io.output(leds, states)
def snd_msg(led):
dataToSend=json.dumps({"leds":[led1State,led2State]})
print("data: " + dataToSend)
mqttc.publish(snd_topic, dataToSend)
io.add_event_detect(btn1,io.FALLING,callback=lambda *a: snd_msg(1),bouncetime=500)
############### main ##################
def main():
try:
while True:
init_leds(leds)
except KeyboardInterrupt:
pass
finally:
io.cleanup()
#toplevel script
#below will only execute if ran directly - above is always accessible
if __name__ == '__main__':
main()
我只包含了我的代码中与我的问题直接相关的部分,并修改了一些代码以使其更短。但是,如果需要更多代码,我可以随时提供。
我意识到这可能是这个 的副本,但我已经尝试从答案中推导出我的代码,但它似乎并没有解决我的问题,除非我做错了什么.
正如 hardillb 所建议的(我不确定如何标记某人),我的错误是我使用不同的 IP 作为每个 Rpi 的代理。他们现在都在听 1.10 并且它有效.
You mentioned swapping the broker ips, surely they should both be
pointing at the same broker? – hardillb
您似乎在两个不同的 IP 上发布和订阅。
为了接收消息,您必须在 IP 192.168.1.10(我假设这是您的 Broker IP)上发布主题 TOPIC_TEST(假设)并在 IP 192.168.1.10 上订阅同一主题 TOPIC_TEST.
当我通过 MQTTLens 测试发布时,它有效。然而,当我按下一个按钮时,它确实触发了 "on_publish" 但在另一端的 on_message 没有收到任何东西;它没有被触发。 有两个Raspberry Pi的运行相同的脚本,唯一的区别是他们的经纪人IP和主题是相反的。
import RPi.GPIO as io
import os
import json
from time import sleep
import paho.mqtt.client as mqtt
import paho.mqtt.publish as publish
############### MQTT section ##################
Broker = "192.168.1.10"
rcv_topic = "home/groundfloor/livingroom/lights/lightx" # receive messages on this topic
snd_topic = "home/groundfloor/kitchen/lights/lightx" # send messages to this topic
def on_connect(mqttc, obj, flags, rc):
print("rc: "+str(rc))
mqttc.subscribe(rcv_topic) #receving/subscriber
#when receving a message:
def on_message(mqttc, obj, msg):
print("sub") #this is not being executed on button push, but it is when I publish through the MQTTLens
print(msg.topic+" "+str(msg.qos)+" "+str(msg.payload))
try:
p = msg.payload.decode("utf-8")
print("decoded payload: " + p)
x = json.loads(p)
set_leds(leds, tuple(x['leds'])) #set leds to received value
return
except Exception as e:
print(e)
# callback functie voor publish event
def on_publish(mqttc, obj, mid):
print("pub")
return
mqttc = mqtt.Client()
mqttc.on_message = on_message
mqttc.on_connect = on_connect
mqttc.on_publish = on_publish
mqttc.on_subscribe = on_subscribe
mqttc.connect(Broker, 1883, 60) #last could be a port too
mqttc.loop_start() #client.loop_forever()
############### led&button section ##################
def init_leds(leds):
io.setup(leds, io.OUT)
def set_leds(leds, states):
print("leds and states: " + str(leds) + " " + str(states))
io.output(leds, states)
def snd_msg(led):
dataToSend=json.dumps({"leds":[led1State,led2State]})
print("data: " + dataToSend)
mqttc.publish(snd_topic, dataToSend)
io.add_event_detect(btn1,io.FALLING,callback=lambda *a: snd_msg(1),bouncetime=500)
############### main ##################
def main():
try:
while True:
init_leds(leds)
except KeyboardInterrupt:
pass
finally:
io.cleanup()
#toplevel script
#below will only execute if ran directly - above is always accessible
if __name__ == '__main__':
main()
我只包含了我的代码中与我的问题直接相关的部分,并修改了一些代码以使其更短。但是,如果需要更多代码,我可以随时提供。
我意识到这可能是这个
正如 hardillb 所建议的(我不确定如何标记某人),我的错误是我使用不同的 IP 作为每个 Rpi 的代理。他们现在都在听 1.10 并且它有效.
You mentioned swapping the broker ips, surely they should both be pointing at the same broker? – hardillb
您似乎在两个不同的 IP 上发布和订阅。 为了接收消息,您必须在 IP 192.168.1.10(我假设这是您的 Broker IP)上发布主题 TOPIC_TEST(假设)并在 IP 192.168.1.10 上订阅同一主题 TOPIC_TEST.