Why is "Subscribing to topic topic/test" printed in console log before connect message?
Spencer Du
spencerdu at hotmail.co.uk
Tue Sep 10 12:59:27 EDT 2019
Hi
I have code for publish and subscribe over mqtt. In the console log I have "Subscribing to topic topic/test" printed before connect message why is this? I want to this to be printed after the connect message. How do I fix this problem. Please run gui.py to test.
gui.py
import paho.mqtt.client as mqtt
from mqtt import *
import json
import time
client = mqtt.Client()
client.connect("broker.hivemq.com",1883,60)
client.on_connect = on_connect
client.on_message = on_message
client.loop_start()
print("Subscribing to topic", "topic/test")
client.subscribe("topic/test")
client.publish("topic/test", "Hello world!")
time.sleep(1)
client.loop_stop()
mqtt.py
import paho.mqtt.client as mqtt
def on_connect(client, userdata, flags, rc):
print("Connected with result code "+str(rc))
def on_message(client, userdata, msg):
if msg.payload.decode() == "Hello world!":
print("Yes!")
Thanks
Spencer
More information about the Python-list
mailing list