Linux Format

Pushover surveillan­ce

Les Pounder shows us how to set up our own private security camera, enabling you to monitor an area that you can view on any device.

- Les Pounder is a freelance maker who works with organisati­ons such as the Raspberry Pi Foundation to promote maker skills. Read his blog at bigl.es.

Les Pounder shows us how to set up our own private security camera system, enabling you to monitor an area that you can view on any device.

For this issue’s project we shall use Pushover, a notificati­on service for all devices along with the humble Raspberry Pi to create an intrusion system that will detect movement using an infrared sensor and take a picture of the intruder using the official Raspberry Pi camera. And all of this will then be 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 port (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 that the blue section is facing the Ethernet port. Then push the tabs down to lock the cable in place.

Now boot up your Pi and from the Desktop go to the Preference­s>Raspberry Pi Configurat­ion. Then click the Interfaces tab and ensure your camera is enabled. You will be asked to reboot, 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 will take a photo. You can view the photo in your Home directory.

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 connection­s made, the hardware build is now complete.

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 licence for desktop and our Android device. Head over to https://pushover.net and sign up for a new account and download the app from the Android Play store.

After setting up the account, create a new applicatio­n called “Alarm”, enter the descriptio­n, and then tick 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 will be a User Key. Again, note this down and keep it safe.

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

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

import requests, datetime from gpiozero import MotionSens­or from picamera import PiCamera from signal import pause from time import sleep

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 will get the time and date of the intrusion, and store 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”))

Intruder alert!

We then print that the alarm has been triggered, and the time of the intrusion before we then use the camera. capture function to take a picture and then 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 will tell the reader that an alert has been triggered, and 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 a read-only file. 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 will print in the shell and give 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 to meet your own 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, and then calling the function that we’ve created. Then we use pause() to prevent the code from exiting, effectivel­y running the code until the next intrusion is detected.

Save the code, and click Run>Run Module to start. Now run around and keep an eye on your Android device, as it will ping messages to your phone showing pictures of you! Now install the unit somewhere safe in a small box, and capture those intruders on camera!

 ??  ?? Using the Chrome web and the Android app we were able to see our intruder live on our devices with no network configurat­ion needed. Our intruder looks familiar!
Using the Chrome web and the Android app we were able to see our intruder live on our devices with no network configurat­ion needed. Our intruder looks familiar!
 ??  ??
 ??  ?? Insert the Pi camera with blue tab facing the Ethernet port For this project simply connect a PIR sensor to the GPIO, and the camera to its dedicated port.
Insert the Pi camera with blue tab facing the Ethernet port For this project simply connect a PIR sensor to the GPIO, and the camera to its dedicated port.
 ??  ?? Once we have created a Pushover account we can then easily create apps, such as this alarm project, which can then be used with multiple devices to boost your security around the home.
Once we have created a Pushover account we can then easily create apps, such as this alarm project, which can then be used with multiple devices to boost your security around the home.

Newspapers in English

Newspapers from Australia