Converting Payload

Access LNS Decoder Values

It may be that you already have Devices on your LoRaWAN Network Server (like TTN or Helium) and that you already have a working Payload Decoder set up and running.

So instead of copying your Payload Decoder to Datacake, you can access the decoded fields directly and use the payload decoder section of Datacake to convert this information.

In this article, we show you how this goes.

Let's assume you have already implemented an application on an LNS (TTN in this example) and already have a usable payload. This would look something like the following:

TTN Example

{
  "app_id": "my-sensod",
  "dev_id": "00137a1000006482",
  "hardware_serial": "00137A1000006482",
  "port": 6,
  "counter": 4085,
  "payload_raw": "AQQBHwAGAAAAAAA=",
  "payload_fields": {
    "battery": 3.1,
    "light": 6,
    "loudness": 45.2,
    "co2": 563
  },
  "metadata": {
    // ...
  },
  "downlink_url": "..."
}

The above example shows you the payload of a webhook forward on TTN. The following information is important for us:

"payload_fields": {
  "battery": 3.1,
  "light": 6,
  "loudness": 45.2,
  "co2": 563
}

You can access these fields from within the payload decoder section of Datacake.

For this, you can access the payload coming over webhook via the global rawPayload variable. This would look like this:

function Decoder(bytes, port) {

    // Access the decoded field like this:
    // var battery = rawPayload.payload_fields.battery;
     
    return [
        {
            field: "BATTERY",
            value: rawPayload.payload_fields.battery
        },
        {
            field: "LIGHT",
            value: rawPayload.payload_fields.light
        },
        {
            field: "LOUDNESS",
            value: rawPayload.payload_fields.loudness
        },
        {
            field: "CO2",
            value: rawPayload.payload_fields.co2
        }
    ];
}

The rawPayload also allows access to many other values coming over the Webhook.

Please note that if you change the LoRaWAN Network Server, the rawPayload is differently structured. You can use the "normalizedPayload" instead but this won't give you access to the decoded fields.

Helium Example

{
   "app_eui":"001122334455667788",
   "dc":{
      "balance":22,
      "nonce":22
   },
   "decoded":{
      "payload":{
         "battery":2.34,
         "humidity":23.4,
         "temperature":22.20
      },
      "status":"success"
   },
   "dev_eui":"DEADBEEF00112233",
   "devaddr":"000000",
   "downlink_url":"",
   "fcnt":2034,
   "hotspots":[
   ],
   // ...
}

Decoder on Datacake

function Decoder(bytes, port) {
     
    return [
        {
            field: "BATTERY",
            value: decoded.payload.battery
        },
        {
            field: "HUMIDITY",
            value: decoded.payload.humidity
        },
        {
            field: "TEMPERATURE",
            value: decoded.payload.temperature
        }
    ];
}

Converter for TTN Decoder

When you have a Payload Decoder on TTN and you want to bring that on Datacake, you can copy and paste these decoders 1:1 in most of the cases.

You would, however, need to modify the Decoder in such a way that it returns the Data in a different way.

But instead of rewriting the existing decoder, you could use the following snippet to convert the output of the existing decoder into an output that is compatible with the Datacake API.

function Decoder(bytes, port) {
    
    // decode Payload from Sensor
    var decoded = ... // {temperature:23.2, co2:234, humidity:34, vdd:2345}
    
    // Array where we store the fields that are being sent to Datacake
    var datacakeFields = []
    
    // take each field from decodedElsysFields and convert them to Datacake format
    for (var key in decoded) {
        if (decoded.hasOwnProperty(key)) {           
            datacakeFields.push({field: key.toUpperCase(), value: decoded[key]})
        }
    }      
    
    // forward data to Datacake
    return datacakeFields;
}

Last updated