-
Notifications
You must be signed in to change notification settings - Fork 0
/
ttn.js
59 lines (52 loc) · 1.41 KB
/
ttn.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
function decodeUplink(input) {
var bytes = input.bytes;
var header = bytes[0];
var headerValue = "?";
switch (header) {
case 0:
headerValue = "BOOT";
break;
case 1:
headerValue = "UPDATE";
break;
case 2:
headerValue = "BUTTON_CLICK";
break;
case 3:
headerValue = "BUTTON_HOLD";
break;
case 4:
headerValue = "DOOR_OPEN";
break;
case 5:
headerValue = "DOOR_CLOSE";
break;
}
var voltage = null;
if (bytes[1] != 255)
voltage = bytes[1] / 10.0;
var doorState = bytes[2] == 1 ? "closed" : "open";
var temperature = ((bytes[3] << 8) | bytes[4]) / 10.0;
var decoded = {
header: header,
headerInfo: headerValue,
voltage: voltage,
door: doorState,
temperature: temperature,
soilSensors: []
};
// soil sensors
var pos = 5;
var sensorsCount = (bytes.length - 5) / 4;
for (i = 0; i < sensorsCount; i++) {
var soilTemperature = ((bytes[pos++] << 8) | bytes[pos++]);
var soilHumidity = ((bytes[pos++] << 8) | bytes[pos++]);
if (soilHumidity == 65535 || soilTemperature == 65535)
continue;
decoded.soilSensors.push({
temperature: soilTemperature / 10,
humidity: soilHumidity
});
}
return decoded;
}