Skip to content

Instantly share code, notes, and snippets.

@koenvervloesem
Created January 30, 2020 10:00
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save koenvervloesem/c9dca322bd25a98dd042324469d0081d to your computer and use it in GitHub Desktop.
Save koenvervloesem/c9dca322bd25a98dd042324469d0081d to your computer and use it in GitHub Desktop.
Payload format decoder for the Dragino LHT65 LoRaWAN node in The Things Network
// Source: http://www.dragino.com/downloads/downloads/LHT65/UserManual/LHT65_Temperature_Humidity_Sensor_UserManual_v1.3.pdf
function Decoder(bytes, port) {
// Decode an uplink message from a buffer
// (array) of bytes to an object of fields.
var value = (bytes[0] << 8 | bytes[1]) & 0x3FFF;
var batV = value / 1000; //Battery,units:V
value = bytes[2] << 8 | bytes[3];
if (bytes[2] & 0x80) {
value |= 0xFFFF0000;
}
var temp_SHT = (value / 100).toFixed(2); //SHT20,temperature,units:°C
value = bytes[4] << 8 | bytes[5];
var hum_SHT = (value / 10).toFixed(1); //SHT20,Humidity,units:%
value = bytes[7] << 8 | bytes[8];
if (bytes[7] & 0x80) {
value |= 0xFFFF0000;
}
var temp_ds = (value / 100).toFixed(2); //DS18B20,temperature,units:°C
return {
BatV: batV,
TempC_DS: temp_ds,
TempC_SHT: temp_SHT,
Hum_SHT: hum_SHT
};
}
@gvasquez95
Copy link

Works great…I was using a previous decoder and just today it overflowed (down) when temperature reached values below ZERO Celsius

Thanks for sharing

@gvasquez95
Copy link

gvasquez95 commented Jun 27, 2023

Rust Version:

fn dragino_decoder(bytes: &[u8]) -> (f32, f32, f32, f32) {
    let value = ((bytes[0] as u16) << 8 | (bytes[1] as u16)) & 0x3FFF;
    let bat_v = value as f32 / 1000.0;

    let value = (bytes[2] as i16) << 8 | (bytes[3] as i16);
    let temp_sht = f32::trunc(value as f32) / 100.0;

    let value = (bytes[4] as u16) << 8 | (bytes[5] as u16);
    let hum_sht = f32::trunc(value as f32) / 10.0;

    let value = (bytes[7] as i16) << 8 | (bytes[8] as i16);
    let temp_ds = f32::trunc(value as f32) / 100.0;

    (bat_v, temp_sht, hum_sht, temp_ds)
}

@davidnghk
Copy link

what is TempC_DS ? what is TempC_SHT ?

@koenvervloesem
Copy link
Author

The TempC_SHT value refers to the temperature measured by the built-in SHT20 sensor, while the TempC_DS value is the temperature measured by the external DS18B20 probe.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment