Reputation: 5
I'm having a problem with subscribing to MQTT broker (using test.mosquitto.org with port 1883) with paho mqtt library. When I use mosquitto_sub client, I'm getting messages normally, but when I subscribe to the same topic in my Python script, callback never gets executed.
import paho.mqtt.client as mqtt
client_get = mqtt.Client(client_id='my_client', protocol=mqtt.MQTTv31)
client_get.connect('test.mosquitto.org', 1883)
client_get.loop_start()
def callback(client, userdata, message):
print(str(message.payload.decode("utf-8")))
client_get.on_message = callback
client_get.subscribe(topic, qos=1)
Upvotes: 0
Views: 2746
Reputation: 59816
Try the following:
import paho.mqtt.client as mqtt
client_get = mqtt.Client(client_id='my_client', protocol=mqtt.MQTTv31)
client_get.connect('test.mosquitto.org', 1883)
def callback(client, userdata, message):
print(str(message.payload.decode("utf-8")))
client_get.on_message = callback
client_get.subscribe(topic, qos=1)
client_get.loop_forever()
I've moved the start_loop()
to the end and changed it to loop_forever()
which will block and keep the script running.
Upvotes: 2