I’m always on the lookout for those teachable moments. Inspiration struck me when my 5-year-old asked for help in keeping his little brother from sneaking into his room. I spotted the perfect time to teach him about inputs, outputs, and programming. Learning is easier when the lesson centers on solving a personal problem.
I could have built a simple alarm system any number of different ways, but I knew that I wanted the system to be an all-in-one unit for versatility beyond my son’s original request. I chose the Raspberry Pi as the controller because it is easy to connect to the internet, it can play MP3 files, it interfaces with USB peripherals like cameras, and it has general purpose inputs and outputs for connecting to simple electronic components like buttons, sensors, and lights.
My alarm system’s code, which you can view on GitHub, is rather simple. To take advantage of Linux’s multithreading, I split the tasks between two Python scripts. One python script (keypadd.py) monitors the keypad for valid codes. Upon boot, the system is set up as “disarmed”. Anytime the arm/disarm code is detected, it toggles the status of the system by flipping a byte in a text file called “armed.txt”.
The second Python script (alarmd.py) monitors the passive infrared sensor via the Raspberry Pi’s GPIO. If motion is detected, it checks the armed.txt file to see if the system is armed. If the system is armed when motion is detected, the alarm is sounded.
I built this alarm system to fit my own needs. Following are the step-by-step instructions for building one like mine. When you build yours, be sure to customize it to fit your own needs, maybe by adding a laser tripwire or a cellular radio.
Step 1: Setup Raspberry Pi
Setting up a Raspberry Pi from scratch is beyond the scope of this project, but fear not, help awaits. You can find web tutorials but I recommend you read the book “Getting Started With Raspberry Pi” by Matt Richardson and Shawn Wallace. This book will help you not just with the PiLarm project, it will also help you discover the capabilities of the Raspberry Pi, so you can combine its various powers into your own new and interesting creations.
- Thus guided, connect your Pi to the internet so you can download the libraries and code needed for the following steps, and so the Pi can tweet and email its intruder pictures.
Step 2: Install GPIO
This library will let you control the Pi’s GPIO pins to use the passive infrared sensor and the keypad as inputs and use the revolving light as an output. Use the following three terminal commands to install it:
sudo apt-get update sudo apt-get install python-dev sudo apt-get install python-rpi.gpio
Step 3: Install fswebcam
If you are using a third party webcam like the Playstation Eye, fswebcam will let you use it to take still photos. If you use the raspberry Pi camera, look at their instructions for installation instead. Use the command:
sudo apt-get install fswebcam
You can take a picture manually with the command:fswebcam -r 640×480 -d /dev/video0 testpicture.jpgThen use the “ls” command to see if your picture appeared in the working directory.
mpg123 is a command line audio player that alarmd.py uses to play audio files such as, “System armed,” and “Motion detected. Please enter passcode.”
sudo apt-get install mpg123
Step 5: Add PiLarm Code
Now add the code that I created to perform the PiLarm functions. It is located at:https://github.com/BabyWrassler/PiLarm
- To put the code on a headless Pi (no monitor, SSHing in over the network), you can use git. The Pi doesn’t come with git, but you can install it:
sudo apt-get install git-core
And then use the “clone” command to download it to your Pi:
git clone https://github.com/BabyWrassler/PiLarm.git
You’ll now have a directory called PiLarm containing the python files and audio files for the PiLarm.
- Set alarmd.py and keypadd.py to run on boot by adding the following lines to /etc/rc.local just before the line that reads “exit 0”.
python /home/pi/Alarm/keypadd.py & python /home/pi/Alarm/alarmd.py &
- Familiarize yourself with the code. Compare it to the flowcharts above and see how the statements in the code work.
- Line 96 in keypadd.py contains the passcode (1912) used to arm and disarm the system. Line 97 is the haltcode (5764), used to gracefully shutdown the Pi and prepare it to be unplugged.
- If you are using the Raspberry Pi Camera Module, refer back to their instructions to see what commands you will need to put into alarmd.py in place of the fswebcam commands.
Step 6: Setup Twitter
Set up an “App” to get a Twitter API key: https://dev.twitter.com/apps/new
- For the “Access Level,” choose “Read and Write”. This will give you the “Consumer Key,” “Consumer Secret,” “Access Token,” and “Access Secret” that you will plug in as values for the function that creates the TweetPony API object (line 10 in alarmd.py, which currently reads “api = tweetpony.API(consumer_key = “abcd”, consumer_secret = “efgh”, access_token = “ijkl”, access_token_secret = “mnop”)”).
Step 7: Solder Up Perma-Proto
The Perma-Proto from Adafruit is great because it comes with a connector for the Pi’s GPIO. The pins are labeled on the board by the connector, which makes connecting things easy.
- Connect the Pi’s 3.3V power pin to the Perma-Proto’s positive power rail, and the Pi’s ground pin to the Perma-Proto’s ground rail. Then its just a matter of neatly and carefully routing wires to get everything connected. I use male and female headers as connectors for things like the passive infrared sensor, but you can solder them directly if you like.
- Notice that the speaker amplifier is on the Perma-Proto to get power from its power rail, but it is not connected to the Pi through the GPIO header. The audio cable connects the amp separately, and the speaker connects to the amp’s screw terminals.
- The matrix keypad has a rather short attached cable, so be sure to plan your layout before soldering.
For more detail: PiLarm Portable Raspberry Pi Room Alarm