Explained: How to guard your home with Raspberry Pi

  • Hi there and welcome to PC Help Forum (PCHF), a more effective way to get the Tech Support you need!
    We have Experts in all areas of Tech, including Malware Removal, Crash Fixing and BSOD's , Microsoft Windows, Computer DIY and PC Hardware, Networking, Gaming, Tablets and iPads, General and Specific Software Support and so much more.

    Why not Click Here To Sign Up and start enjoying great FREE Tech Support.

    This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.
  • Hello everyone We want to personally apologize to everyone for the downtime that we've experienced. We are working to get everything back up as quickly as possible. Due to the issues we've had, your password will need to be reset. Please click the button that says "Forgot Your Password" and change it. We are working to have things back to normal. Emails are fixed and should now send properly. Thank you all for your patience. Thanks, PCHF Management

PCHF IT Feeds

PCHF Tech News
PCHF Bot
Jan 10, 2015
52,145
26
pchelpforum.net
mainimage-470-75.jpg

Introduction


The Raspberry Pi is an impressive platform for prototyping projects of any scale. From a simple blinking LED to a quadcopter, anything is possible with a Raspberry Pi – all you need is a little imagination and some extra components.

One of the most interesting areas of practical use is sensing the world around us, and using the data gathered in all sorts of ways. The world is full of data just waiting to be recorded and manipulated – for example, weather stations come with many different sensors to measure temperature, wind speed, humidity and pressure.

All this data can be stored and manipulated by a savvy hacker to produce graphs and tables that can be imported into other applications or projects. And of all the sensors on the market, the cheapest and most simple is the humble PIR, which is present in so many parts of everyday life.

mainimage2-420-90.jpg


PIR plans


Passive infrared sensors (PIR) are commonly used in devices such as triggers for burglar alarms in homes and offices. In fact, they are so cheap that they're found inside motion-activated air fresheners, which could be a ripe source of components for this project.

The PIR sensor works on the simple principle of sending a beam of infrared light into a room. If the beam remains unbroken – by which we mean there is no movement in the room – no action is taken.

But the second the beam is broken, a signal is sent to a device that's programmed to respond in a certain manner. These PIR sensors are very cheap – around £4 (about $6, AU$8.40) delivered on eBay – and they are also an excellent starter project because they require very little breadboarding or prototyping.

For our project, we will set up a PIR sensor to watch an area for any movement. Once movement is detected, it will trigger the code to complete a sequence of events that will capture a picture of the event and record a short 10-second video at a resolution of 640 pixels by 480 pixels.

The project will culminate with a text message being sent to our phone, alerting us to an intruder or other event. To explain how this project will work, we'll illustrate it using pseudocode, which is a way of explaining a programming sequence using easy-to-understand language. Here's how the project will work in pseudo code:

PIR sensor sends out a beam.

If the beam is broken.

Send a signal to our Raspberry Pi.

On receiving the signal, the Raspberry Pi will take a picture of the activity.

The Pi will then record 10 seconds of video.

After the picture is created, the Pi will attempt to send a text alerting us to the activity.

This text will contain a picture and video .

Once the text has been sent, the Pi will wait for 30 seconds before looping the process.

So where could you use this project? The most obvious answer would be home security but let's think of something nicer.

In the summer there are lots of plants growing and animals moving in the sunshine, so why not use your Raspberry Pi and this project to capture the joys of summer?

You could house this project in a weatherproof case and leave it outside to record animal life. In fact, a project similar to this was used to monitor the feeding patterns of birds caring for their offspring.

A sensor was placed near the nesting box, and this would record the comings and goings of the parent birds. This data was then linked to weather data for that area.

When comparing the data, it became clear that the natural food of birds – insects – were not as abundant when the weather was rainy or windy. This meant the offspring were not being fed as often.

Alert! Alert! In this project, we're choosing to alert the user via a text message, because no matter what type of phone you have, and what type of signal you receive, a text message is more reliable as a method of delivery than an email or tweet, which rely on 3G or Wi-Fi coverage to receive data.

This project can be built upon to include other methods of delivering the alert – in fact, you can send more than one alert from the project.

To send an email, you can use the smtplib, and we found a great how-to. You can also send a tweet from Python using the tweepy library.

To do this you will need to create an application via https://dev.twitter.com. Once you have completed this part of the process, you can import the API key into tweepy and use it to send and receive tweets.

You can read more about tweepy via its website. The pictures and video that are captured in this project are stored on our Raspberry Pi for later retrieval via any means that is convenient.

Get kitted out


In brief, our goal is to build an alarm system with our Raspberry Pi that's triggered by a PIR sensor, which detects movement.

Once the alarm is tripped, a camera will take a quick picture of the event, and then record 10 seconds of video. To alert the user that the alarm has been triggered a text message is sent. So let's get started!

kitted1-420-90.jpg


1. Connect the PIR


Our PIR sensor needs three connections to work: VCC (5V), Out and Ground (GND). To connect our sensor to the Raspberry Pi, we need to use female to female jumper cables.

You can buy them from many online retailers. On the sensor, locate the VCC pin and gently push a cable until it is firmly in place.

On your Raspberry Pi, locate Pin 2 and gently push the VCC cable into place. We connect the VCC pin on the PIR sensor to a 5V pin on a Raspberry Pi, which is Pin 2.

When connecting anything to the GPIO, ensure that the Raspberry Pi is powered off and that all connections are checked before powering on your Raspberry Pi.

kitted2-420-90.jpg


2. Ground and Out


We then need to repeat the process and connect the GND pin of our sensor to GND on a Raspberry Pi, which is Pin 6. Lastly, we connect our Out pin to Pin 7.

For our connection to the PIR sensor, we will require no resistors inline as the PIR has a diode to protect the board, and the signal sent via the Out pin is 3V and safe with our Raspberry Pi.

Our PIR sensor needs three connections to work: VCC (5V), Out and Ground (GND). To connect our sensor to the Raspberry Pi, we need to use female to female jumper cables. You can buy them from many online retailers. On the sensor, locate the VCC pin and gently push a cable until it is firmly in place.

On your Raspberry Pi, locate Pin 2 and gently push the VCC cable into place. We connect the VCC pin on the PIR sensor to a 5V pin on a Raspberry Pi, which is Pin 2. When connecting anything to the GPIO, ensure that the Raspberry Pi is powered off and that all connections are checked before powering on your Raspberry Pi.

kitted3-420-90.jpg


3. Connect the camera


Our next piece of hardware is the Raspberry Pi Camera, and to insert the camera, you need to locate a plastic port between the HDMI and Ethernet port on the board.

Gently lift the plastic lock from the port and then slide the ribbon cable into the slot, with the silver strips of the ribbon facing the HDMI port.

Once fully slid into place, press the plastic lock back into its original position. The ribbon cable will now be locked in place.

Software setup


soft1-420-90.jpg


1. Enable the camera


Now we have our hardware installed, it's time to configure the software that will control it. Firstly we need to set up our camera and that is done via this command – in a terminal type:

sudo raspi-config

In the menu, there is an option to 'Enable the camera' – move to there and then press [Enter]. You will now be asked whether you would like to enable the camera, so move the cursor to 'Enable' and press [Enter]. You will now return to the original menu, so move your cursor to 'Finish' and press [Enter].

soft2-420-90.jpg


2. Test the camera


Now we have told the Raspberry Pi to use the camera, we need to test that it can take a picture, and the best way to do that is via this command in the terminal:

raspistill -o test.jpg

This command will launch an application to take a picture using the camera. If you encounter any errors, check that you have the correct syntax and that the camera is enabled in the raspi-config menu.

The last thing to check should be the physical connection between the camera and the Raspberry Pi.

soft3-420-90.jpg


3. Install pip


In order to use the camera with Python, we need to download the library picamera, and the best way to do this is via a package manager for Python called pip.

Pip works in the same way that apt-get works on your Raspberry Pi, and it is a great tool to keep your projects up to date. To use pip we first need to install it via a terminal.

soft4-420-90.jpg


4. Install picamera


To install pip, type in the following:

sudo apt-get update

sudo apt-get install python-pip

sudo pip install picamera

Pip will now install the picamera library for Python. Now it is time to grab a copy of the project code. Download the project as a zip file.

soft5-420-90.jpg


5. Open the project files


To use the project files, we need to open idle, the Python editor, but because we are using the GPIO, we need to do so using sudo:

sudo idle

This will open the idle application, and in the application, go to 'File > Open' and then navigate to where your project files are located. We will be working with pir_alarm.py.

soft6-420-90.jpg


6. Added extras


In our code we have a section that imports extra functionality; these are libraries. The first is time, which enables control of our program.

Next we import datetime, which enables our program to use calendar dates and clock times. Next we import the RPi.GPIO library but rename it to GPIO to make it easier to use.

We then import picamera which enables the use of the official Pi camera with Python.

Now for the code


code1-420-90.jpg


1. Store the pin number


Earlier we referred to connecting the Out pin of the PIR sensor to Pin 7 of our Raspberry Pi.

Well, in order to be efficient with our code, we need to create a variable that stores the pin number ready for use in our code.

To do that, use the following code:

pir = 7

By doing this, we can easily change the pin in our code if we need to.

code2-420-90.jpg


2. Enable text messages


Creating a function called sms() on line 14 is a great way contain the code kindly provided by smspi.co.uk.

Using three arguments called to, message and hash we can easily pass the target phone number, the alert message and the unique hash which permits the use of the SMSpi text message gateway.

To use the text message service, you will need to sign up for a free login and hash to use in your project.

code3-420-90.jpg


3. Easier coding


In the code you can see:

GPIO.setup(pir, GPIO.IN)

This line of code sets our pin 7, which is referenced as a variable called pir, as an input, so that it waits to receive a signal from the sensor.

The next line of code shortens the full picamera function down to just camera making it easier to work with.

camera = picamera.PiCamera()

code4-420-90.jpg


4. How the main loop works


If the alarm is triggered.

Get the date and time and convert it to a string.

Alter the string to only contain the first 20 characters.

Create the message and file name for the alert.

Take the picture and record 10 seconds of video.

Use the sms function to send the alert to our phone.

Sleep for 30 seconds.

Loop the process if the alarm is triggered.

SMSpi-420-90.jpg


SMSpi


The team at SMSpi project has kindly provided its Python code for this project. SMSpi offers a free text messaging service for Pibased projects. The SMSpi project is powered by Raspberry Pi; all your texts are routed via the same device that is powering our alarm.

The API can be dropped into most common programming languages, including CURL, PERL, Python, PHP and Ruby.

To use the service, you need to sign up for a free login, which then allows access to the network via a hash that acts as your unique key.

Using the service, you can easily send SMS messages over the SMSpi network, as proven by this project, but you can also receive text messages and have them forwarded to an email address or posted to a web page for use in web apps.

Once you have your hash, paste it into your Python project code on line 11.

With your alarm now installed and working, you need to remotely access your Raspberry Pi to see the pictures and video.

The easiest way would be to remove the card and view the media on your computer. But you could enable SSH on your Raspberry Pi via the raspi-config advanced menu and then use your file manager to SSHFS into the folder where the pics are.

If you are feeling adventurous, you could install lighttpd or nginx to serve the content over a network. The choice is yours but remember to be careful with sharing the images over an open connection to the internet.









a2t.img
mf.gif
OWogrANm0SE


Continue reading...