Problem with subscription to MQTT Broker
-
Hi all!
I added a subscribtion to my MQTT Broker (Mosquitto on a RPi3) to receive some timing settings. Did it all straight forward like in the example, no fancy things.
Unfortunately, the callback function is only triggered once, right after subscribe. Here's a little snippet:def cb_Get_Settings(topic, msg): print("+++++++++++++++++++++++++++++++++\n" * 2) print(topic, ' : ', msg) print("+++++++++++++++++++++++++++++++++\n" * 2) def start_wlan(): global wlan global mqtt wlan = WLAN(mode=WLAN.STA, power_save=False) wlan.connect(config.WIFI_SSID, auth=(WLAN.WPA2, config.WIFI_PASS), timeout=5000) print("Try to connect to Wifi...", end='') wifi_ok = False for _ in range (10): if wlan.isconnected(): wifi_ok = True break else: time.sleep(1) if wifi_ok: #pycom.rgbled(0x550000) print("Done!!!") wifiaddress = "IP Address is: " + ((str(wlan.ifconfig()[:1]))[2:])[:-3] print(wifiaddress) print("Connect to MQTT Broker at " + config.MQTT_BROKER + "...", end='') mqtt = MQTTClient(config.UNIQUE_ID, config.MQTT_BROKER, keepalive=1, port=config.MQTT_PORT) mqtt.settimeout = settimeout mqtt.set_last_will(config.LAST_WILL_TOPIC, config.LAST_WILL_MSG, retain = True, qos = 1) mqtt.set_callback(cb_Get_Settings) mqtt.connect(clean_session=False) mqtt.publish(config.LAST_WILL_TOPIC, config.LAST_WILL_BACK, retain=True, qos=1) print("Done!!!") print("Subscribe to Settings Topic...") subTopic = config.MQTT_DEVICE_NAME +"/settings/get" mqtt.subscribe(topic=subTopic, qos=1) print("Done!!!") else: maschine.reset()
When ever I send I message to the topic I subscribed to, I get nothing.
I tried to usemqtt.check_msg()
on top my main loop. Then I receive the messages, but check_msg blocks the main loop, so everything inside the main loop is only executed if I publish a message to the subscribed topic.
What I now wondering is something in the official Pycom MQTT library.
Snipp---------# Checks whether a pending message from server is available. # If not, returns immediately with None. Otherwise, does # the same processing as wait_msg. def check_msg(self): self.sock.setblocking(False) return self.wait_msg()
Is there really any check done? For me, it just runs
wait_msg()
, isn't it?Cheers,
Thomas
-
Ok, some news:
If I publish something to any of my topics with QoS set to 1, I can receive the message from the topic I subsrcibed to, but, unfortunately, only one time. The callback function runs fine, but then, the main loop is blocked.
I can see, that the sensor threads are still running fine, but there are no more data published, because of the blocked main loop.Cheers,
Thomas