-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathcombined.js
136 lines (112 loc) · 3.3 KB
/
combined.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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
/*
* Decoder function for The Things Network to unpack the payload of the Smart Building Sensors
*
* This function was created by Cameron Sharp at Sensational Systems - [email protected]
*/
function Decoder(bytes, port) {
var params = {
"bytes": bytes
};
if (port === 103) {
// Healthy Home Sensor
// VOC Measurement
voc = (bytes[7] << 8) | bytes[6];
if (voc === 65535) {
voc_error = true;
} else {
voc_error = false;
}
// CO2 Measurement
co2 = (bytes[5] << 8) | bytes[4];
if (co2 === 65535) {
co2_error = true;
} else {
co2_error = false;
}
// IAQ Measurement
iaq = (bytes[9] << 9) | bytes[8];
// Humidity Measurement
rh = bytes[3] &= 0x7f;
if (rh === 127) {
rh_error = true;
} else {
rh_error = false;
}
// Board temp measurement
temp_board = bytes[2] & 0x7f;
temp_board = temp_board - 32;
// Ambient temp measurement
temp_ambient = bytes[10] & 0x7f;
temp_ambient = temp_ambient - 32;
// Battery measurements
batt = bytes[1] & 0x0f;
batt = (25 + batt) / 10;
params.voc = voc;
params.voc_error = voc_error;
params.co2 = co2;
params.co2_error = co2_error;
params.iaq = iaq;
params.rh = rh;
params.rh_error = rh_error;
params.temp_board = temp_board;
params.temp_ambient = temp_ambient;
params.batt = batt;
} else if (port === 100) {
// Door Sensor
// Temp measurement
temp = bytes[2] & 0x7f;
temp = temp - 32;
// Battery measurements
batt = bytes[1] & 0x0f;
cap = bytes[1] >> 4;
batt = (25 + batt) / 10;
cap = (cap / 15) * 100;
// Time measurement
time = (bytes[4] << 8) | bytes[3];
// Count measurement
count = ((bytes[7] << 16) | (bytes[6] << 8)) | bytes[5];
// Status measurement
status = bytes[0] & 0x1;
if (status === 1) {
open = true;
} else {
open = false;
}
params.temp = temp;
params.batt = batt;
params.cap = cap;
params.time = time;
params.count = count;
params.open = open;
params.port = port;
} else if (port === 102) {
// PIR Sensor
// Temp measurement
temp = bytes[2] & 0x7f;
temp = temp - 32;
// Battery measurements
batt = bytes[1] & 0x0f;
cap = bytes[1] >> 4;
batt = (25 + batt) / 10;
cap = (cap / 15) * 100;
// Time measurement
time = (bytes[4] << 8) | bytes[3];
// Count measurement
count = ((bytes[7] << 16) | (bytes[6] << 8)) | bytes[5];
// Status measurement
status = bytes[0] & 0x1;
if (status === 1) {
occupied = true;
} else {
occupied = false;
}
params.temp = temp;
params.batt = batt;
params.cap = cap;
params.time = time;
params.count = count;
params.occupied = occupied;
params.port = port;
}
return params;
}