Custom Payload Codec

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!

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

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

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…

3 Likes

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

1 Like

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?

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?

It uses a package called otto, which runs an ES5 version.

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

Hi. would you mind to share me how you did it please.
Thank you

still not understanding why my custom codec won’t decode the data/bytes.
I have a data field that is about 64 characters (base64 looking), data is encoded in bytes, so why does my payload return just 1… like the number 1 when i do
return bytes;

Thoughts?

UPDATE
I wasn’t using the correct signature for my function. I needed this:
function (fport, bytes)

All good now. Thanks!

char * str = "Hello, world!";
for(int i = 0 ; i < strlen(str) ; i++ )
 printf("%02x", str[i]);

by using the c code above to transfer the “Hello, world!”
we will get 48656c6c6f2c20776f726c6421

use the lora module to tranfer this hex
mac tx cnf 1 48656c6c6f2c20776f726c6421

function Decode(fPort, bytes) {
   var data = {
      "cmd":""
   };
   for (var i = 0; i < bytes.length; i++) {
      data.cmd+= String.fromCharCode(bytes[i]);
   }
   return data;
}

my appID is 1 we can sub the topic to see into the payload
use mosquitto_sub or any mqtt client like mqtt.fx
mosquitto_sub -t “application/1/#” -v

the payload will show the string:
“objectJSON”:"{“cmd”:“Hello, world!”}",

Hi Moonlca, this seems like a very good answer, if you could expand on it a little more, more than one of us would appreciate it.
I think we need more examples like this.
Thank you

Hi, I too have a same query while I am creating my decode code in Custom Payload Codec I have tried to convert data using my code.Before I start my I checked by returning {“Data” : bytes} I am getting output as * objectJSON:“Data : Payload” Payload as base64 encoded String instead of array of bytes. And I don’t why I am getting Base64. Can you check my query and tell how I can proceed.
Thank You

Please i am looking for assistance. unfortunately i am not understanding
i have this in the codec custom

function bin2String(array) {
return String.fromCharCode.apply(String, array);
}

function bin2HexStr(arr)

{
var str = "";
for(var i=0; i<arr.length; i++)
{

var tmp = arr[i].toString(16);
  if(tmp.length == 1)
   {
    tmp = "0" + tmp;
   }
    tmp = "0x" + tmp;
     if (i != arr.length - 1) {
     tmp += ",";
   }
   str += tmp;
   }
  return str;
  }


function Decode(fPort, bytes)
{
  var myObj = {"DecodeDataString":"", "DecodeDataHex":"", "SensorID":"", "Temp":"", "RH":""};
  var tostring=bin2String(bytes);
  var tosHextring=bin2HexStr(bytes);
  myObj.DecodeDataString = bytes;
  myObj.DecodeDataHex = tosHextring;
  return myObj;
}

this was the canned function that came with the chirpstack.
i get this return.

DecodeDataHex:"0x01,0x03,0x04,0x08,0x81,0x0f,0xc1,0x6c,0x1b"
DecodeDataString:"AQMECIEPwWwb"
RH:""
SensorID:""
Temp:""

if i put the “DecodeDataString” AQMECIEPwWwb into a Base64 to Hex converter online, i get 010304080810fc16c1b which matches the DecodeDataHex if you were to add the commas and 0x in front of each number.
But i need the data to be different and dont know how to modify. The 01 is my SensorID, the 03 is the READ function, the 04 how many bytes i have. The two bytes 08 and 08 go together to make 0808 Hex that needs to convert to decimal to give the temp in C x100 or 2056. the two bytes 10 and fc go together to be 10fc hex to be converted to decimal RH x 100 or 4348. The last bit is CRC and doesnt need to be extracted.
So how do i do this in Javascript

i resolved this one
thanks for taking a look to all those that did/