🤖Have you ever tried Chat.M5Stack.com before asking??😎
    M5Stack Community
    • Categories
    • Recent
    • Tags
    • Popular
    • Users
    • Groups
    • Search
    • Register
    • Login

    MQTT message to JSON issues

    Scheduled Pinned Locked Moved UIFlow
    3 Posts 2 Posters 2.0k 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
      travisstdenis
      last edited by

      I'm attempting to send JSON data over MQTT and have the M5 load the different key values into labels. I haven't been successful trying it a few different ways. I think part of the problem is that a b' gets added to the beginning of both the topic an message values received by the MQTT software.

      For reference, here is the updateProject function called:
      updatge_project.png

      And here is the JSON string i'm sending over MQTT:

      { "id": 00004578, "shape": "hex", "total": 856, "cut1": 230, "cut2": 184, "cut3": 207, "cut4": 156, "cut5": 143, "clr1": 230, "clr2": 184, "clr3": 207, "clr4": 156, "clr5": 143 }
      

      First try was this way:
      simple_load.png
      This give me an error at the line of code in the updateProject function assigning the first label value to the appropriate key/value:

      TypeError: object with buffer protocol required
      

      The second way was to remove the leading b' that gets inserted (if anyone has a better way to do this, please lmk)
      remov_b_load.png
      This gives me an error at the line where it sets projDetails in the MQTT event:

      ValueError: syntax error in JSON
      

      The third and fourth way was trying the Dump JSON block alone and trying to remove the b'
      dump_simple.png
      dump_complex.png
      Both cases gives the error in the first line of the updateProject function where setting the first label

      TypeError: string indices must be integers, not str
      

      Then I tried just making the variable the data minus any JSON blocks and put it off to the function to do all the work
      no_json.png
      This give me an error at the line of code in the updateProject function assigning the first label value to the appropriate key/value:

      ValueError: syntax error in JSON
      

      Not sure how to proceed on this. Any help would be greatly appreciated.

      felmueF 1 Reply Last reply Reply Quote 0
      • felmueF Offline
        felmue @travisstdenis
        last edited by

        Hello @travisstdenis

        have you tried to use decode to convert bytes into a string (instead of replace)? You'll find decode in the Text group.

        Thanks
        Felix

        GPIO translation table M5Stack / M5Core2
        Information about various M5Stack products.
        Code examples

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

          I solved it. See below:

          solution.png

          First step was removing the b' at the beginning and ' at the end
          Then convert that to JSON with "loads json" block
          The "loads json" for the individual elements are redundant and you just pull straight from the json variable

          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
          • First post
            Last post