In this article
Categories

Milesight EM-310-UDL payloads on TTNv3 for AlphaX

Print

Example fixed length LoRaWAN payload formatter for Milesight EM-310-UDL on TTN V3 console

Decoder Example
/**
* Payload Decoder for The Things Network *
* @product EM310-UDL
*/

function decodeUplink(input) {

dynamic = [];

for (var i = 0; i < input.bytes.length;) {
var channel_id = input.bytes[i++];
var channel_type = input.bytes[i++];

// BATTERY
if (channel_id === 0x01 && channel_type === 0x75) {
//decoded.battery = bytes[i];
dynamic.push({channelId: 255, value:input.bytes[i]});
i += 1;
}

// DISTANCE
else if (channel_id === 0x03 && channel_type === 0x82) {
//decoded.distance = readUInt16LE(bytes.slice(i, i + 2));
dynamic.push({channelId: 1, value:readUInt16LE(input.bytes.slice(i, i + 2))});
i += 2;
}

// POSITION
else if (channel_id === 0x04 && channel_type === 0x00) {
//decoded.position = bytes[i] === 0 ? “normal” : “tilt”;
dynamic.push({channelId: 2, value:(input.bytes[i] === 0 ? 0 : 1)});
i += 1;
} else {
break;
}
}
return {
data: dynamic,
warnings: [],
errors: [] }
}

/* ******************************************
* bytes to number
********************************************/

function readUInt16LE(bytes) {
var value = (bytes[1] << 8) + bytes[0]; return value & 0xffff; } function readInt16LE(bytes) { var ref = readUInt16LE(bytes); return ref > 0x7fff ? ref – 0x10000 : ref;
}