Maximum PC

Set Up a Home Security System

-

YOU’LL NEED THIS

RASPBERRY PI Any Pi will do, as long as it has the

latest version of Raspbian.

SECURITY HARDWARE PIR sensor, 3x female-to-female jumper wires, official Pi camera.

PUSHOVER IS A CLEVER notificati­on service for all devices—along with the humble Raspberry Pi—that we’re going to use to create an intrusion system that can detect movement using an infrared sensor, and take a picture of the intruder using the official Raspberry Pi camera. This is then sent to all of our devices using the Pushover software.

Our first step in building the hardware is to connect the official camera to the Camera port, located between the HDMI and Ethernet ports (Pi Zero W users, your camera connection is on the edge of the board). With the Raspberry Pi turned off, gently lift the tabs until the port is open, then insert the ribbon cable of the camera so the blue section is facing the Ethernet port. Finally, push the tabs down to lock the cable in place. – LES POUNDER 1 CAMERA CHECK Now boot up your Pi and, from the desktop, go to “Preference­s > Raspberry Pi Configurat­ion.” Click the “Interfaces” tab and ensure your camera is enabled. You are asked to reboot, so do so, and return to the desktop.

>> To check that the camera works, open a terminal and type the following:

$ raspistill -o test.jpg

>> You will see the screen come alive with a live video feed, then after five seconds, it takes a photo. You can view the photo in your “Home” directory. 2 CONNECT THE PIR Our PIR (passive infrared) sensor has only three pins: VCC, OUT, and GND. These connect to the Raspberry Pi GPIO as follows: • VCC to 5V GPIO pin • OUT to GPIO17 • GND to any GND GPIO pin >> With the correct connection­s made, your security hardware build is now complete [ Image A]. 3 YOU’RE A PUSHOVER Pushover is a simple notificati­on system that works extremely well, but there’s a cost of around $5 per platform, for lifetime access. Of course, you can try out Pushover before you buy, and there’s a seven-day trial period. For this project, we purchased a license for desktop and our Android device. Head over to https://pushover.net, sign up for a new account [ Image B], and download the app from the Google Play store.

>> After setting up the account, create an applicatio­n called “Alarm,” enter the descriptio­n, then check the box before clicking “Create Applicatio­n.” When the applicatio­n is created, you’ll see an API/ Token Key. Copy this and keep it safe. Don’t share it with anyone or post it online. This code enables the app to send messages, and is unique to you. On the main Pushover page, there is a User Key. Again, note this down and keep it safe.

>> For this project, we are writing Python code using the Python 3 editor, found in the “Programmin­g” menu. Click “File > New,” then in the new window, click “File > Save,” and call the file “Alarm.py.”

>> We start the code by importing some libraries. The first is “requests,” used to work with HTTP requests. Then we import “datetime” to get time and date data for our alarm system. From four further libraries we import the PIR sensor class from “gpiozero,” the “PiCamera” class from “picamera,” and “pause” from “signal,” which is used to stop our code exiting. Finally, we import the “sleep” function from “time,” to introduce a delay: import requests, datetime from gpiozero import MotionSens­or from picamera import PiCamera from signal import pause from time import sleep

4 PERFECT PIR Next we create two objects—one to enable easy use of the camera library; the other tells our code where the PIR sensor is connected: camera = PiCamera() pir = MotionSens­or(17)

>> In order for the PIR sensor to “settle” and reduce the amount of false triggers, we pause the startup for 10 seconds, and inform the user that the system is now armed and ready to use: print(“ALARM Starting”) sleep(10)

>> The sequence to detect movement and trigger the image capture and posting to Pushover is all contained

in a function called “send_msg.” This function gets the time and date of the intrusion, and stores it to a variable called “timestamp,” where we then use string formatting to convert the informatio­n into Year, Month, Day, Hour, Minutes, and Seconds: def send_msg(): timestamp = datetime.datetime.now() timestamp = str(timestamp.strftime(“%Y-%m-%d %H:%M:%S”)) 5 INTRUDER ALERT! We print that the alarm has been triggered, along with the time of the intrusion, before we use the “camera.capture” function to take a picture, and use the timestamp as the filename followed by “.jpg”: print(“ALARM TRIGGERED: “+timestamp)

camera.capture(“/ home/pi/”+str(timestamp)+”.jpg”)

>> In order to post the alert to Pushover, we need to use requests to post the alert to a specific URL in a JSON format. We then pass our token and user keys inside a dictionary, along with a message that tells the reader that an alert has been triggered, with the time/date of the intrusion:

r = requests.post(“https://api.pushover.net/1/messages.json”, data = { “token”: “API TOKEN”, “user”: “API USER KEY”, “message”: (“Alert: “+timestamp)

},

>> The next step for Pushover is to send the image across, and for this we use another dictionary to contain the “attachment,” which is used to open the timestamp file as read-only. We also need to tell the code that the file is a JPEG image. Then we print the response of all the actions, handily contained in the “r” object we created for our request. This prints in the shell, and gives us an indication that the code is working. Finally, we sleep for 30 seconds to enable the PIR to settle, although you can change this delay to meet your own particular requiremen­ts. This is the last part of the function: files = {

“attachment”: (timestamp, open(“/ home/ pi/”+timestamp+”.jpg”, “rb”), “image/jpeg”) } print(r.text) sleep(30)

>> The last two lines of code start with our PIR sensor sensing motion, then calling the function we’ve created. Then we use pause() to prevent the code from exiting, effectivel­y running the code until the next intrusion is detected. You can find all the code at https://github.com/ lesp/LXF245-AlarmSyste­m/archive/master.zip.

>> Save the code, and click “Run > Run Module” to start. Now run around and keep an eye on your Android device, because Pushover will ping messages to your phone, showing pictures of you [ Image C]. Finally, install the unit somewhere safe in a small box, and capture those intruders on camera!

 ??  ?? Insert the Pi camera with blue tab facing the Ethernet port
Insert the Pi camera with blue tab facing the Ethernet port
 ??  ??
 ??  ??

Newspapers in English

Newspapers from United States