Running up to date Stretch on a Pi2B.
Running MQTT mosquito broker.
Running a python app that connects ok to the broker.
Running a single ESP8266 that also connects ok to the broker on the Pi. (All on a local network).
Controlling an output on the ESP8266 from the Pi python app is all ok.
I wanted to add a second ESP8266. So, I gave it a different username and password and set up 2 passwords in the mosquito pwfile.
It also connects OK to the broker.
But now, I can't control either of them from my Python app on the Pi.
I am new to this. What is the correct way to connect 2 or more devices/clients to be controlled via the python app on the Pi.
Search the World Wise Web has left me a little bit bamboozled.
This is a cut down version of my code showing my connection to the broker and the 2 messages I send to the 2 different ESP's.
Any help much appreciated. Thanks.
Code: Select all
import paho.mqtt.client as mqtt import threading def handleMQTT() : # Once we have told the client to connect, let the client object run itself client.loop_forever() def handleMQTT1() : # Once we have told the client to connect, let the client object run itself client1.loop_forever() mqtt_username = "esp8266" mqtt_username1 = "esp8266_1" # Same for both mqtt_password = "pwd" mqtt_topic_from_esp = "topicfrom" mqtt_topic_to_esp = "topicto" # This is the ip address of THIS Raspberry Pi (which is acting as the BROKER) mqtt_broker_ip = "192.168.0.35" client = mqtt.Client() # Set the username and password for the MQTT client client.username_pw_set(mqtt_username, mqtt_password) client1 = mqtt.Client() # Set the username and password for the MQTT client client1.username_pw_set(mqtt_username1, mqtt_password) client.on_connect = on_connect client.on_message = on_message client1.on_connect = on_connect1 client1.on_message = on_message1 # Once everything has been set up, we can (finally) connect to the broker # 1883 is the listener port that the MQTT broker is using client.connect(mqtt_broker_ip, 1883) client1.connect(mqtt_broker_ip, 1883) t2 = threading.Thread(target=handleMQTT) t2.daemon = True t2.start() t3 = threading.Thread(target=handleMQTT1) t3.daemon = True t3.start() # And I eventually call these when things need to happen... client.publish(mqtt_topic_to_esp, "0 DEV/OFF") client1.publish(mqtt_topic_to_esp, "0 DEV1/OFF")