© Copyright 2016 by The-Box Development

Translate sensor values

 

Being able to view our encoded data in the web interface is nice, however it would be nicer to be able to view the data in a format more suited for human consumption. Seeing a temperature in a format like '26.30' is preferable over '0A46'  for humans. Also having to know the first two bytes are the temperature multiplied by 100 and the last two bytes are humidity is not user friendly either.

 

The Things Network provides the means to translate the data. We will configure this using the web interface.

 

On the application detail page in the "Application Info" block you will find "Payload Functions", click on either the pencil icon or the word 'edit'.

Now we are able to provide three scripts. A decoder script, a converter script and a validator script, These scripts are written in Javascript.

For now we are only going to use the decoder script. Replace the existing code with:

function (bytes) {

  var t = (bytes[0] * 256 + bytes[1]) / 100;

  var h = (bytes[2] * 256 + bytes[3]) / 100;

 

  // todo: return an object

  return {

    temperature : t,

    humidity: h,

    payload: bytes,

  };

}

To test the code, enter '0A460F50' in the input field left of the "Test" button and click the "Test' Button. The result should look like this:

Now hit the "Save" button and navigate one page back (Use the breadcrumbs at the top left). When the next packet is received the messages will show the temperature and humidity in a human readable format as well.