In this article
Categories

Milesight EM300-MCS TTN payload decoder for AlphaX

Print

Example fixed length LoRaWAN payload formatter for Milesight EM300-MCS on TTN V3 console

Example fixed length LoRaWAN payload formatter for Milesight EM300-MCS on TTN V3 console

Decoder
/**
* Payload Decoder for The Things Network
* Copyright 2021 Minnovation IoT
* @product Milesight EM300-MCS
*/

function Decoder(bytes, port) {

var decoded = [];

for (var i = 0; i < bytes.length;) {
var channel_id = bytes[i++];
var channel_type = bytes[i++];
// BATTERY
if (channel_id === 0x01 && channel_type === 0x75) {
decoded.push({channelId:255, value:bytes[i]});
i += 1;
}
// TEMPERATURE
else if (channel_id === 0x03 && channel_type === 0x67) {
// ℃
decoded.push({channelId:1, value:readInt16LE(bytes.slice(i, i + 2)) / 10});
i += 2;

// ℉
// decoded.push({channelId:1, value:readInt16LE(bytes.slice(i, i + 2)) / 10 * 1.8 + 32});
// i +=2;
}
// HUMIDITY
else if (channel_id === 0x04 && channel_type === 0x68) {
decoded.push({channelId:2, value: bytes[i] / 2});
i += 1;
}
// WATER LEAK
else if (channel_id === 0x06 && channel_type === 0x00) {
decoded.push({channelId:3, value:(bytes[i] === 0) ? 0 : 1});
i += 1;
} else {
break;
}
}

return decoded;
}

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