Custom Payload Codec


#1

Hello!

I am trying to use the custom javascript decoding to read my sensor data.

I only need to convert the base64 encoded data into bytes. But I seem to be getting stuck when I use simple functions like atob etc.

Anyone has written a payload function decode to convert base64 to binary/hex data?

Thanks much!


#2

Please note that an array of bytes are passed as argument to the Decode function, not a base64 encoded string.


#3

Hmmm, it is a bit confusing. My device sends a 3 byte data. On the application server, device - > live event logs , for the ‘data’ key , the value is ‘AQUA’. When I decode this base64 string to bytes (used online decoder like https://cryptii.com/base64-to-hex) , I get the 3 byte sensor data.

My payload function for now is very simple :

function Decode(fPort, bytes) {
return {“myresult”:bytes};
}

payload: {} 10keys
applicationID:“4”
applicationName:“emergency-button-application”
data:“AQUA”
devEUI:“004a770124008988”
deviceName:“emergency-button-device”
fCnt:34
fPort:2
object:{} 1 key
myresult:“AQUA”

Shouldn’t myresult object show the bytes, instead shows the same BASE64 data? I am sorry if my confusion is silly, I am very new to the world of LoRa itself. :sweat:

Thanks very much,
Sam


#4

Note that Go by default encodes a byte array to base64 when encoding an object to JSON. So within the context of:

function Decode(fPort, bytes) {
return {“myresult”:bytes};
}

bytes is an array containing the decrypted payload bytes. Thus bytes[0] returns the first byte, etc…


#5

ah got it now… thanks much!!! :grinning:


#6

To me give a base64 string because I send “Hello, world!” and I get “SGVsbG8sIHdvcmxkIQ==” that is the base64 for “Hello, world!”.

I declared in the Arduino skecth:

static uint8_t mydata[] = “Hello, world!”;

Is there a way to understand how decode bytes param from “function Decode(fPort, bytes)”?

I tried a lot of base64 decode javascript function but nothing worked. What kind of javascript LoRa Server implements? What version of the standard?


#7

I used the javascript decode function from here: https://en.wikibooks.org/wiki/Algorithm_Implementation/Miscellaneous/Base64#Javascript_2

but it says:

error:"js vm error: TypeError: 'replace' is not a function".

why?


#8

It uses a package called otto, which runs an ES5 version.
https://www.loraserver.io/lora-app-server/use/applications/


#9

you don’t need to decode base64, you just need to decode your byte depending how you encode your byte in de end-device .