MQTT message to JSON issues
-
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:
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:
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)
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'
Both cases gives the error in the first line of the updateProject function where setting the first labelTypeError: 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
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.
-
Hello @travisstdenis
have you tried to use
decode
to convert bytes into a string (instead ofreplace
)? You'll finddecode
in theText
group.Thanks
Felix -
I solved it. See below:
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