-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.js
54 lines (44 loc) · 1.15 KB
/
server.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
var http = require('http');
var Gpio = require('onoff').Gpio,
led = new Gpio(14, 'out');
var i2c = require('i2c');
var address = 0x48;
var wire = new i2c(address, {device: '/dev/i2c-1', debug: false}); // point to your i2c address, debug provides REPL interface
var data = [];
setInterval(function () {
wire.readBytes(0, 2, function (err, res) {
if (err) {
console.log(err);
throw err;
}
console.log("Analog gigrometer say: %d", res[1]);
var date = new Date();
data.push(date.valueOf() + ":" + res[1]);
});
}, 1000 * 60 * 60 * 2); //Every 2 hour;
led.write(1, function (err) {
if (err) throw err;
setInterval(function () {
led.read(function (err, value) {
if (err) throw err;
var v = value == 0 ? 1 : 0;
led.write(v, function (err) {
if (err) throw err;
setTimeout(function(){
led.write(!v, function (err) {
if (err) throw err;
});
},100);
});
});
}, 2000);
});
process.on('SIGINT', function exit() {
led.unexport();
process.exit();
});
http.createServer(function (request, response) {
response.writeHead(200, {"Content-Type": "text/plain"});
response.end(data.join(';'));
data = [];
}).listen(5000);