-
Notifications
You must be signed in to change notification settings - Fork 59
/
ble-ruuvi.js
138 lines (130 loc) · 3.63 KB
/
ble-ruuvi.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
137
138
let CONFIG = {
scan_duration: BLE.Scanner.INFINITE_SCAN,
temperature_thr: 18,
switch_id: 0,
mqtt_topic: "ruuvi",
event_name: "ruuvi.measurement",
};
let RUUVI_MFD_ID = 0x0499;
let RUUVI_DATA_FMT = 5;
//format is subset of https://docs.python.org/3/library/struct.html
let packedStruct = {
buffer: '',
setBuffer: function(buffer) {
this.buffer = buffer;
},
utoi: function(u16) {
return (u16 & 0x8000) ? u16 - 0x10000 : u16;
},
getUInt8: function() {
return this.buffer.at(0)
},
getInt8: function() {
let int = this.getUInt8();
if(int & 0x80) int = int - 0x100;
return int;
},
getUInt16LE: function() {
return 0xffff & (this.buffer.at(1) << 8 | this.buffer.at(0));
},
getInt16LE: function() {
return this.utoi(this.getUInt16LE());
},
getUInt16BE: function() {
return 0xffff & (this.buffer.at(0) << 8 | this.buffer.at(1));
},
getInt16BE: function() {
return this.utoi(this.getUInt16BE(this.buffer));
},
unpack: function(fmt, keyArr) {
let b = '<>!';
let le = fmt[0] === '<';
if(b.indexOf(fmt[0]) >= 0) {
fmt = fmt.slice(1);
}
let pos = 0;
let jmp;
let bufFn;
let res = {};
while(pos<fmt.length && pos<keyArr.length && this.buffer.length > 0) {
jmp = 0;
bufFn = null;
if(fmt[pos] === 'b' || fmt[pos] === 'B') jmp = 1;
if(fmt[pos] === 'h' || fmt[pos] === 'H') jmp = 2;
if(fmt[pos] === 'b') {
res[keyArr[pos]] = this.getInt8();
}
else if(fmt[pos] === 'B') {
res[keyArr[pos]] = this.getUInt8();
}
else if(fmt[pos] === 'h') {
res[keyArr[pos]] = le ? this.getInt16LE() : this.getInt16BE();
}
else if(fmt[pos] === 'H') {
res[keyArr[pos]] = le ? this.getUInt16LE() : this.getUInt16BE();
}
this.buffer = this.buffer.slice(jmp);
pos++;
}
return res;
}
};
let RuuviParser = {
getData: function (res) {
let data = BLE.GAP.ParseManufacturerData(res.advData);
if (typeof data !== "string" || data.length < 26) return null;
packedStruct.setBuffer(data);
let hdr = packedStruct.unpack('<HB', ['mfd_id', 'data_fmt']);
if(hdr.mfd_id !== RUUVI_MFD_ID) return null;
if(hdr.data_fmt !== RUUVI_DATA_FMT) {
print("unsupported data format from", res.addr);
print("expected format", RUUVI_DATA_FMT);
return null;
};
let rm = packedStruct.unpack('>hHHhhhHBHBBBBBB', [
'temp',
'humidity',
'pressure',
'acc_x',
'acc_y',
'acc_z',
'pwr',
'cnt',
'sequence',
'mac_0','mac_1','mac_2','mac_3','mac_4','mac_5'
]);
rm.temp = rm.temp * 0.005;
rm.humidity = rm.humidity * 0.0025;
rm.pressure = rm.pressure + 50000;
rm.batt = (rm.pwr >> 5) + 1600;
rm.tx = (rm.pwr & 0x001f * 2) - 40;
rm.addr = res.addr.slice(0, -2);
rm.rssi = res.rssi;
return rm;
},
};
function publishToMqtt(measurement) {
MQTT.publish(
CONFIG.mqtt_topic + "/" + measurement.addr,
JSON.stringify(measurement)
);
}
function emitOverWs(measurement) {
Shelly.emitEvent(CONFIG.event_name, measurement);
}
function triggerAutomation(measurement) {
if (measurement.temp < CONFIG.temperature_thr) {
// turn the heater on
Shelly.call("Switch.Set", { id: CONFIG.switch_id, on: true });
}
}
function scanCB(ev, res) {
if (ev !== BLE.Scanner.SCAN_RESULT) return;
let measurement = RuuviParser.getData(res);
if (measurement === null) return;
print("ruuvi measurement:", JSON.stringify(measurement));
publishToMqtt(measurement);
emitOverWs(measurement);
triggerAutomation(measurement);
}
BLE.Scanner.Start({ duration_ms: CONFIG.scan_duration }, scanCB);