-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathnotifications.js
54 lines (44 loc) · 1.25 KB
/
notifications.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
const https = require('https');
const keys = require('./keys');
const mhz19bSensor = require('./sensors/MHZ19B');
mhz19bSensor.on('data', mhz19bMeasureEvent);
var co2 = 0;
var limit = 700;
function mhz19bMeasureEvent(data) {
try {
let newCo2 = data['co2'];
if (newCo2 >= limit && co2 < limit) {
sendPhoneNotification(`CO2 High: ${newCo2} ppm`);
}
if (newCo2 < limit && co2 >= limit) {
sendPhoneNotification(`CO2 OK: ${newCo2} ppm`);
}
co2 = newCo2;
} catch (error) {
console.log('Error with notification MH-Z19b measure event', error);
}
}
function sendPhoneNotification(message) {
const options = {
hostname: 'maker.ifttt.com',
port: 443,
path: `/trigger/sensor/with/key/${keys.MakerKey}`,
method: 'POST',
headers: {
'Content-Type': 'application/json'
}
}
const req = https.request(options, (res) => {
console.log(`statusCode: ${res.statusCode}`);
res.on('data', (d) => {
})
})
req.on('error', (error) => {
console.log('sendPhoneNotification error', error);
})
const data = JSON.stringify({
value1: message
})
req.write(data);
req.end();
}