I am new to mqtt and still discovering this interesting protocol. I want to create a client in python that publishes every 10 seconds a message. Until now I succeed to publish only one message and keep the client connected to the broker.
How can I make the publishing part a loop ?
Below is my client:
import mosquitto
mqttc=mosquitto.Mosquitto("ioana")
mqttc.connect("127.0.0.1",1884,60,True)
mqttc.publish("test","Hello")
mqttc.subscribe("test/", 2)
while mqttc.loop() == 0:
pass
Thanks.
I would suggest:
import paho.mqtt.client as mqtt # mosquitto.py is deprecated
import time
mqttc = mqtt.Client("ioana")
mqttc.connect("127.0.0.1", 1883, 60)
#mqttc.subscribe("test/", 2) # <- pointless unless you include a subscribe callback
mqttc.loop_start()
while True:
mqttc.publish("test","Hello")
time.sleep(10)# sleep for 10 seconds before next call
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With