Skip to content

Instantly share code, notes, and snippets.

@cmur2
Last active June 7, 2023 21:14
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 cmur2/f4308442498922154b6bf36222e2a371 to your computer and use it in GitHub Desktop.
Save cmur2/f4308442498922154b6bf36222e2a371 to your computer and use it in GitHub Desktop.
TTN Decoder for Dragino TrackerD (reformatted)
function decodeUplink(input) {
var bytes = input.bytes;
var port = input.fPort;
function getLatitude() {
return (bytes[0]<<24 | bytes[1]<<16 | bytes[2]<<8 | bytes[3])/1000000; // gps latitude,unit: °
}
function getLongitude() {
return (bytes[4]<<24 | bytes[5]<<16 | bytes[6]<<8 | bytes[7])/1000000; // gps longitude,unit: °
}
function getLocation() {
var latitude = getLatitude();
var longitude = getLongitude();
if ((latitude > -190) && (latitude < 190) && (latitude !== 0) &&
(longitude > -190) && (longitude < 190) && (longitude !== 0))
{
return "" + latitude + "," + longitude + "";
} else {
return "NULL";
}
}
var batV, led_updown, intwk;
if(port == 2 || port == 3) {
var alarm = (bytes[8] & 0x40) !== 0; // alarm status
batV = (((bytes[8] & 0x3f)<<8) | bytes[9])/1000; // battery,unit:V
led_updown = (bytes[8] & 0x02) !== 0; // LED status for position, uplink and downlink
intwk = (bytes[8] & 0x01) !== 0;
var hum, temp;
if((bytes[10] & 0xc0) !== 1) {
hum = (bytes[11]<<8 | bytes[12])/10; // humidity,unit: %
temp = (bytes[13]<<8 | bytes[14])/10; // temperature,unit: °C
}
if(port == 2) {
return {
data: {
Location: getLocation(bytes),
Latitude: getLatitude(bytes),
Longitude: getLongitude(bytes),
Humidity: hum,
Temperature: temp,
BatteryVoltage: batV,
AlarmStatus: alarm,
LightStatus: led_updown,
Transport: intwk,
},
};
} else if(port == 3) {
return {
data: {
Location: getLocation(bytes),
Latitude: getLatitude(bytes),
Longitude: getLongitude(bytes),
BatteryVoltage: batV,
Alarm: alarm,
LightEnabled: led_updown,
TransportMoving: intwk,
},
};
}
}
else if(port == 4) {
var year = bytes[8]<<8 | bytes[9];
var month = bytes[10];
var day = bytes[11];
var hour = bytes[12];
var min = bytes[13];
var sec = bytes[14];
return {
data: {
Location: getLocation(bytes),
Latitude: getLatitude(bytes),
Longitude: getLongitude(bytes),
Date: year+":"+month+":"+day,
Time: hour+":"+min+":"+sec,
},
};
}
else if(port == 5) {
var sensor_model = (bytes[0] == 0x13) ? "TrackerD" : "NULL";
var freq_band;
if(bytes[3] == 0x01)
freq_band = "EU868";
else if(bytes[3] == 0x02)
freq_band = "US915";
else if(bytes[3] == 0x03)
freq_band = "IN865";
else if(bytes[3] == 0x04)
freq_band = "AU915";
else if(bytes[3] == 0x05)
freq_band = "KZ865";
else if(bytes[3] == 0x06)
freq_band = "RU864";
else if(bytes[3] == 0x07)
freq_band = "AS923";
else if(bytes[3] == 0x08)
freq_band = "AS923_1";
else if(bytes[3] == 0x09)
freq_band = "AS923_2";
else if(bytes[3] == 0x0A)
freq_band = "AS923_3";
else if(bytes[3] == 0x0B)
freq_band = "CN470";
else if(bytes[3] == 0x0C)
freq_band = "EU433";
else if(bytes[3] == 0x0D)
freq_band = "KR920";
else if(bytes[3] == 0x0E)
freq_band = "MA869";
var sub_band = (bytes[4] == 0xff) ? "NULL" : bytes[4];
var firm_ver = (bytes[1] & 0x0f)+'.'+((bytes[2]>>4) & 0x0f)+'.'+(bytes[2] & 0x0f);
var sensor_mode = (bytes[7]>>6) & 0x3f;
var gps_mod = (bytes[7]>>4) & 0x03;
var ble_mod = bytes[7] & 0x0f;
batV = (((bytes[5] & 0x3f)<<8) | bytes[6])/1000; // battery,unit:V
var pnackmd = (bytes[8] & 0x04) !== 0; // PNACKMD status
led_updown = (bytes[8] & 0x02) !== 0; // LED status for position, uplink and downlink
intwk = (bytes[8] & 0x01) !== 0;
var sensor;
if(sensor_mode == 1)
sensor = "GPS";
else if(sensor_mode == 2)
sensor = "BLE";
else if(intwk)
sensor = "Spots";
else if(sensor_mode == 3)
sensor = "BLE+GPS Hybrid";
return {
data: {
SensorModel: sensor_model,
FirmwareVersion: firm_ver,
FrequencyBand: freq_band,
SubBand: sub_band,
Sensor: sensor,
GPSMode: gps_mod,
BLEMode: ble_mod,
BatteryVoltage: batV,
PNACHEnabled: pnackmd,
LightEnabled: led_updown,
TransportMoving: intwk,
},
};
}
// if(port == 6)
// {
// major = bytes[17];
// minor = bytes[19];
// power = bytes[20] << 8 | bytes[21];
// rssi = bytes[23]<<24>>24;
// con = "";
// for(i = 0 ; i < 16 ; i++) {
// con += bytes[i].toString(16);
// }
// value = con;
// var uuid = value;
// alarm=(bytes[24] & 0x40)?"TRUE":"FALSE";//Alarm status
// batV=(((bytes[24] & 0x3f) <<8) | bytes[25])/1000;//Battery,units:V
// mod = (bytes[26] & 0xC0)>>6;
// led_updown=(bytes[26] & 0x20)?"ON":"OFF";//LED status for position,uplink and downlink
// if(bytes[26] & 0xC0==0x40)
// {
// hum=(bytes[27]<<8 | bytes[28])/10;//hum,units: °
// tem=(bytes[29]<<8 | bytes[30])/10; //tem,units: °
// }
// return {
// BatV:batV,
// ALARM_status:alarm,
// MD:mod,
// LON:led_updown,
// UUID: uuid,
// MAJOR: major,
// MINOR: minor,
// RSSI:rssi,
// POWER:power,
// };
// }
// if(port == 0x07)
// {
// alarm=(bytes[0] & 0x40)?"TRUE":"FALSE";//Alarm status
// batV=(((bytes[0] & 0x3f) <<8) | bytes[1])/1000;//Battery,units:V
// mod = bytes[2] & 0xC0;
// led_updown=(bytes[2] & 0x20)?"ON":"OFF";//LED status for position,uplink and downlink
// return {
// BatV:batV,
// ALARM_status:alarm,
// MD:mod,
// LON:led_updown,
// };
// }
// if(port==0x08)
// {
// con = "";
// for(i = 0 ; i < 6 ; i++) {
// con = bytes[i].toString();
// str += String.fromCharCode(con);
// }
// var wifissid = str,
// rssi = bytes[6]<<24>>24;
// alarm=(bytes[7] & 0x40)?"TRUE":"FALSE";//Alarm status
// batV=(((bytes[7] & 0x3f) <<8) | bytes[8])/1000;//Battery,units:V
// mod = (bytes[9] & 0xC0)>>6;
// led_updown=(bytes[9] & 0x20)?"ON":"OFF";//LED status for position,uplink and downlink
// return {
// WIFISSID:wifissid,
// RSSI:rssi,
// BatV:batV,
// ALARM_status:alarm,
// MD:mod,
// LON:led_updown,
// };
// }
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment