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

    mqtt message directed to switc-case issue

    Scheduled Pinned Locked Moved Discussion
    pythonmqtt
    3 Posts 2 Posters 820 Views 2 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.
    • M Offline
      misterlisty
      last edited by

      It appears that the message from a mqtt subscription are bytes as when i print them, i get b'PON' for example. I have a switcher statement to execute a function based on the message but it appears to have issues with it being in bytes and i cannot decode it.

      def sub_cb(topic, msg):
      print("mqtt::",topic,"msg[",msg,"]")
      switch_demo(msg.decode('utf-8')) #doesn't recognise decode

      def PON():
      p_out.value(True)
      return "ON"

      def POFF():
      p_out.value(False)
      return "OFF"

      def switch_demo(argument):
      print("switch_demo::argument[",argument,"]")
      switcher = {
      'PON': PON,
      'POFF': POFF,
      }
      # Get the function from switcher dictionary
      func = switcher.get(argument, lambda: "Invalid month")
      # Execute the function
      print(func())

      L 1 Reply Last reply Reply Quote 0
      • L Offline
        livius @misterlisty
        last edited by livius

        @misterlisty
        are you sure that your problem is decode? - what is the error message?
        For me decode work without problem

        >>> a = b'PON'
        >>> print(a)
        b'PON'
        >>> b = a.decode("utf-8")
        >>> print(b)
        PON
        >>>
        

        please look at formating rules of the forum here
        http://commonmark.org/help/

        e.g. surround your code with triple ``` to make python code format

        think about people first, then computers

        1 Reply Last reply Reply Quote 0
        • M Offline
          misterlisty
          last edited by

          its working now, not sure why, maybe the file were out of sync

          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