Skip to content

Instantly share code, notes, and snippets.

@e-cite
Last active January 9, 2022 17:05
Show Gist options
  • Save e-cite/c1b040e6f34ebb53b10ad6756347f4db to your computer and use it in GitHub Desktop.
Save e-cite/c1b040e6f34ebb53b10ad6756347f4db to your computer and use it in GitHub Desktop.
Dragino TTN Decoders
// From: https://www.dragino.com/downloads/index.php?dir=LGT_92/Decoder/
// But roll, pitch and altitude are wrong due to <<24>>16.
// Better: <<8
//The function is :
function Decoder(bytes, port) {
// Decode an uplink message from a buffer
// (array) of bytes to an object of fields.
var latitude;//gps latitude,units: °
latitude=(bytes[0]<<24 | bytes[1]<<16 | bytes[2]<<8 | bytes[3])/1000000;//gps latitude,units: °
var longitude;
longitude=(bytes[4]<<24 | bytes[5]<<16 | bytes[6]<<8 | bytes[7])/1000000;//gps longitude,units: °
var alarm=(bytes[8] & 0x40)?"TRUE":"FALSE";//Alarm status
var batV=(((bytes[8] & 0x3f) <<8) | bytes[9])/1000;//Battery,units:V
if((bytes[10] & 0xC0)==0x40)
{
var motion_mode="Move";
}
else if((bytes[10] & 0xC0) ==0x80)
{
motion_mode="Collide";
}
else if((bytes[10] & 0xC0) ==0xC0)
{
motion_mode="User";
}
else
{
motion_mode="Disable";
} //mode of motion
var led_updown=(bytes[10] & 0x20)?"ON":"OFF";//LED status for position,uplink and downlink
var Firmware = 160+(bytes[10] & 0x1f); // Firmware version; 5 bits
var roll=(bytes[11]<<8 | bytes[12])/100;//roll,units: °
var pitch=(bytes[13]<<8 | bytes[14])/100; //pitch,units: °
var hdop = 0;
if(bytes[15] > 0)
{
hdop =bytes[15]/100; //hdop,units: °
}
else
{
hdop =bytes[15];
}
var altitude =(bytes[16]<<8 | bytes[17]) / 100; //Altitude,units: °
return {
Latitude: latitude,
Longitude: longitude,
Roll: roll,
Pitch:pitch,
BatV:batV,
ALARM_status:alarm,
MD:motion_mode,
LON:led_updown,
FW:Firmware,
HDOP:hdop,
Altitude:altitude,
};
}
// From: https://www.dragino.com/downloads/index.php?dir=LHT65/payload_decode/
// Additional: Decode battery status in function bat_status_decode
function str_pad(byte){
var zero = '00';
var hex= byte.toString(16);
var tmp = 2-hex.length;
return zero.substr(0,tmp) + hex + " ";
}
function bat_status_decode(byte){
if(byte === 0){
return "Ultra Low";
}
else if(byte == 1){
return "Low";
}
else if(byte == 2){
return "OK";
}
else if(byte == 3){
return "Good";
}
else {
return "Unknown";
}
}
function Decoder(bytes, port) {
var Ext= bytes[6]&0x0F;
var poll_message_status=(bytes[6]&0x40)>>6;
var Connect=(bytes[6]&0x80)>>7;
var decode = {};
if(Ext==0x09)
{
decode.TempC_DS=parseFloat(((bytes[0]<<24>>16 | bytes[1])/100).toFixed(2));
decode.Bat_status=bat_status_decode(bytes[4]>>6);
}
else
{
decode.BatV= ((bytes[0]<<8 | bytes[1]) & 0x3FFF)/1000;
decode.Bat_status=bat_status_decode(bytes[0]>>6);
}
if(Ext!=0x0f)
{
decode.TempC_SHT=parseFloat(((bytes[2]<<24>>16 | bytes[3])/100).toFixed(2));
decode.Hum_SHT=parseFloat((((bytes[4]<<8 | bytes[5])&0xFFF)/10).toFixed(1));
}
if(Connect=='1')
{
decode.No_connect="Sensor no connection";
}
if(Ext=='0')
{
decode.Ext_sensor ="No external sensor";
}
else if(Ext=='1')
{
decode.Ext_sensor ="Temperature Sensor";
decode.TempC_DS=parseFloat(((bytes[7]<<24>>16 | bytes[8])/100).toFixed(2));
}
else if(Ext=='4')
{
decode.Work_mode="Interrupt Sensor send";
decode.Exti_pin_level=bytes[7] ? "High":"Low";
decode.Exti_status=bytes[8] ? "True":"False";
}
else if(Ext=='5')
{
decode.Work_mode="Illumination Sensor";
decode.ILL_lx=bytes[7]<<8 | bytes[8];
}
else if(Ext=='6')
{
decode.Work_mode="ADC Sensor";
decode.ADC_V=(bytes[7]<<8 | bytes[8])/1000;
}
else if(Ext=='7')
{
decode.Work_mode="Interrupt Sensor count";
decode.Exit_count=bytes[7]<<8 | bytes[8];
}
else if(Ext=='8')
{
decode.Work_mode="Interrupt Sensor count";
decode.Exit_count=bytes[7]<<24 | bytes[8]<<16 | bytes[9]<<8 | bytes[10];
}
else if(Ext=='9')
{
decode.Work_mode="DS18B20 & timestamp";
decode.Systimestamp=(bytes[7]<<24 | bytes[8]<<16 | bytes[9]<<8 | bytes[10] );
}
else if(Ext=='15')
{
decode.Work_mode="DS18B20ID";
decode.ID=str_pad(bytes[2])+str_pad(bytes[3])+str_pad(bytes[4])+str_pad(bytes[5])+str_pad(bytes[7])+str_pad(bytes[8])+str_pad(bytes[9])+str_pad(bytes[10]);
}
if(poll_message_status===0)
{
if(bytes.length==11)
{
return decode;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment