Payload Decoders
LoRa devices send their readings as bytes instead of a full JSON object in order to save bandwidth. For Datacake to be able to process and store the sent measurements, you need a piece of JavaScript code called a Payload Decoder.
A Payload Decoder takes a buffer (list of bytes) as well as the port of the message and converts them to a normalized object Datacake can understand.
If you want to send data to your devices, you can define Downlinks (or Payload Encoders). You'll find all the Informations about this here:

How to access the Payload Decoder section?

Use the tab bar of the Device View to navigate to the configuration of your Device and scroll down a little until you reach the box "LoRaWAN".
There you will find a "Show" button in the "Payload Decoder" area at the end of the panel. If you click on it, the Payload Decoder section will be displayed.

How to develop a Payload Decoder on Datacake?

Imagine a LoRa sensor sending a single measurement, which is a float indicating a temperature.
To encode this measurement, one could use the following code:
1
float temperature = 23.5;
2
byte payload[1];
3
payload[0] = round(temperature * 100);
Copied!
In this example, we are first convert the float to an integer without losing precision by multiplying it with 100. To decode it back to a float, we simply divide it by 100 again. The following code is an example, how a Payload Decoder can look on the Datacake platform.
1
function Decoder(payload, port) {
2
if(port === 1) {
3
return [
4
{
5
field: "TEMPERATURE",
6
value: payload[0] / 100
7
}
8
];
9
}
10
}
Copied!
As you can see, a Payload Decoder must contain a function Decoder which takes the payload and port as arguments and returns a list of measurements in the {field: str, value: any, timestamp?: number} format.

Recording multiple measurements

A Payload Decoder can return a list of an arbitrary number of measurements.
1
function Decoder(payload, port) {
2
return [
3
{
4
field: "FIELD1",
5
value: true
6
},
7
{
8
field: "FIELD2",
9
value: false
10
}
11
];
12
}
Copied!

Recording historic data

Some LoRa devices send an aggregated list of measurements for e.g. the last hour. Each measurement can take an optional timestamp parameter which is a UNIX timestamp (seconds since Jan 01 1970 (UTC)).
1
function Decoder(payload, port) {
2
return [
3
{
4
field: "FIELD1",
5
value: true,
6
timestamp: 1589815300
7
},
8
{
9
field: "FIELD1",
10
value: false,
11
timestamp: 1589811700
12
}
13
];
14
}
Copied!

Converting TTN Payload Decoders

You can use JS-Style Payload Decoders that work in your TheThingsNetwork Applications on Datacake. However, they must be adjusted so that the output of these decoders meets the expectations of Datacake.
Mostly a TTN Decoder Looks like the following:
1
/**
2
* Ursalink Sensor Payload Decoder
3
*
4
* definition [channel-id] [channel-type] [channel-data]
5
*
6
* 01: battery -> 0x01 0x75 [1byte] Unit: %
7
* 03: temperature -> 0x03 0x67 [2bytes] Unit: °C
8
* ------------------------------------------ EM500-PT100
9
*/
10
function Decoder(bytes, port) {
11
var decoded = {};
12
13
for (var i = 0; i < bytes.length;) {
14
var channel_id = bytes[i++];
15
var channel_type = bytes[i++];
16
// BATTERY
17
if (channel_id === 0x01 && channel_type === 0x75) {
18
decoded.battery = bytes[i];
19
i += 1;
20
}
21
// TEMPERATURE
22
else if (channel_id === 0x03 && channel_type === 0x67) {
23
decoded.temperature = readInt16LE(bytes.slice(i, i + 2)) / 10;
24
i += 2;
25
} else {
26
break;
27
}
28
}
29
return decoded;
30
}
31
32
/* ******************************************
33
* bytes to number
34
********************************************/
35
function readUInt16LE(bytes) {
36
var value = (bytes[1] << 8) + bytes[0];
37
return value & 0xffff;
38
}
39
function readInt16LE(bytes) {
40
var ref = readUInt16LE(bytes);
41
return ref > 0x7fff ? ref - 0x10000 : ref;
42
}
Copied!
This Decoder returns the payload structured as follows:
1
payload = {
2
"temperature": 21.3,
3
"battery": 55
4
};
5
6
return payload;
Copied!
Datacake expects this to be:
1
payload = [
2
{ "field": "TEMPERATURE", "value": 21.3 },
3
{ "field": "BATTERY", "value": 55 },
4
];
5
6
return payload;
Copied!
So instead of having a dictionary with a key:value-pair for every field or measurement value (like temperature, battery), Datacake expects an Array to be returned with a Dictionary for each value you would like to forward into Datacake.
Each Dictionary inside that Array consists of at least 2 elements:
    1.
    The Field-Identifier ("field": "TEMPERATURE")
    2.
    The actual value of that field ("value": 21.3)
Also as Field-Identifiers in Datacake are always in capital letters you have to turn temperature into TEMPERATURE in order for the payload decoder to match the returned values to the database fields of your device.
Now let's convert this to Datacake.
1
/**
2
* Ursalink Sensor Payload Decoder
3
*
4
* definition [channel-id] [channel-type] [channel-data]
5
*
6
* 01: battery -> 0x01 0x75 [1byte] Unit: %
7
* 03: temperature -> 0x03 0x67 [2bytes] Unit: °C
8
* ------------------------------------------ EM500-PT100
9
*/
10
function Decoder(bytes, port) {
11
12
// decoded needs to be an array so change this:
13
// var decoded = {};
14
// to:
15
var decoded = [];
16
17
for (var i = 0; i < bytes.length;) {
18
var channel_id = bytes[i++];
19
var channel_type = bytes[i++];
20
// BATTERY
21
if (channel_id === 0x01 && channel_type === 0x75) {
22
decoded.push({
23
"field": "BATTERY",
24
"value": bytes[i]
25
});
26
i += 1;
27
}
28
// TEMPERATURE
29
else if (channel_id === 0x03 && channel_type === 0x67) {
30
decoded.push({
31
"field": "TEMPERATURE",
32
"value": readInt16LE(bytes.slice(i, i + 2)) / 10;
33
});
34
i += 2;
35
} else {
36
break;
37
}
38
}
39
return decoded;
40
}
41
42
/* ******************************************
43
* bytes to number
44
********************************************/
45
function readUInt16LE(bytes) {
46
var value = (bytes[1] << 8) + bytes[0];
47
return value & 0xffff;
48
}
49
function readInt16LE(bytes) {
50
var ref = readUInt16LE(bytes);
51
return ref > 0x7fff ? ref - 0x10000 : ref;
52
}
Copied!

Tips and tricks

Execution time

A Payload Decoder must not take longer than 100ms (milliseconds) to return, otherwise its execution will be aborted and you will find a note on the Debug tab of the device.

Access to base64-encoded payload

The base64-encoded payload is available via the global b64payload-variable.

Converting the buffer to a string

You should avoid sending strings over LoRa, since text uses a lot of bytes. You can still decode the buffer to string using:
1
String.fromCharCode.apply(null, payload);
Copied!
Last modified 11mo ago