-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
102 lines (94 loc) · 2.88 KB
/
index.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
require('dotenv').config()
const Gpio = require('pigpio').Gpio;
const exec = require('child_process').exec;
let toggleMonitor = false;
let togglePage = false;
const Button = new Gpio(25, {
mode: Gpio.INPUT,
pullUpDown: Gpio.PUD_UP,
alert: true
});
let transferData = [];
const PIRSENSOR = new Gpio(23, {
mode: Gpio.INPUT,
alert: true
});
const BME280 = require('bme280-sensor');
const mqtt = require('mqtt');
const mqttClient = mqtt.connect(process.env.MQTT_ADDRESS);
// The BME280 constructor options are optional.
//
const options = {
i2cBusNo : 1, // defaults to 1
i2cAddress : BME280.BME280_DEFAULT_I2C_ADDRESS() // defaults to 0x77
};
const bme280 = new BME280(options);
// Read BME280 sensor data, repeat
//
const readSensorData = () => {
bme280.readSensorData()
.then((data) => {
// temperature_C, pressure_hPa, and humidity are returned by default.
data.temperature_C = (Number(data.temperature_C) + Number(process.env.TEMP_ADJUST)).toFixed(2);
data.pressure_hPa = (Number(data.pressure_hPa) + Number(process.env.PRESSURE_ADJUST)).toFixed(2);
data.humidity = data.humidity.toFixed(2);
data.timing = new Date();
transferData.push(data)
if (transferData.length >5) {
transferData.shift();
}
console.log(`data = ${JSON.stringify(transferData)}`);
setTimeout(readSensorData, 900000);
})
.catch((err) => {
console.log(`BME280 read error: ${err}`);
setTimeout(readSensorData, 2000);
});
};
// Initialize the BME280 sensor
//
bme280.init()
.then(() => {
console.log('BME280 initialization succeeded');
readSensorData();
mqttClient.publish('tempSensor', JSON.stringify(transferData));
})
.catch((err) => console.error(`BME280 initialization failed: ${err} `));
function MonitorOff() {
setTimeout(function() {
child = exec('vcgencmd display_power 0',
(error, stdout, stderr) => {
if (error !== null) {
console.log(`exec error: ${error}`);
};
toggleMonitor = false;
mqttClient.publish('MonitorOn', String(toggleMonitor));
console.log(`Monitor on : ${toggleMonitor}`);
});
}, 600000);
}
PIRSENSOR.on('alert', (level) => {
if (!toggleMonitor) {
child = exec('vcgencmd display_power 1',
(error, stdout, stderr) => {
if (error !== null) {
console.log(`exec error: ${error}`);
return
}
MonitorOff();
mqttClient.publish('tempSensor', JSON.stringify(transferData));
toggleMonitor = true;
mqttClient.publish('MonitorOn', String(toggleMonitor));
togglePage = false;
console.log(`Monitor on : ${toggleMonitor}`);
});
};
});
Button.glitchFilter(10000);
Button.on('alert', (level, tick) => {
if (level) {
togglePage = !togglePage;
mqttClient.publish('pageBtn', String(togglePage));
console.log(togglePage);
}
});