-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
73 lines (63 loc) · 1.54 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
/*
* IoT Project Series #2
*
* A Surveillance system which detects motion
* then sends a mail to the owner of the system
*
* This is a code that runs on node.js
* and is used to control the Raspberry Pi it connectS to
*
* @author Nwachukwu Chibuike <[email protected]>
* @copyright 2018 IoT Project Series
*/
var Gpio = require("onoff").Gpio;
var LED1 = new Gpio(2, "out");
var LED2 = new Gpio(4, "out");
var LED3 = new Gpio(17, "out");
var state = 2;
var pir = new Gpio(27, "in", "both");
pir.watch(function(err, value) {
if (err) exit();
var blinkInterval = setInterval(blinkLED, 250);
console.log("Intruder detected");
console.log("Pi Bot deployed successfully!");
console.log("Guarding the raspberry pi 3...");
if (value == 1) require("./mailer").sendEmail();
setTimeout(endBlink, 15000);
function endBlink() {
clearInterval(blinkInterval);
LED1.writeSync(0);
LED1.unexport();
LED2.writeSync(0);
LED2.unexport();
LED3.writeSync(0);
LED3.unexport();
//included when we are working with sensors
pir.unexport();
process.exit();
}
});
function blinkLED() {
if (state == 2) {
if (LED1.readSync() === 0) {
LED1.writeSync(1);
} else {
LED1.writeSync(0);
state = 4;
}
} else if (state == 4) {
if (LED2.readSync() === 0) {
LED2.writeSync(1);
} else {
LED2.writeSync(0);
state = 6;
}
} else {
if (LED3.readSync() === 0) {
LED3.writeSync(1);
} else {
LED3.writeSync(0);
state = 2;
}
}
}