I am running a traffic light (pi-stop) solution for my project integrating with MQTT. So the publisher will sent the number of counting to the subscriber and the subscriber will control and change the traffic lights. However, I am unable to sent any counting out of the publisher. Pls advice, Thanks
MQTT_pub.py (publisher)
import paho.mqtt.client as mqtt
import paho.mqtt.publish as publish
sub_topic = "light" #recieve message on this topic
pub_topic = "light" #send message to this topic
Broker = "127.0.1.1"
# when connecting to mqtt do this;
def on_connect(client, userdata, flags, rc):
print("Connected with result code "+str(rc))
client.subscribe(sub_topic)
# when receiving a mqtt message do this;
def on_message(client, userdata, msg):
message = str(msg.payload)
print(msg.topic+" "+message)
# publish_mqtt("got your message")
# to send a message
def publish_mqtt(count):
mqttc = mqtt.Client("counting")
mqttc.connect(Broker, 1883)
mqttc.publish(pub_topic, "10")
#mqttc.loop(5) //timeout = 5s
def on_publish(mosq, obj, mid):
print("mid: " + str(mid))
client = mqtt.Client()
client.on_connect = on_connect
client.on_message = on_message
client.connect(Broker, 1883, 60)
publish_mqtt(0)
MQTT.py (subscriber)
import paho.mqtt.client as mqtt
import paho.mqtt.publish as publish
import TrafficKit03
sub_topic = "light" #recieve message on this topic
pub_topic = "light" #send message to this topic
Broker = "127.0.0.1"
# when connecting to mqtt do this;
def on_connect(client, userdata, flags, rc):
print("Connected with result code "+str(rc))
client.subscribe(sub_topic)
# when receiving a mqtt message do this;
def on_message(client, userdata, msg):
message = str(msg.payload)
print(msg.topic+" "+message)
# decide on traffic counting (if 5 display.. if 0 display..)
publish_mqtt(count)
TrafficKit03.call("TrafficKit03.py")
def on_subscribe(client, userdata, mid, gqos):
print("subscribed: " + gpos)
pass
def publish_mqtt(count):
mqttc = mqtt.Client("counting")
mqttc.connect(Broker, 1883)
mqttc.publish(pub_topic, "10")
#mqttc.loop(5) //timeout = 5s
client = mqtt.Client()
client.on_connect = on_connect
client.on_subscribe = on_subscribe
client.on_message = on_message
client.connect(Broker, 1883, 60)
client.loop_start()
As @Tagic said you need to remove the extra mqtt.Client()
instances
Edit both files to remove the extra connection in the publish_mqtt
function
def publish_mqtt(count):
#mqttc = mqtt.Client("counting")
#mqttc.connect(Broker, 1883)
mqttc.publish(pub_topic, "10")
#mqttc.loop(5) //timeout = 5s
In the MQTT_pub.py file you should also move the call to publish_mqtt
to the on_connect
function:
def on_connect(client, userdata, flags, rc):
print("Connected with result code "+str(rc))
publish_mqtt(0)
and start the network loop
client.connect(Broker, 1883, 60)
client.loop_start()
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.