Receiving a JSON data

I am sending my whole raster data in JSON as

{“text” : “\x1B\x2A\x62\x34\x38\x57\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x1B\x2A\x62\x34\x38\x57”}

from my python server to Electric Imp Agent.

But when I am sending the same data to device and trying to print it. It is printing text instead of image.
But when printing the same data locally written in device, then I am successfully able to print.

So I guess the problem is from sending and receiving end while encoding or decoding. Is there an example where I can take a loot at it. Or what is the way to do it?

By digging more, I found a problem here which is:

While sending the data from python server as:


The data printed in agent server is as binary and the data is changed which is:

5b 50 52 4e 5d 3a 20 1b 2a 62 34 57 00 01 c2 80 00 1b 2a 62 34 57 00 01 c2 80 00

But the actual local raster data when I log is:

5b 50 52 4e 5d 3a 20 1b 2a 62 34 57 00 01 80 00 1b 2a 62 34 57 00 01 80 00

Please help what needs to be changed so that I can get the exact same data or if any type of encoding is required.

How are you handling the incoming JSON on the agent side? I would call http.jsondecode() to convert the incoming JSON (a string) to a Squirrel table, which in this case will have one key, text, which has your binary string as its value. You can then send that value to the device.

What you appear to be doing is just sending the incoming JSON string right through to the device without decoding it manually to extract the bytes you want, ie. your code is trying to print the incoming data as a complete string.

You can see your data bytes as the last 18 pairs in the “local raster data”.

01 80 00


01 c2 80 00

This is a Latin-1 to UTF-8 conversion. JSON strings are required to be encoded in UTF-8, which you raw binary data isn’t. I’m not sure exactly where in the process things are going wrong, but you can fix it by ensuring that your JSON string is valid UTF-8 – e.g., by Base64-encoding it in your Python server and Base64-decoding it in the agent code before sending it to the device.