Building a custom home alarm system.
It all started in the summer of 2011 where my basement was flooded during a heavy rain storm. No great damage was done that time, but what about next time? It initiated the idea of building an alarm system that could monitor events in the house and send notifications of manual incidents as well a timed updates. But living in an old house presents a lot challenges in terms of wiring and modifications if you want to install a surveillance system.
The scope for me was to create a system that could give some kind of feed-back from unusual events happening when the family was away. This could be water leaks, smoke and human activity etc. For a start a simple e-mail reminder could be useful so neighbours could be contacted in case something unusual is happening while travelling. Later on an SMS service was added.
Standard alarm systems are easy to operate and install but they often miss features that can combine motion activity with other useful inputs and outputs from a private house. The choice could therefore be to install some of the expensive home automation systems with a lot of standard interface boxes and a nice programming user interface.
Instead I decided to try out the Raspberry Pi (RPi). It is a cheap device which has a nice and rare combination of programming features and pins that can be connected to different sensors/buttons. The device operates in an open source environment and there are some standard programs that can be used for surveillance purposes. But if you want a true custom installation you have to build everything yourself.
Using the RPi presents a lot of technical challenges so some tech knowledge is necessary in order to succeed. But being systematic and dividing the project up in to smaller chunks helps a lot for understanding, designing, installing and operating a system based on the RPi. A custom and wired installation also benefits from a central control with one power source for all devices saving you from maintenance and battery changes around the installation.
The Raspberry forum has a lot of many nice examples that can be picked up for inspiration so benefitting from this shared environment also encouraged me to make this documentation in order to share my experiences with other newcomers.
Designing the connection of peripherals to the RPi is the best way to start. During this process a lot of considerations and decisions are made in terms of functionality and which components to use. And its easy to add new features when they pop up (and they will do).
Once made the design is a great help when wiring the house and purchasing electronics and components for building the interface board for the RPi. All devices that were previously installed in the house were also connected to the system (door bell, PIR etc.)
The design can be seen here
Colored cables are used in the installation and electronics for easy testing and error checking.
There are several options for interfacing the RPi to peripherals such as the Slice of Pi and Humble Pi boards on which you can solder your electronic components. These are an excellent solution if you want a compact installation as they can be fitted on top of the RPi in one of the standard RPi cases.
Another option is to buy a bread board which is good for prototyping and the use this in the installation. But it takes up some space.
For this project a prototype interface board was built on a print test board with copper strips. It is easy to cut the strips eg if you mount a resistor on top of them. This also gives the ability of making “bus-lines” eg for Ground or Power distribution.
Next consideration is connectors to the board. If you go for a compact installation there is no space for eg the Molex 2,54mm connectors on the interface board if you need to keep everything inside the standard casing. You can decide to build a break-out box and connect the two with a standard 26p flat cable. This enables you to use large connectors to your different peripherals and protect the RPi via resistors. The advantage is that the space gives a good freedom during testing (and reconstruction).
In my home installation I have an ethernet patch box equipped with RJ-45 connectors which lead to the different outlets in the house. Using some of the spare connectors in this box enables me to tie the RPi to the box via CAT5 cables soldered directly to the interface board which is mounted on top of the RPi. Cutting some slices in the hood of the RPi case makes it possible to close and secure the unit after testing has been made making it a truly compact device in my installation.
The final unit with cables, modified hood and buzzer mounted on top of the print board. Ready for power supply before it is up and running.
From the patch box CAT5 cables go to the different peripherals and terminals.
The basement has two 220V PIR motion-sensors installed – one at each end of the staircase. Activating one of the sensors turns on the light so it stays on for five minutes. Using PIR instead of a standard power switch was a choice made during a renovation project as it is easy to forget to switch off the light when you leave the rooms downstairs as using a standard 220V switch the light can potentially stay on for a long time before the next person enters. The alarm system should thus connect to the PIR’s in a simple way. Dealing with 220 volts is not easy and it is not recommendable to connect the RPi directly to the power installation. Instead a simple spare power supply was connected to an extra 220V outlet for producing a 9 V DC voltage each time the light is switched on. By using a resistor and an opto coupler at the RPi interface board a safe power bridge is made between the two installations.
An extra low voltage PIR was installed in the entrance hall. This unit also has a “Tamper” switch in case someone tries to break the unit.
As smoke sensor the old fire alarm was used. It has two terminals inside which are used for connecting a series of alarms together, so if one alarm smells smoke then all alarms will sound. The terminal outputs around 5-6V when active and this is used for triggering the RPi via another opto coupler. Mounting the smoke alarm next to the PIR made it easy to take the 9V power supply and loop it from the PIR to the fire alarm so batteries are avoided.
A water leak sensor was a high priority as the basement was flooded during a rain storm in 2011. After the renovation the probability of a new event is much reduced but it will be an extra security to add this feature. The idea was to construct a primitive sensor based on some wires and a piece of kitchen roll. Adding some resistors together with a transistor at the RPi board then picks up the small current that would walk between the wires when the paper gets wet. Simple and cheap – but efficient!
The opto coupler used for interfacing with the 220V PIR is the IL74 but there are a lot of other alternatives on the market if you can’t get this one.
Two transistors are used for driving the water leak input and the buzzer output respectively. Using transistors gives a good protection of the RPi and it also loops current away from the GPIO pins. The transistors used here are the BC 546C
Resistors are all standard carbon 5-10% resistors.
Piezo buzzers were selected for sound signalling as they are a very efficient when it comes to sound versus current consumption. Two buzzers are mounted in parallel – one at the board in the basement and one in the hall above the front door.
This board is too small if all GPIO ports needs to be used but for the initial use it worked fine. After six months it was decided to build an expansion board and connect the two boards with a 8-wire Molex connector. This open up for extended weather logging and triggers.
The system had to be simple to operate and a high WAF is a prerequisite for daily use. So external keypads was quickly abandoned in favour of a simple on/off switch mounted inside beside the front door. The switch should turn on alarm function for the movement sensors (PIR) in the entrance hall and in the basement. All other sensors should be armed all times.
When leaving the house, the switch is set to “on” position which starts a series of short beeps. After 30 seconds the beeps get longer and finally after 40 seconds a long beep is heard in order to show that the alarm is now armed eg if you wait outside the door. The system is now armed and a service message will be sent by mail.
Entering the house through the main door will almost immediately trigger the PIR sensor which invokes a loop of 30 seconds with a series of short beeps that should remind the incoming person to switch off the system. Doing so stops the loop immediately.
If an intruder enters the house through the basement the system will switch to alarm mode when the light turns on through the PIR sensors. If entering directly in to the ground floor the intruder will be caught by the PIR in the hall which starts the warning loop. Assuming that he/she will not find the Off switch as it looks like a normal light switch the system will switch to alarm mode after 30 seconds. If the house is entered from the first floor there is no PIR up there to catch the intruder. Obviously he will descend some time and then be caught by the PIR in the Hall.
In Alarm mode a series of “SOS” beeps will be sound at the buzzers installed and a “Alarm” warning message will be sent by mail with information about which sensor that was activated. Same thing will happen if one of the other sensors (smoke, water etc) are triggered while system is disarmed (and armed).
A function sends a mail every day in order to show that the system is alive. This feature is nice to have when leaving the house for longer vacations.
All mails can also be converted to SMS if you want a direct and quick information about an event.
Python was selected as the programming language as it is recommended for RPi usage. I had no previous experience with Python and was only familiar with Basic and Visual Basic. It seemed to be a good back-ground as there are a lot of similarities. However Python is not so helpful with debugging as VB and especially the sensivity to correct casing causes some extra debugging in comparison to what I am used to.
There are some general differences you have to observe when walking from VB to Python:
<li>Be aware of casing. Sometimes you have to start with a capital letter (“True”, “False” etc.) and sometimes not (“if”, “while”, “def” etc.).</li>
<li>def is the same function as a SUB routine</li>
<li>Using variables in the different modules (def’s) is fine as long as they are declared Global in the def.</li>
<li>Use threads if you want a def to run independently of the rest of your program</li>
In general two resistors are used for the IN functions as I doubted that the internal resistors could provide full protection if I did something wrong. The input circuitry pulls the level down to ground by default and up to 3,3 volts of an event happens. The two PIR outputs from the hall are both closed by default so the programming has to be opposite for those.
Assuming that PIN 3 and 5 could be set-up for this kind of inputs was wrong. They can only be triggered if the input level is taken down as the internal pull up resistor can’t be disabled (at least I couldn’t find a way). This is also the reason for using BCM mode instead of BOARD mode as I saw someone recommending this for solving the PIN 3+5 problems. It didn’t help but I let the programming stay in BCM mode (for now). I will recommend BOARD mode for other projects as BCM doesn’t make sense to me as it is a source of unnecessary distraction to change pin numbers to something that isn’t logical even though that BCM should be compatible to other types of hardware. Instead of changing wiring so PINs 3+5 could be ground connected I decided to leave them free as they can be used if you want to expand GPIO’s at a later time (see links at bottom of the document).
All programming should be made directly at the RPi for quick testing of the GPIO functionality. Your old TFT with analog input can get a revival if you buy a HDMI->VGA <a href=”http://www.newegg.com/Product/Product.aspx?Item=N82E16812400359″ target=”_blank”>adapter</a>. It works in contradiction to what is (was?) posted somewhere at the RPi site.
It is also possible to install Python at your PC and do the coding there but then there is no way of testing the GPIO’s unless the code is transferred to the RPi. This can be done via FTP if you know the unit’s IP-address but it is not advisable to follow this procedure before you know that the electronics is working and you have gained some knowledge about Python. The RPi makes it possible to overwrite the program while it is running in contradiction to Windows. So you can do some adjustments on your PC’s Python program and the transfer this to the RPi that just has to be rebooted for the changes to take effect. With the remote terminal program PuTTy you can also test your new version on top of the old version.
There’s no GUI or displays for this installation. The only feed-back I get is the audible beeps when the system boots and alarm is armed together with a blinking LED outside the front door. If a monitor is connected to the RPi there will however be some “print events” displayed but no interactivity.
When the program is ready it is logical to start the alarm program every time the unit boots. This is done by editing the /etc/rc.local file. You have to do this in command mode, that is starting the terminal where you type “sudo leafpad”. Then insert this line: (sleep 5; sudo python /home/pi/projects/alarm.py) and make sure the last line is “exit 0″.
The code is <a title=”Code” href=”https://jesperbonde.dk/rpi/alarm/code/”>here</a>
I’ve made a page <a title=”SMS” href=”https://jesperbonde.dk/rpi/alarm/sms/”>here </a>which describes how an SMS function was added to the project.
For testing the electronics and programming (and learning) I decided to build a “dummy” of the house interfaces so I could sit down next to my work station in order to use it for research and debugging. As the installation is made with CAT5 and RJ-45 connectors, three cables were made with some spare components that can be quickly connected when the RPi is removed from the basement installation.
It was decided to use CAT5 cables for wiring as they were “on stock” already. Two new cables were drawn from hall to the RJ45 patch bay in the basement and extra cable trays mounted in the hall. A standard 220V switch was mounted next to the front door for easy Alarm control. And finally the new PIR sensor was mounted in the hall.
In the basement three new RJ-45 connectors were mounted in the patch bay. One of them leading to a terminal where inputs from power supplies and water leak resistor could be connected. The other two connects to the cables to the hall.
So this is how it looks like now: The basic network installation with the RPi added top right and the terminal break-out bottom left. Other components are ADSL router, switchers and landline-to-wireless phone.
This project has taken app 60-70 hours from the first idea to final use. The installation is easy to operate and is therefore used when leaving the house which is the most important prerequisite for functionality.
There are a lot of cheap and easy standard solutions out on the market but they don’t give as much fun, versatility and knowledge as with this technology. It is great to see that the functionality is exactly what our family need for daily use and to know that new features can easily be added in the future. The experience gained is highly useful for other projects at home and professional use as the RPi is highly adaptable to all purposes.
A digital/optical rain sensor has now been included in the set-up. More details <a title=”Rain sensor” href=”https://jesperbonde.dk/rpi/alarm/rain-sensor/”>here</a>
Temperature sensors are now added to the system. A specification can be seen here (soon)
Plans for extra functionality and features are
<li>Extending the “Tamper” circuit to include windows and outside doors</li>
<li>Connecting more water sensors in parallel</li>
<li>External light sensor for conditional programming (daylight sensor)</li>
<li>Heating control – lower temperature in house when alarm is armed.</li>
<li>Perhaps some GUI stuff or touch display but at this time I don’t see that it is useful.</li>
<span style=”line-height: 1.714285714; font-size: 1rem;”>LINKS</span>
<a style=”line-height: 1.714285714; font-size: 1rem;” href=”http://www.raspberrypi.org/” target=”_blank”>The Raspberry Site</a>
<a href=”https://docs.python.org/2/” target=”_blank”>Python programming</a>
<a href=”http://elinux.org/RPi_Low-level_peripherals” target=”_blank”>Details about the GPIO</a>
The TI expander chip <a href=”http://www.ti.com.cn/cn/lit/ds/symlink/pcf8574a.pdf” target=”_blank”>PCF8574A</a> can be used if you need extra GPIO’s via pins 3+5
Please respect that all the above information, guidelines, pictures and diagrams are published as a courtesy without warranty and responsibility and should be used at own risk.