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

    Problem with subscription to MQTT Broker

    Scheduled Pinned Locked Moved LoPy
    mqttsubscription
    2 Posts 1 Posters 460 Views 1 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.
    • T Offline
      Thosch42
      last edited by

      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 use mqtt.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

      1 Reply Last reply Reply Quote 0
      • T Offline
        Thosch42
        last edited by

        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

        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