我很难将2(或更多) ESP8266订阅到一个RPi3代理。我用:
import paho.mqtt.client as mqtt
import datetime
mqtt_topics = ["esp8266-1", "esp8266-2"]
def on_connect(client, userdata, flags, rc):
print("Connected with result code "+str(rc))
for topic in mqtt_topics:
client.subscribe(topic)
def on_message(client, userdata, msg):
print(datetime.datetime.now())
print(str(msg.topic)+" "+str(msg.payload))
client = mqtt.Client()
client.on_connect = on_connect
client.connect('localhost', 1883, 60)
try:
for topic in mqtt_topics:
client.on_message = on_message
client.loop_forever()
except KeyboardInterrupt:
print("CTRL-C: Terminating program.")它只起作用,尤其是1倍。如果我连接even 8266-1,它检查它的值,那么我将even 8266-2连接到它的值,并且even 8266-1不再可用(即使我转到even 8266-2)。
如何同时订阅两个esp8266?我只能在同一时间吃其中一个。
发布于 2018-08-01 18:57:42
代码几乎不需要修改:
import paho.mqtt.client as mqtt
import datetime
mqtt_topics = [("esp8266-1",0), ("esp8266-2",0)]
def on_connect(client, userdata, flags, rc):
print("Connected with result code "+str(rc))
client.subscribe(mqtt_topics)
def on_message(client, userdata, msg):
print(datetime.datetime.now())
print(str(msg.topic)+" "+str(msg.payload))
client = mqtt.Client()
client.on_connect = on_connect
client.on_message = on_message
client.connect('localhost', 1883, 60)
try:
client.loop_forever()
except KeyboardInterrupt:
print("CTRL-C: Terminating program.")但是,如果一个客户端工作,而另一个客户端强制第一个客户端断开连接,那么听起来您对两个客户端都有相同的clientID。客户端to需要对所有客户端都是唯一的。
https://stackoverflow.com/questions/51640220
复制相似问题