MAJOR ISSUE: MQTT library blocking when Subscribe

  • When I'm running this simple code to connect to Thingsboard and subscribing to a topic,
    the code doesn't run passed the call to the subscribe method (line 35):

    from mqtt import MQTTClient
    from network import WLAN
    import machine
    import time
    import config
    def settimeout(duration):
    def sub_cb(topic, msg):
    wlan = WLAN(mode=WLAN.STA)
    wlan.connect(config.WLAN_SSID, auth=(WLAN.WPA2, config.WLAN_Password), timeout=5000)
    while not wlan.isconnected():  
    print("\nConnected to WiFi")
    client = MQTTClient(client_id=config.MQTT_DEVICE_ID, server=config.MQTT_BROKER_ADDRESS,user=config.MQTT_DEVICE_TOKEN, password="", port=config.MQTT_BROKER_PORT)
    client.settimeout = settimeout
    print("Connected to MQTT Broker\n")
    client.publish(topic="v1/devices/me/telemetry", msg="{\"d\":\"OFF\"}")
    while True:
       print("Sending ON")
       client.publish(topic="v1/devices/me/telemetry", msg="{\"d\":\"ON\"}")
       print("Sending OFF")
       client.publish(topic="v1/devices/me/telemetry", msg="{\"d\":\"OFF\"}")

    When triggering updates in Thingsboard, the device indeed gets the subscribed updates as shown in the terminal output:


    But the code never reach the while loop, looks like the subscribe method are blocking(!?)
    When idle for a few minutes, the code exits whit the following message:

    Traceback (most recent call last):
      File "/flash/", line 35, in <module>
      File "/flash/lib/", line 142, in subscribe
      File "/flash/lib/", line 161, in wait_msg
    OSError: -1
    Pycom MicroPython 1.18.2.r3 [v1.8.6-849-a1641ca] on 2019-02-28; FiPy with ESP32
    Type "help()" for more information.

    If I exclude the subscribe call, the loop suns as expected and updates reaches Thingsboard.

    Running similar code in Arduino IDE on a Wemos Lolin ESP32 device works just fine, so it's not an issue with Thingsboard.
    ANY help would be much appreciated to fix this issue, it's a showstopper for my (commercial) project!

  • @Siridar Hello, did you find the solution? I'm now at the same point

  • Hi, did this ever get resolved? I think I'm seeing the same issue...

  • Hi! Did you manage to resolve this issue? I'm facing it too and would appreciate if any kind soul can assist.

Log in to reply

Pycom on Twitter