Sensor module code

Now, we write the code for sensor modules, which will govern the functioning of all the sensors and LED. To accomplish our task, we need an npm module called pigpio, which gives us access to the GPIO of Raspberry Pi. To install the pigpio module, run the sudo npm install pigpio command in the terminal as shown in Figure 6.25:

Figure 6.25

Check out the official npm link at https://www.npmjs.com/package/pigpio for more details about the pigpio module.

Create a file with the name Survillance.js and include the pigpio module. We also include CameraModule.js and EmailModule.js, which we developed in the previous section. This will give access to the functions of each module for taking pictures, videos, and sending emails:

var GPIO = require('pigpio').Gpio,
cameraModule = require('./CameraModule'),
emailModule = require('./EmailModule');

Make sure that files Survillance.js, EmailModule.js, and CameraModule.js are placed in the same directory.

As per the connection we made in the Wiring up section, we initialize the Raspberry Pi's pins for reading out and sending signals to sensors and LEDs:

var PIR_out= new GPIO(19,{mode: GPIO.INPUT,alert: true}),
red_LED= new GPIO(17,{mode: GPIO.OUTPUT}),
buzzer= new GPIO(26,{mode: GPIO.OUTPUT}),
IR_out= new GPIO(5,{mode: GPIO.INPUT,alert: true}),
trigger = new GPIO(16, {mode: GPIO.OUTPUT}),
echo = new GPIO(21, {mode: GPIO.INPUT, alert: true});

The PIR sensor's output (PIR_out) is connected to GPIO 19, so we declared GPIO 19 as the input and set the alert event flag to true. The alert event indicates that whenever the value at the GPIO changes from low (0) to high (1) or vice versa, then an alert event will be raised. We can listen to the alert event and take action accordingly.

The positive terminal of LED (red_LED) is connected to GPIO 17. The LED will work as an indicator; whenever a trespasser is detected, the LED will glow. GPIO 17 is set as the OUTPUT pin.

The buzzer is connected to the Pi with one of its terminals connected to GPIO 26 and the other connected to the ground. The buzzer is used to raise an alarm when an intruder is detected, so we make GPIO 26 as OUTPUT.

The infrared sensor's output (IR_out) is connected to GPIO 5, which is declared as the INPUT pin, and the alert event flag is set to true as we did for the PIR sensor.

The trigger terminal of the ultrasonic sensor is used to generate high frequency ultrasonic waves. The trigger pin is connected to GPIO 16, so we declared it as the OUTPUT pin.

The echo pin of the ultrasonic sensor is connected to GPIO 21. The echo pin is used to get the output of the ultrasonic sensor, so it is declared as INPUT and the alert event flag is set to true.

Make sure you initialize the LED to low (0) level:

red_LED.digitalWrite(0);

Let's first write the code block to read the data of the PIR sensor:

PIR_out.on('alert', function(level, tick){
if(level==1) {
cameraModule.takePicture(function (callback) {
var result = callback;
if (result == 'success') {
emailModule.sendMailPhoto()
}
})
console.log('PIR : Intruder Alert..!!')
red_LED.digitalWrite(level);
buzzer.digitalWrite(level);
}
else {
red_LED.digitalWrite(level);
buzzer.digitalWrite(level);
}
})

Here, we listen to the alert event for the PIR sensor. As soon as the PIR sensor detects any trespasser in the house, the output goes high from the initial low state, which causes an alert event to fire. The alert function returns a callback with two parameters: one is level and the other one is tick. The level indicates the state of the GPIO at that moment and tick is the timestamp at which the change in state is observed at the GPIO. So we check the alert event and if the level is high (1), then we immediately take a photo of the trespasser by calling the takePicture function of the camera module; then, we raise the alarm by setting the output of the buzzer and LED indicator to high. Once we receive the success as callback from the camera module's takePicture function, we call the sendMailPhoto function of the email module, which will send the alert email along with the photo of the trespasser to the owner of the house.

Now, we write the code for the IR sensor, which is very much similar to the PIR sensor code:

IR_out.on('alert', function(level, tick){
if(level==1){
cameraModule.takeVideo(function (callback) {
var result = callback;
if(result == 'success'){
emailModule.sendMailVideo() ;
}
})
console.log('IR : Intruder Alert..!!'
red_LED.digitalWrite(level);
buzzer.digitalWrite(level);
}
else {
red_LED.digitalWrite(level);
buzzer.digitalWrite(level);
}
})

An IR sensor works in the exact same way as the PIR sensor. The only difference is that we take a video recording instead of taking a still photo.

Lastly, we write the code block for an ultrasonic sensor module:

  trigger.digitalWrite(0);
var MICROSECDONDS_PER_CM = 1000000/33000;
// Trigger a distance measurement once per second
setInterval(function () {
trigger.trigger(10, 1); // Set trigger high for 10 microseconds
}, 5000);
//The number of microseconds it takes sound to travel 1cm at 20 degrees celcius
var startTick;
echo.on('alert', function (level, tick) {
var endTick,
diff;
if (level == 1) {
startTick = tick;
}
else {
endTick = tick;
diff = (endTick >> 0) - (startTick >> 0); //Unsigned 32-bit arithmetic
var actualDist = (diff / 2 / MICROSECDONDS_PER_CM) ;
if (actualDist <10){
console.log('Ultrasonic : Intruder Detected...!!')
red_LED.digitalWrite(1);
buzzer.digitalWrite(level);
cameraModule.takePicture(function (callback) {
var result = callback;
if (result == 'success'){
emailModule.sendMailPhoto() }
})
}
else {
red_LED.digitalWrite(0);
buzzer.digitalWrite(0);
}
}
});

Making use of the trigger function of the pigpio module, we send a high pulse on the trigger pin of the ultrasonic sensor for 10 microseconds, which causes the sensor transmitter to emit a burst 8 high-frequency pulse, which makes the output at the echo pin high.

The output of the ultrasonic sensor is taken from the Echo pin on which we are listening for the alert event. As soon as the ultrasonic wave is transmitted, the state of the Echo pin changes to high and the alert event is fired, which returns a callback with two parameters. The first is the level, which is used to check whether the state of echo is high (1) or low (0) and the other is tick, which is used to check the timestamp when the state changed. The state of the echo is changed from low to high and we save this timestamp as startTick. When the waves get reflected from obstruction and fall on the receiver, the state of the Echo pin is changed from high to low; we save this timestamp as endTick. Now, to get the distance of the obstruction from the sensor, we need to get the time the ultrasonic waves took to travel, to and from the sensor to obstruction and back to the sensor; we need to subtract startTick from endTick. The tick is actually the number of microseconds since the system boot. Its value is an unsigned 32-bit quantity. So, to get the correct value after subtraction, we use the right shift operator.
Once we have the travel time, we then calculate the distance using the formula knowing the wave speed (330 m/s). This means that, in 1 second, ultrasonic waves travel 330 meters (in 1,000,000 micro seconds the waves travel 33,000 cm).

Once we have the distance, we then check whether the distance is less than 10 cm. If it is, we raise an alert by lighting the LED, beeping the Buzzer, taking a picture, and finally sending the alert through email.

We have completed the code part. Now, let's execute the code by running the command in the terminal:

sudo node Survillance.js

We have written a few statements to log the data on the console to understand the execution of this code block. The output for the PIR sensor is shown in the console (as shown in Figure 6.26):

Figure 6.26

The output of the IR sensor is shown in Figure 6.27:

Figure 6.27

The output of the ultrasonic sensor is shown in Figure 6.28:

Figure 6.28

Figure 6.29 shows a snapshot of the email notification received as an alert:

Figure 6.29
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.221.165.246