In this article
Categories

Milesight AM104/AM107 payload decoder on TTNv3 for AlphaX

Print

Example fixed length LoRaWAN payload formatter for Milesight AM104/AM107 on TTN V3 console to AlphaX

Decoder
/**
* Payload Decoder for The Things Network *
* @product Milesight AM104 / AM107
*/

function decodeUplink(input) {

//var decoded = {};

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 = input.bytes[i];
dynamic.push({channelId: 255, value:input.bytes[i]});
i += 1;
}

// TEMPERATURE
else if (channel_id === 0x03 && channel_type === 0x67) {
// ℃
//decoded.temperature = readInt16LE(bytes.slice(i, i + 2)) / 10;
dynamic.push({channelId: 1, value:readInt16LE(input.bytes.slice(i, i + 2)) / 10});
i += 2; // ℉ // decoded.temperature = readInt16LE(bytes.slice(i, i + 2)) / 10 * 1.8 + 32;
// i +=2;
}

// HUMIDITY
else if (channel_id === 0x04 && channel_type === 0x68) {
//decoded.humidity = bytes[i] / 2;
dynamic.push({channelId: 2, value:input.bytes[i]/2});
i += 1;
}

// PIR
else if (channel_id === 0x05 && channel_type === 0x6A) {
//decoded.activity = readUInt16LE(bytes.slice(i, i + 2));
dynamic.push({channelId: 3, value:readUInt16LE(input.bytes.slice(i, i + 2))});
i += 2;
}

// LIGHT
else if (channel_id === 0x06 && channel_type === 0x65) {
//decoded.illumination = readUInt16LE(bytes.slice(i, i + 2));
dynamic.push({channelId: 4, value:readUInt16LE(input.bytes.slice(i, i + 2))});
//decoded.infrared_and_visible = readUInt16LE(bytes.slice(i + 2, i + 4));
dynamic.push({channelId: 5, value:readUInt16LE(input.bytes.slice(i + 2, i + 4))});
//decoded.infrared = readUInt16LE(bytes.slice(i + 4, i + 6));
dynamic.push({channelId: 6, value:readUInt16LE(input.bytes.slice(i + 4, i + 6))});
i += 6;
}

// CO2
else if (channel_id === 0x07 && channel_type === 0x7D) {
//decoded.co2 = readUInt16LE(bytes.slice(i, i + 2));
dynamic.push({channelId: 7, value:readUInt16LE(input.bytes.slice(i, i + 2))});
i += 2;
}

// TVOC
else if (channel_id === 0x08 && channel_type === 0x7D) {
//decoded.tvoc = readUInt16LE(bytes.slice(i, i + 2));
dynamic.push({channelId: 8, value:readUInt16LE(input.bytes.slice(i, i + 2))});
i += 2;
}

// PRESSURE
else if (channel_id === 0x09 && channel_type === 0x73) {
//decoded.pressure = readUInt16LE(bytes.slice(i, i + 2)) / 10;
dynamic.push({channelId: 9, value:readUInt16LE(input.bytes.slice(i, i + 2)) / 10});
i += 2;
} else {
break;
}
}

return {
data: dynamic,
warnings: [],
errors: [] }
}

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;
}