Skip to content

Instantly share code, notes, and snippets.

@mattrude
Last active September 3, 2021 17:38
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save mattrude/e733b35e19b06a9ddd9a580a931b2a2d to your computer and use it in GitHub Desktop.
Save mattrude/e733b35e19b06a9ddd9a580a931b2a2d to your computer and use it in GitHub Desktop.
TTN or ChirpStack Payload Decode Codec for the Dragino LHT65 Remote Sensor
/*
* ChirpStack Payload Decode Codec for the Dragino LHT65 Remote Sensor
* Version 0.1.0 - 2020-08-14 - Matt Rude <matt@mattrude.com>
*
* *******************************************************************************
*
* Copyright (c) 2020 Matt Rude <matt@mattrude.com>
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*
* *******************************************************************************
*/
function Decode(fPort, bytes) {
switch(fPort) {
case 2:
return {
// External sensor
external_sensor:
{
"0":"None",
"1":"Temperature Sensor",
"4":"Interrupt Sensor send",
"5":"Illumination Sensor",
"6":"ADC Sensor",
"7":"Interrupt Sensor count",
}[bytes[6]&0x7F],
// battery, units:V
battery:parseFloat(((bytes[0]<<8 | bytes[1]) & 0x3FFF)/1000),
// SHT20, temperature, units:°C
temperature:parseFloat(((bytes[2]<<24>>16 | bytes[3])/100).toFixed(2)),
// SHT20, humidity, units:%
humidity:parseFloat(((bytes[4]<<8 | bytes[5])/10).toFixed(1)),
// DS18B20, external_temperature, units:°C
external_temperature:
{
"1":parseFloat(((bytes[7]<<24>>16 | bytes[8])/100).toFixed(2)),
}[bytes[6]&0xFF],
// External pin level, PA4
external_pin_level:
{
"4":bytes[7] ? "High":"Low",
}[bytes[6]&0x7F],
// External pin status, PA4
external_status:
{
"4":bytes[8] ? "True":"False",
}[bytes[6]&0x7F],
// BH1750, illumination, units:lux
external_illumination:
{
"5":parseFloat(bytes[7]<<8 | bytes[8]),
}[bytes[6]&0x7F],
// ADC, PA4, units:V
external_adc_voltage:
{
"6":parseFloat((bytes[7]<<8 | bytes[8])/1000),
}[bytes[6]&0x7F],
// External count, PA4, units:times
external_count:
{
"7":(bytes[7]<<8 | bytes[8]),
}[bytes[6]&0x7F],
// Applicable to working mode 4567,and working mode 467 requires short circuit PA9 and PA10
no_connect:
{
"1":"Sensor no connection",
}[(bytes[6]&0x80)>>7],
};
default:
return {
Error: "No port configured, check the upload payload file."
};
}
}
/*
* TTN Payload Decode Codec for the Dragino LHT65 Remote Sensor
* Version 0.1.0 - 2020-08-14 - Matt Rude <matt@mattrude.com>
*
* *******************************************************************************
*
* Copyright (c) 2020 Matt Rude <matt@mattrude.com>
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*
* *******************************************************************************
*/
function Decoder(bytes, port) {
switch(port) {
case 2:
return {
// External sensor
external_sensor:
{
"0":"None",
"1":"Temperature Sensor",
"4":"Interrupt Sensor send",
"5":"Illumination Sensor",
"6":"ADC Sensor",
"7":"Interrupt Sensor count",
}[bytes[6]&0x7F],
// battery, units:V
battery:parseFloat(((bytes[0]<<8 | bytes[1]) & 0x3FFF)/1000),
// SHT20, temperature, units:°C
temperature:parseFloat(((bytes[2]<<24>>16 | bytes[3])/100).toFixed(2)),
// SHT20, humidity, units:%
humidity:parseFloat(((bytes[4]<<8 | bytes[5])/10).toFixed(1)),
// DS18B20, external_temperature, units:°C
external_temperature:
{
"1":parseFloat(((bytes[7]<<24>>16 | bytes[8])/100).toFixed(2)),
}[bytes[6]&0xFF],
// External pin level, PA4
external_pin_level:
{
"4":bytes[7] ? "High":"Low",
}[bytes[6]&0x7F],
// External pin status, PA4
external_status:
{
"4":bytes[8] ? "True":"False",
}[bytes[6]&0x7F],
// BH1750, illumination, units:lux
external_illumination:
{
"5":parseFloat(bytes[7]<<8 | bytes[8]),
}[bytes[6]&0x7F],
// ADC, PA4, units:V
external_adc_voltage:
{
"6":parseFloat((bytes[7]<<8 | bytes[8])/1000),
}[bytes[6]&0x7F],
// External count, PA4, units:times
external_count:
{
"7":(bytes[7]<<8 | bytes[8]),
}[bytes[6]&0x7F],
// Applicable to working mode 4567,and working mode 467 requires short circuit PA9 and PA10
no_connect:
{
"1":"Sensor no connection",
}[(bytes[6]&0x80)>>7],
};
default:
return {
Error: "No port configured, check the upload payload file."
};
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment