// Décodeur LoRaWAN officiel : WTS506 - Weather Station (Milesight) // Source : https://github.com/TheThingsNetwork/lorawan-devices/blob/master/vendor/milesight-iot/wts506.js // The Things Network Device Repository, fichier publié par le constructeur, distribué sans modification. // Format : decodeUplink(input) (TTN v3 / ChirpStack v4). Hébergé par smartbuildings.fr. function decodeUplink(input) { var res = Decoder(input.bytes, input.fPort); if (res.error) { return { errors: [res.error], }; } return { data: res, }; } /** * Payload Decoder for The Things Network * * Copyright 2023 Milesight IoT * * @product WTS305 / WTS506 */ function Decoder(bytes, port) { return milesight(bytes); } function milesight(bytes) { 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.battery = bytes[i]; i += 1; } // TEMPERATURE else if (channel_id === 0x03 && channel_type === 0x67) { // ℃ decoded.temperature = readInt16LE(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; i += 1; } // Wind Direction, unit degree else if (channel_id === 0x05 && channel_type === 0x84) { decoded.wind_direction = readInt16LE(bytes.slice(i, i + 2)) / 10; i += 2; } // Barometric Pressure, unit hPa else if (channel_id === 0x06 && channel_type === 0x73) { decoded.pressure = readUInt16LE(bytes.slice(i, i + 2)) / 10; i += 2; } // Wind Speed, unit m/s else if (channel_id === 0x07 && channel_type === 0x92) { decoded.wind_speed = readUInt16LE(bytes.slice(i, i + 2)) / 10; i += 2; } // rainfall_total, unit mm, Frame counter to define whether device enters the new rainfall accumulation phase, it will plus 1 every upload, range: 0~255 else if (channel_id === 0x08 && channel_type === 0x77) { decoded.rainfall_total = readUInt16LE(bytes.slice(i, i + 2)) / 100; decoded.rainfall_counter = bytes[i + 2]; i += 3; } 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; }