-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathindex.js
73 lines (60 loc) · 2.09 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
/*
* Author @ Rocco Musolino - Hackerstribe.com
*/
//var fs = require('fs');
var Gpio = require('onoff').Gpio;
//var exec = require('child_process').exec;
var functions = require('./functions.js'); // functions
var config = require('./config.json'); // Configuration file
var argv = require('./arguments-handler.js');
var moment = require('moment');
var pin = new Gpio(argv.pin || config.PIN_NUMBER, 'out'); // Check out RPi2 Pinout map
// Start Loop
(
function loop(){
var iter = setInterval(function(){
functions.execute('/opt/vc/bin/vcgencmd measure_temp', function(data){
if (functions.parse_temp(data) >= (argv.temperature || config.TEMPERATURE_THRESHOLD)){
if (functions.allowedInterval(moment())){
pin.read(function(err, value){
if (value == 0){
pin.writeSync(1); // ON
console.log(new Date().toString(), ' - Fan ON! - ', data);
}
});
clearInterval(iter);
setTimeout(loop, 300 * 1000); // Fan on at least for 5 minutes
}
}else{
pin.read(function(err, value){
if (value == 1){
pin.writeSync(0); // OFF
console.log(new Date().toString(), ' - Fan OFF! - ', data);
}
});
}
});
}, (argv.refreshtime || config.REFRESH_TIME) * 1000);
}()
);
// Web Server
if (config.WEB_SERVER){
var http = require('http');
var server = http.createServer(function (req, res) {
res.writeHead(200, {'Content-Type': 'text/html'});
functions.execute('/opt/vc/bin/vcgencmd measure_temp', function(data){
pin.read(function(err, val){
if (err) console.log('Error: '+ err);
var logs = functions.replaceAll(functions.getLogs(), '\n', '<br/>');
res.end('<u>Current temperature</u>: <b>'+functions.parse_temp(data)+'</b> - <u>Fan status</u>: <b>'+((val == 1) ? 'on' : 'off')+'</b><br/><br/><u>Logs</u>:<br/><br/>'+logs);
});
});
})
server.listen(argv.port || config.SERVER_PORT, '0.0.0.0');
console.log(new Date().toString(), ' - Fan web server running at http://0.0.0.0:'+config.SERVER_PORT+'/');
}
function exit() {
pin.unexport(); // OFF and release the GPIO
process.exit();
}
process.on('SIGINT', exit);