I’m using a decoder sent to me by the manufacturer, but I keep getting the follwoing error when I try to decode a packet
js vm error: (anonymous): Line 12:7 Unexpected identifier (and 16 more errors)
Can;t for the life of me work it out and not getting much response from the manufacturer. If anyone can help I’d be most grateful.
/********************************************
* Sontay LoRaWAN Packet Decoder script *
* v1.1 *
* *
* Compatible with Sontay LoRaWAN sensors *
********************************************/
function decodeUplink(input) {
let SENSOR_TEMP = (1 << 1);
let SENSOR_RH = (1 << 2);
let SENSOR_CO2 = (1 << 3);
let TOTAL_DATA_SLOTS = 4;
var data = {};
var slot = 0;
let sensors = (input.bytes[0] << 8) | (input.bytes[1] & 0xFE);
var vBat = ((input.bytes[1] & 0x01) << 8) | input.bytes[2];
data.Battery = vBat / 100;
if(vBat >= 330)
data.BatteryStatus = "Good";
else if(vBat < 300)
data.BatteryStatus = "Poor";
else
data.BatteryStatus = "OK";
for(let i = 1; i < 16; i++) {
var d = (input.bytes[3+(2*slot)] << 8) | (input.bytes[4+(2*slot)]);
if(d > 0x7FFF)
d -= 0xFFFF; // Correct for a negative value
switch(sensors & (1 << i)) {
case SENSOR_TEMP:
data.Temperature = d / 100;
break;
case SENSOR_RH:
data.Humidity = d / 10;
break;
case SENSOR_CO2:
data.CO2 = d;
break;
default:
continue;
}
if(++slot > TOTAL_DATA_SLOTS)
break;
}
return {
data,
warnings: [],
errors: []
};
}
Looks like they gave you a TTN decoder.
Chirpstack has a different API.
function Decode(fPort, bytes, variables)
// Decode decodes an array of bytes into an object.
// - fPort contains the LoRaWAN fPort number
// - bytes is an array of bytes, e.g. [225, 230, 255, 0]
// - variables contains the device variables e.g. {"calibration": "3.5"} (both the key / value are of type string)
// The function must return an object, e.g. {"temperature": 22.5}
function Decode(fPort, bytes, variables) {
payload = Decoder(bytes, fPort);
Object.keys(variables).forEach(function(key) { payload[key] = variables[key] } );
return payload;
}
/*
______ _ _______ _______
| ____| | / ____\ \ / / ____|
| |__ | | | (___ \ \_/ / (___
| __| | | \___ \ \ / \___ \
| |____| |____ ____) | | | ____) |
|______|______|_____/ |_| |_____/
ELSYS simple payload decoder.
This file has been truncated. show original
Thanks. Have modified it accordingly and got it working.
1 Like
Hi there,
Did you ever get this working?
I have swapped out the decodeUplink statement with function Decode(fPort, bytes, variables) but no joy, just Line 12:7 Unexpected identifier.
I am using a Milesight Gateway so may be different.
Thanks
Hi. Yes I did and its been running on a Milesight UG65 gateway for a while now.
/********************************************
* Sontay LoRaWAN Packet Decoder script *
* v1.1 *
* *
* Compatible with Sontay LoRaWAN sensors *
* *
* Adapted for use with Milesight Gateway *
* *
* T Judd - Palcon Energy Services Ltd *
********************************************/
function Decode(fport, bytes) {
// function decodeUplink(input) {
var SENSOR_TEMP = (1 << 1);
var SENSOR_RH = (1 << 2);
var SENSOR_CO2 = (1 << 3);
var TOTAL_DATA_SLOTS = 4;
var decoded = {};
var slot = 0;
// decoded.devEUI = LoRaObject.devEUI;
// decoded.rssi = LoRaObject.rxInfo[0].rssi;
var sensors = (bytes[0] << 8) | (bytes[1] & 0xFE);
var vBat = ((bytes[1] & 0x01) << 8) | bytes[2];
decoded.Battery = vBat / 100;
if(vBat >= 330)
decoded.BatteryStatus = "Good";
else if(vBat < 300)
decoded.BatteryStatus = "Poor";
else
decoded.BatteryStatus = "OK";
for(var i = 1; i < 16; i++) {
var d = (bytes[3+(2*slot)] << 8) | (bytes[4+(2*slot)]);
if(d > 0x7FFF)
d -= 0xFFFF; // Correct for a negative value
switch(sensors & (1 << i)) {
case SENSOR_TEMP:
decoded.Temperature = d / 100;
break;
case SENSOR_RH:
decoded.Humidity = d / 10;
break;
case SENSOR_CO2:
decoded.CO2 = d;
break;
default:
continue;
}
if(++slot > TOTAL_DATA_SLOTS)
break;
}
return decoded
}
Hi, that’s working now, I had the wrong return statement, was trying to use the data, warnings, errors bit from Sontay.
Excellent news thanks for your help.