-
Notifications
You must be signed in to change notification settings - Fork 9
Create Chirpstack Decoding Function
You can use any decoding function created for
Chirpstackbut you can only use a decoding function created for Chirpstack
Decoding functions are attached to Device Profile in your tenant setup
A decoding function working with Chirpstack must have a function with the following structure and name:
function decodeUplink(input) {
return {
data: {
temp: 22.5
}
};
}The function must process the input field with the following structure:
{
bytes : number [...], // Byte array containing the uplink payload, e.g. [255, 230, 255, 0]
fPort : number, // Uplink fPort.
variables : { ... } // Object containing the configured device variables.
}
If the decoding function you found does not extract the data from input.bytes[] field, it means it's not compatible with Chirpstack.
- First, if your decoding function have a syntax error, you will see that in the event log in the
eventtab:

- If you had no error in the event log, you can check the decoding clicking on
upevent in the event log and have a look to theobjectentry, it should contain your decoded data:

- If you have error or the decoding is not as you are expecting it, the best approach is to test your function in an online javascript tool. As an example you can use playcode.io.
Let's take the following function (working)
function decodeUplink(input) {
var bytes = input.bytes;
// Frame format
// 00..03 Base or BlueDay Night hours
// 04..06 Total delta with previous transmission
decoded = {
baseKW : ((bytes[0] << 24) + (bytes[1] << 16) + (bytes[2] << 8) + bytes[3]) /1000.0,
delta : (bytes[4] << 16) + (bytes[5] << 8) + bytes[6],
};
return {
data: decoded
};
}Copy and paste in into playcode.io and add the following test function:
console.log(decodeUplink(
{
bytes: [0x02,0x67,0x01,0x04,0x03,0x02,0x01,0x48],
fPort: 1,
variables : {}
}
))The bytes field comes from the up event in the event log and reformated:

You should see the result in the console automatically:

If you have a syntax error, like in the following code where missing a ,
decoded = {
baseKW : ((bytes[0] << 24) + (bytes[1] << 16) + (bytes[2] << 8) + bytes[3]) /1000.0
delta : (bytes[4] << 16) + (bytes[5] << 8) + bytes[6],
};
You may see the error in the console:
