In this article
Categories

Milesight UC501 TTN payload decoder for AlphaX

Print

This help article provides a copy-paste example on how to decode the payload for use in AlphaX with the Milesight UC501 device and when using The Things Network (TTN) as the LoRaWAN network. The article includes the necessary code snippet to effectively decode the payload, enabling you to extract and interpret the sensor data transmitted by the Milesight UC501 device.

Decoder Example: Milesight UC501

function Decoder(bytes, port) {
var decoded = [];
for (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; } // GPIO1 elseif (channel_id === 0x03 && channel_type !== 0xC8) { decoded.push({channelId:1, value:bytes[i] === 0 ? 0: 1}); i += 1; } // GPIO2 elseif (channel_id === 0x04 && channel_type !== 0xC8) { decoded.push({channelId:2, value:bytes[i] === 0 ? 0: 1}); i += 1; } // PULSE COUNTER 1 elseif (channel_id === 0x03 && channel_type === 0xc8) { decoded.push({channelId:3, value:readUInt32LE(bytes.slice(i, i + 4))}); i += 4; } // PULSE COUNTER 2 elseif (channel_id === 0x04 && channel_type === 0xc8) { decoded.push({channelId:4, value:readUInt32LE(bytes.slice(i, i + 4))}); i += 4; } // ADC 1 // NOTE: For UC50x V2 with firmware version 1.10 and below and UC50x V1, change 1000 to 100. elseif (channel_id === 0x05) { decoded.push({ channelId:5, value:readInt16LE(bytes.slice(i, i + 2)) / 1000 }); decoded.push({ channelId:6, value:readInt16LE(bytes.slice(i + 2, i + 4)) / 1000 }); decoded.push({ channelId:7, value:readInt16LE(bytes.slice(i + 4, i + 6)) / 1000 }); decoded.push({ channelId:8, value:readInt16LE(bytes.slice(i + 6, i + 8)) / 1000 }); i += 8; continue; } // ADC 2 // NOTE: For UC50x V2 with firmware version 1.10 and below and UC50x V1, change 1000 to 100. elseif (channel_id === 0x06) { decoded.push({ channelId:9, value:readInt16LE(bytes.slice(i, i + 2)) / 1000 }); decoded.push({ channelId:10, value:readInt16LE(bytes.slice(i + 2, i + 4)) / 1000 }); decoded.push({ channelId:11, value:readInt16LE(bytes.slice(i + 4, i + 6)) / 1000 }); decoded.push({ channelId:12, value:readInt16LE(bytes.slice(i + 6, i + 8)) / 1000 }); i += 8; continue; } // MODBUS elseif (channel_id === 0xFF && channel_type === 0x0E) { var modbus_chn_id = bytes[i++] - 6; var package_type = bytes[i++]; var data_type = package_type & 7; var date_length = package_type >> 3;
switch (data_type) {
case0:
decoded.push({ channelId:modbus_chn_id, value:bytes[i] ? 1 : 0 });
i += 1;
break;
case1:
decoded.push({ channelId:modbus_chn_id, value:bytes[i] });
i += 1;
break;
case2:
case3:
decoded.push({ channelId:modbus_chn_id, value:readUInt16LE(bytes.slice(i, i + 2)) });
i += 2;
break;
case4:
case6:
decoded.push({ channelId:modbus_chn_id, value:readUInt32LE(bytes.slice(i, i + 4)) });
i += 4;
break;
case5:
case7:
decoded.push({ channelId:modbus_chn_id, value:readFloatLE(bytes.slice(i, i + 4)) });
i += 4;
break;
}
}
// MODBUS READ ERROR
elseif (channel_id === 0xff && channel_type === 0x15) {
var modbus_chn_id = bytes[i] + 1;
decoded.push({channelId:253,value:modbus_chn_id});
i += 1;
} else {
break;
}
}
return decoded;
}
/* ******************************************
* bytes to number
********************************************/
function readUInt8LE(bytes) {
return (bytes & 0xFF);
}
function readInt8LE(bytes) {
var ref = readUInt8LE(bytes);
return (ref > 0x7F) ? ref - 0x100 : ref;
}
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;
}
function readUInt32LE(bytes) {
var value = (bytes[3] << 24) + (bytes[2] << 16) + (bytes[1] << 8) + bytes[0]; return (value & 0xFFFFFFFF); } function readInt32LE(bytes) { var ref = readUInt32LE(bytes); return (ref > 0x7FFFFFFF) ? ref - 0x100000000 : ref;
}
function readFloatLE(bytes) {
// JavaScript bitwise operators yield a 32 bits integer, not a float.
// Assume LSB (least significant byte first).
var bits = bytes[3] << 24 | bytes[2] << 16 | bytes[1] << 8 | bytes[0]; var sign = (bits >>> 31 === 0) ? 1.0 : -1.0; var e = bits >>> 23 & 0xff;
var m = (e === 0) ? (bits & 0x7fffff) << 1 : (bits & 0x7fffff) | 0x800000;
var f = sign * m * Math.pow(2, e - 150);
return f;
}