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):
       pass
    
    def sub_cb(topic, msg):
      print(msg)
    
    wlan = WLAN(mode=WLAN.STA)
    wlan.antenna(WLAN.EXT_ANT)
    wlan.connect(config.WLAN_SSID, auth=(WLAN.WPA2, config.WLAN_Password), timeout=5000)
    
    while not wlan.isconnected():  
       machine.idle()
       
    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
    client.connect()
    print("Connected to MQTT Broker\n")
    
    client.set_callback(sub_cb)
    client.subscribe(topic="v1/devices/me/attributes")
    time.sleep(1)
    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\"}")
       time.sleep(1)
       print("Sending OFF")
       client.publish(topic="v1/devices/me/telemetry", msg="{\"d\":\"OFF\"}")
       client.check_msg()
       time.sleep(1)
    

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

    b'{"AlwaysSendData":true}'
    b'{"AlwaysSendData":false}
    

    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/main.py", line 35, in <module>
      File "/flash/lib/mqtt.py", line 142, in subscribe
      File "/flash/lib/mqtt.py", 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!



  • 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