Payload Decoders
Last updated
Last updated
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:
Downlinks (LoRaWAN)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.
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:
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.
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.
A Payload Decoder can return a list of an arbitrary number of measurements.
When Inserting Data into Datacake you can provide a Timestamp. Usually Datacake auto-creates a timestamp upon writing into the Database. This does happen in serialized write requests so there can be a few seconds in delay in between writing operation that normally are encapsulated within one single message.
By providing a timestamp key option in the dictionary of fields, the database uses a foreign timestamp.
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)).
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:
This Decoder returns the payload structured as follows:
Datacake expects this to be:
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:
The Field-Identifier ("field": "TEMPERATURE"
)
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.
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.
The base64-encoded payload is available via the global b64payload
-variable.
You should avoid sending strings over LoRa, since text uses a lot of bytes. You can still decode the buffer to string using: