Created
August 31, 2022 18:07
-
-
Save phrrngtn/b77e9ae435b21c998691451c596ceecc to your computer and use it in GitHub Desktop.
JS uplink formatter for The Things Industries LoRaWAN for the Rejeee SL 300 CO2 sensor
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// xref https://www.thethingsindustries.com/docs/integrations/payload-formatters/javascript/uplink/ | |
// code for uplink formatter for The Things Industries LoRaWAN for the Rejeee SL 300 CO2 sensor. | |
// based on code in the Rejee documentation | |
// Convert a hex string to a byte array | |
// from https://stackoverflow.com/a/34356351/40387 | |
function hexToBytes(hex) { | |
for (var bytes = [], c = 0; c < hex.length; c += 2) | |
bytes.push(parseInt(hex.substr(c, 2), 16)); | |
return bytes; | |
} | |
function decodeUplinkBytes(bytesArray) | |
{ | |
var obj = {}; | |
var warnings = []; | |
var len = bytesArray ? bytesArray.length : 0; | |
var offset = 0; | |
var dtype; | |
do { | |
dtype = bytesArray[offset++]; /* dtype: Sensor Type */ | |
switch (dtype) { | |
case 0x00: { | |
obj.ver = bytesArray[offset] & 0xE0; | |
obj.battery = bytesArray[offset] & 0x1F; | |
offset = offset + 2; | |
break; | |
} | |
case 0x04: { | |
obj.temperature = (bytesArray[offset] << 8) + bytesArray[offset + 1]; | |
offset = offset + 2; | |
break; | |
} | |
case 0x05: { | |
obj.humidity = bytesArray[offset]; | |
offset = offset + 1; | |
break; | |
} | |
case 0x30: { | |
/** | |
* dtype 30: Mixed gas sensor, like CO2, H2S | |
* volume:Gas concentration detection,Unit: 0.01 | |
*/ | |
var l = bytesArray[offset++]; | |
var gastype = bytesArray[offset++]; | |
obj.gastype=gastype; | |
obj.volume = ((bytesArray[offset] << 24) + (bytesArray[offset + 1] << 16) + | |
(bytesArray[offset + 2] << 8) + bytesArray[offset + 3]) / 100; | |
offset =offset+ 4; | |
if (obj.volume < 0) { | |
warnings.push("it's invalid"); | |
} | |
break; | |
} | |
} | |
} while (offset < len); | |
return { | |
data: obj, | |
warnings: warnings | |
}; | |
} | |
function decodeUplink(input) { | |
return decodeUplinkBytes(input.bytes); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment