This guide will instruct you to create a codec for a LoRaWAN node to decode uplinks in Easy LoRaWAN Cloud.
- Easy LoRaWAN Server: https://lorawan.easylorawan.com
- Codec repository: here
- The uplink data is in field data with Base64 format. We need to decode the data into object with readable values.
1. Sample codecs for commercial LoRaWAN Nodes
Step 1.1: Sample codecs are collected here
Step 1.2: (Optional) If you have a codec for TTNv2 or ChirpStack v3, you need to put the below wrapper function to convert a codec to ChirpStack v4.
// TTNv2
function Decoder(bytes, port) {
...
}
// TTNv2 to V4 converter
function decodeUplink(input) {
return {
data: Decoder(input.bytes, input.fPort)
};
}
// V3
function Decode(fPort, bytes, variables) {
...
}
// V3 to V4 converter
function decodeUplink(input) {
// Wrapper function for ChirpStack v4
return {
data: Decode(input.fPort, input.bytes, input.variables)
};
}
2. To create a custom codec if a sample codec is not available
Step 2.1: We need to decode uplink data into required JSON format for Easy LoRaWAN Cloud.
- To choose “Custom JavaScript codec functions”.
- To populate the function decodeUplink(input).
- To check sample codecs for hints.
Step 2.2: We can use the below codec as a starting point to convert bytes into a JSON object.
- To convert bytes into other format, you can check this guide.
function decodeUplink(input) {
var bytes = input.bytes;
var BatV = (bytes[0] << 8 | bytes[1]) / 1000 + 0.277;
var PayVER = bytes[2];
// If AT+DATAUP=1, PayloadCount=1 byte, payload#=1 byte
var Temp = (bytes[3] << 8 | bytes[4]) / 10;
var Humid = (bytes[5] << 8 | bytes[6]) / 10;
return {"data": {"BatV":BatV, "PayVER":PayVER, "Temp":Temp, "Humid":Humid}};
}
Step 2.3: After a successful decode, we will see the decoded value in “object”.
Done.
Want to test LoRaWAN?
Subscribe Easy LoRaWAN Cloud to try this guide on your LoRaWAN gateways and nodes.
We will help you to get started and troubleshooting.
Thanks for the guides?
If this guide saves your days, you can give us a cup of coffee.
This is one-time donation.
Thank you for supporting us.
How can I convert bytes into float?
Thanks a lot.
Hi William,
You can look at this guide.
We have sample converters to convert from byte array to integer, float and double.
Hope it helps.
https://iotthinks.com/common-converter-for-decoder-script/