HT Sensor Uplink Decoder
1
var calculateTemperature = function (rawData){return (rawData - 400) / 10};
2
var calculateHumidity = function(rawData){return (rawData * 100) / 256};
3
function decbin(byte){
4
return (parseInt(byte, 16).toString(2)).padStart(8, '0');
5
}
6
function Decoder(hexData){
7
var data = hexData;
8
var tempHex = ("0" + data[1].toString(16)).substr(-2) + ("0" + data[2].toString(16)).substr(-2);
9
var tempDec = parseInt(tempHex, 16);
10
var temperatureValue = calculateTemperature(tempDec);
11
var humidityValue = calculateHumidity(data[3]);
12
var batteryTmp = ("0" + data[4].toString(16)).substr(-2)[0];
13
var batteryVoltageCalculated = 2 + parseInt("0x" + batteryTmp, 16) * 0.1;
14
var reason = data[0];
15
var temperature = temperatureValue;
16
var humidity = humidityValue;
17
var batteryVoltage = batteryVoltageCalculated;
18
var thermistorProperlyConnected = decbin(data[5])[5] == 0;
19
var extT1 = ("0" + data[5].toString(16)).substr(-2)[1];
20
var extT2 = ("0" + data[6].toString(16)).substr(-2);
21
var extThermistorTemperature = thermistorProperlyConnected ? parseInt(`0x${extT1}${extT2}`, 16) * 0.1 : 0;
22
23
// check if it is a keepalive
24
if (reason == 1) {
25
return {
26
reason: 'keepalive',
27
temperature: temperature,
28
humidity:humidity,
29
batteryVoltage: batteryVoltage,
30
thermistorProperlyConnected:thermistorProperlyConnected,
31
extThermistorTemperature:extThermistorTemperature
32
33
}
34
}
35
}
Copied!
1
var calculateTemperature = function (rawData){return (rawData - 400) / 10};
2
var calculateHumidity = function(rawData){return (rawData * 100) / 256};
3
var decbin = function(number) {
4
var number = parseInt(number, 10).toString(2);
5
return Array(9 - number.length).join("0") + number;
6
}
7
function Decoder(bytes){
8
var data = bytes;
9
var tempHex = ("0" + data[1].toString(16)).substr(-2) + ("0" + data[2].toString(16)).substr(-2);
10
var tempDec = parseInt(tempHex, 16);
11
var temperatureValue = calculateTemperature(tempDec);
12
var humidityValue = calculateHumidity(data[3]);
13
var batteryTmp = ("0" + data[4].toString(16)).substr(-2)[0];
14
var batteryVoltageCalculated = 2 + parseInt("0x" + batteryTmp, 16) * 0.1;
15
var reason = data[0];
16
var temperature = temperatureValue;
17
var humidity = humidityValue;
18
var batteryVoltage = batteryVoltageCalculated;
19
var thermistorProperlyConnected = decbin(data[5])[5] == 0;
20
21
var extT1 = ("0" + data[5].toString(16)).substr(-2)[1];
22
var extT2 = ("0" + data[6].toString(16)).substr(-2);
23
var extThermistorTemperature = thermistorProperlyConnected ? parseInt('0x'+extT1+extT2, 16) * 0.1 : 0;
24
25
// check if it is a keepalive
26
if (reason == 1) {
27
return {
28
reason: 'keepalive',
29
temperature: temperature,
30
humidity:humidity,
31
batteryVoltage: batteryVoltage,
32
thermistorProperlyConnected:thermistorProperlyConnected,
33
extThermistorTemperature:extThermistorTemperature
34
}
35
}
36
}
37
Copied!

Chirpstack Decoder (JavaScript ES5)

1
function Decode(fPort, bytes, variables) {
2
try{
3
var data = {};
4
var calculateTemperature = function (rawData){return (rawData - 400) / 10};
5
var calculateHumidity = function(rawData){return (rawData * 100) / 256};
6
function decbin (number) {
7
if (number < 0) {
8
number = 0xFFFFFFFF + number + 1;
9
}
10
return parseInt(number, 10).toString(2);
11
}
12
function handleKeepalive(bytes, data){
13
var tempHex = '0' + bytes[1].toString(16) + bytes[2].toString(16);
14
var tempDec = parseInt(tempHex, 16);
15
var temperatureValue = calculateTemperature(tempDec);
16
var humidityValue = calculateHumidity(bytes[3]);
17
var batteryTmp = ("0" + bytes[4].toString(16)).substr(-2)[0];
18
var batteryVoltageCalculated = 2 + parseInt("0x" + batteryTmp, 16) * 0.1;
19
var temperature = temperatureValue;
20
var humidity = humidityValue;
21
var batteryVoltage = batteryVoltageCalculated;
22
var thermistorProperlyConnected = decbin(bytes[5])[5] == 0;
23
var extT1 = ("0" + bytes[5].toString(16)).substr(-2)[1];
24
var extT2 = ("0" + bytes[6].toString(16)).substr(-2);
25
var extThermistorTemperature = 0;
26
if(thermistorProperlyConnected){
27
extThermistorTemperature = parseInt("0x"+extT1+""+extT2, 16) * 0.1;
28
}
29
data.sensorTemperature = Number(temperature.toFixed(2));
30
data.relativeHumidity = Number(humidity.toFixed(2));
31
data.batteryVoltage = Number(batteryVoltage.toFixed(2));
32
data.thermistorProperlyConnected = thermistorProperlyConnected;
33
data.extThermistorTemperature = extThermistorTemperature;
34
return data;
35
}
36
37
function handleResponse(bytes, data){
38
var commands = bytes.map(function(byte){
39
return ("0" + byte.toString(16)).substr(-2);
40
});
41
commands = commands.slice(0,-7);
42
var command_len = 0;
43
44
commands.map(function (command, i) {
45
switch (command) {
46
case '04':
47
{
48
command_len = 2;
49
var hardwareVersion = commands[i + 1];
50
var softwareVersion = commands[i + 2];
51
data.deviceVersions = { hardware: Number(hardwareVersion), software: Number(softwareVersion) };
52
}
53
break;
54
case '12':
55
{
56
command_len = 1;
57
data.keepAliveTime = parseInt(commands[i + 1], 16);
58
}
59
break;
60
case '19':
61
{
62
command_len = 1;
63
var commandResponse = parseInt(commands[i + 1], 16);
64
var periodInMinutes = commandResponse * 5 / 60;
65
data.joinRetryPeriod = periodInMinutes;
66
}
67
break;
68
case '1b':
69
{
70
command_len = 1;
71
data.uplinkType = parseInt(commands[i + 1], 16) ;
72
}
73
break;
74
case '1d':
75
{
76
command_len = 2;
77
var deviceKeepAlive = 5;
78
var wdpC = commands[i + 1] == '00' ? false : commands[i + 1] * deviceKeepAlive + 7;
79
var wdpUc = commands[i + 2] == '00' ? false : parseInt(commands[i + 2], 16);
80
data.watchDogParams= { wdpC: wdpC, wdpUc: wdpUc } ;
81
}
82
break;
83
default:
84
break;
85
}
86
commands.splice(i,command_len);
87
});
88
return data;
89
}
90
if (bytes[0] == 1) {
91
data = handleKeepalive(bytes, data);
92
}else{
93
data = handleResponse(bytes,data);
94
bytes = bytes.slice(-7);
95
data = handleKeepalive(bytes, data);
96
}
97
return data;
98
} catch (e) {
99
console.log(e)
100
throw new Error('Unhandled data');
101
}
102
}
Copied!