-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathpir.js
47 lines (37 loc) · 1007 Bytes
/
pir.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
/*
* 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
};
// 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;
}