Payload formatting script

This page will explain how you can edit the payload formatting script

NOTE: If you need more information about payload formatters you should checkout the things stack documentation

The payload formatting script is what you use to customize your data coming from the cube cell and convert it to a JSON file sent by the things stack webhook.

To create a payload formatting script you need to go into your application then select "payload formatters -> uplink" in the menu on the left side. Uplink is the data from the node and downlink is the data you want to send to your node. For this application you only need to edit the uplink.

Now select "Custom Javascript formatter" as the formatter type and you should see a code window with a basic function.

function decodeUplink(input) {
  return {
    data: {
      bytes: input.bytes
    },
    warnings: [],
    errors: []
  };
}

The input variable passed to the decodeUplink function is the data received from the node. This data has more than just the bytes you send from your node. You can use input.bytes to get the array of bytes you sent from the node. The function can be used to format these bytes in the way you want it for the webserver.

This is the payload formatting script for our webserver.

//Main function for converting data from CubeCell
function decodeUplink(input) {
  //create an array
  let byteArr = [];
  
  //puts all the bytes sent from the cubecell as a string form into byteArr
  for(let i = 0; i < input.bytes.length; i++){
    byteArr.push(input.bytes[i].toString(16));
  }
  
  //puts first value in byteArr into connector and deletes it
  let connector = byteArr.shift() 
  //puts first value in byteArr into sensor and deletes it
  let sensor = byteArr.shift() 
  //puts first value in battery_byte1 into connector and deletes it
  let battery_byte1 = byteArr.shift() 
  //puts first value in battery_byte2 into connector and deletes it
  let battery_byte2 = byteArr.shift() 
  //Takes battery_byte 1 and 2 combines them and turns the string into decimal
  let batteryValue = parseInt((battery_byte1+battery_byte2),16) 
  
  //return a JSON file with all the variables
  return {
    data: {
      rawInput: input, //Raw input bytes (only used for debugging in the webserver)
      battery: batteryValue, //Battery value as a decimal number
      connector: connector, //The connector number in hex
      sensor: sensor, //The sensor adress in hex
      dataArray: byteArr //The rest of byteArr which should be the sensor values
    },
    warnings: [], // optional
    errors: [] // optional (if set, the decoding failed)
  };
}

Last updated