-
Notifications
You must be signed in to change notification settings - Fork 0
/
rpi-node-temp.js
42 lines (32 loc) · 1.05 KB
/
rpi-node-temp.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
#!/usr/bin/node
var exec = require('child_process').exec;
var tempId = '28-00000249bf39',
dispTempInterval,
dispTempIntervalClock = 10; //sec
var colors = {
'red' : '\u001b[31m',
'green' : '\u001b[32m',
'yellow' : '\u001b[33m',
'blue' : '\u001b[34m',
'reset' : '\u001b[0m'
};
var log = function( msg ){
var now = new Date();
console.log( now.toUTCString() + " " + msg + colors.reset );
}
var dispTemp = function(){
exec( "cat /sys/bus/w1/devices/" + tempId + "/w1_slave | grep t= | cut -f2 -d= | awk '{print $1/1000}'", function( error, stdout, stderr ){
if ( error != null ){
log( colors.red + "Error: " + error);
}
var temp = parseFloat(stdout).toFixed(2),
color = (temp > 30 ) ? colors.red : colors.green;
log ( color + "Current temperature: " + temp + "°C" );
});
}
log( colors.blue + 'Started node-temp');
log( colors.blue + '-----------------');
/* Start interval */
dispTempInterval = setInterval( function(){
dispTemp();
}, dispTempIntervalClock*1000 );