Welcome to the Pycom forum Forum
    • \[\[global:header.categories\]\]
    • \[\[global:header.recent\]\]
    • \[\[global:header.tags\]\]
    • \[\[global:header.popular\]\]
    • Login

    Explore Pybytes | Official Documentation | Report a Firmware Bug/Issue | GitHub

    MAJOR ISSUE: MQTT library blocking when Subscribe

    Scheduled Pinned Locked Moved Issues & Bugs
    5 Posts 5 Posters 447 Views 6 Watching
    Loading More Posts
    • Oldest to Newest
    • Newest to Oldest
    • Most Votes
    Reply
    • Reply as topic
    Log in to reply
    This topic has been deleted. Only users with topic management privileges can see it.
    • G Offline
      GeirFrimann
      last edited by GeirFrimann

      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!

      1 Reply Last reply Reply Quote 0
      • E Offline
        Eric Ang
        last edited by

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

        1 Reply Last reply Reply Quote 2
        • S Offline
          Siridar
          last edited by

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

          O 1 Reply Last reply Reply Quote 0
          • O Offline
            Osquillar @Siridar
            last edited by

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

            1 Reply Last reply Reply Quote 0
            • S Offline
              saber phung
              last edited by

              Hello, Did you find the solution? I faced with the same issue

              1 Reply Last reply Reply Quote 0

              Hello! It looks like you're interested in this conversation, but you don't have an account yet.

              Getting fed up of having to scroll through the same posts each visit? When you register for an account, you'll always come back to exactly where you were before, and choose to be notified of new replies (either via email, or push notification). You'll also be able to save bookmarks and upvote posts to show your appreciation to other community members.

              With your input, this post could be even better 💗

              Register Login
              • 1 / 1
              • First post
                Last post

              Pycom on Twitter

              Pycom