Decoder testing

Hi!

SInce i very new to javascript please forgive my very basic question.

I want to run the decoder script in html page to be able to test and se what happens with different payloads.

But how can i simulate the data being sent to the decoder?

I tried simply adding

var data0 = 0x70;
var data1 = 0x00;

and so on but

console.log(data[0]);

in decoder wont show anything in console of chrome.

I fell like iam missing somethin really basic here.

Hi Patrik,

You’d be better off running the code as a local program from the command line using nodeJS rather than using the browser just to get a JS environment.

Phil.

Yes, thanks, i also came to that conclusion.

Do you know how i can fake a payload to the script with node.js?

Hi Patrik,

Here’s a very simple script that sets up the input data as an array of bytes, calls the Decode function, and prints the result, based on a ThingsNode. Hope that helps

$ node ./test.js
{ event: ‘interval’, battery: 258, light: 772, temperature: 12.86 }

// Decode decodes an array of bytes into an object.
//  - fPort contains the LoRaWAN fPort number
//  - bytes is an array of bytes, e.g. [225, 230, 255, 0]
//  - variables contains the device variables e.g. {"calibration": "3.5"} (both the key / value are of type string)
// The function must return an object, e.g. {"temperature": 22.5}
function Decode(port, bytes, variables) {

  var decoded = {};
  var events = {
    1: 'setup',
    2: 'interval',
    3: 'motion',
    4: 'button'
  };
  decoded.event = events[port];
  decoded.battery = (bytes[0] << 8) + bytes[1];
  decoded.light = (bytes[2] << 8) + bytes[3];
  if (bytes[4] & 0x80)
    decoded.temperature = ((0xffff << 16) + (bytes[4] << 8) + bytes[5]) / 100;
  else
    decoded.temperature = ((bytes[4] << 8) + bytes[5]) / 100;
  return decoded;
}

// define the input data
let data = Buffer.from([01, 02, 03, 04, 05, 06])

// call the decode function
let res = Decode(2, data, null)

// print the result
console.dir(res, {depth:null});
1 Like

If you are using MQTT you could use our tool - it uses the same JS-Engine as chirpstack.


https://gitlab.com/wobcom/iot/homebrew-iot (if you want to use homebrew to download the binary)