October 7, 2013 · coding electronics gpio hacks piface python raspberrypi rsyslog security WiringPi

Raspberry Pi - Simple Home Security System

Or Whatever This Might Be Useful for.


Playing with my PiFace, I've decided to get it to a next level and create something that might be of a practical use. A £25-ish add-on board shouldn't be used just for switching LEDs on and off. That would be a waste of money. I've come up with an idea (not very unique, but one has to start with something) to make a simple security system. For start, it would remind me if (and when) my home doors/windows are shut/opened.

The simple Python script I've written to work with the PiFace, loops through the output connectors (there are 8 of them on the PiFace) and turns the LEDs on/off when a corresponding input is open or closed. When it detects a change, it writes information in the terminal and logs it to a separate file created by the script (to do so, you need to alter a bit your rsyslog.conf, but we'll cover that later).  The script can be found on my github.

And a short photo documentation of testing layout on my flickr stream.

How it works

reed switchThe Python script simply "scans" for input trigger. It responds to closing of a magnetic reed switch (in fact, any way of shorting wires would do, but I think this is the most sufficient way for this task) mounted on input wires.

The switch itself is shorted by a magnet. As you close the door, the magnet causes the metal reeds to close, activating a corresponding output pin on PiFace to light up an LED. If you open the door, the reed gap opens again and the LED turns off.

DISCLAIMER: The wire version seems not to be a good solution. As someone on + pointed out, the wires would be difficult to hide on the walls, especially if one has lots of doors/windows to "secure". In my case, it's just three windows and one door, but still, the wires have to run somewhere. One solution would be to use RF Transmitters, but still, they have to be powered somehow, and it'd be just a waste of space to have a battery pack lying next to each window/door. A good solution (better of two evils) would be to use a flat wire. It still doesn't look too nice, but once you hide them, it's okay.

Here's a simplified sketch of the PiFace connections:

Security PiFace

Initially, when you start the script, it shows you the current state of doors/windows/whatever you've decided to monitor. An example console output looks like this:

Main Door was OPENED at Thu, 03 Oct 2013 11:24:53
Roof Window was CLOSED at Thu, 03 Oct 2013 11:24:53
Kitchen Window was CLOSED at Thu, 03 Oct 2013 11:24:53
Fridge was OPENED at Thu, 03 Oct 2013 11:24:53

If the, let's say, Main Door is now closed, the script will tell you that:

Main Door is CLOSED at Thu, 02 Oct 2013 15:22:53
Cupboard is OPENED at Thu, 02 Oct 2013 16:34:02

The script logs also to a separate log file. The output there is something like this:

Oct 1 11:54:25 funpi security.py: Door was OPENED
Oct 1 12:02:11 funpi security.py: Roof Window was CLOSED
Oct 2 18:23:34 funpi security.py: Fridge was CLOSED
Oct 3 14:16:58 funpi security.py: Closet was OPENED

In order to make your Raspberry Pi log the output of the script into a separate file, you have to make some changes in the rsyslog configuration. Before you start to mingle with the configuration, make a backup of a file, in case something goes wrong.

Rsyslog Configuration

NOTE: You don't have to do this. Without it, however, the messages from the script will be logged into /var/log/syslog and /var/log/messages files. If you want your system to log the messages into a separate file of your choice, please follow the instructions.

Raspbian comes with rsyslog service preinstalled, so you don't have to worry about installation. If you don't know what the service does, please read bout it here: Guide for rsyslog, to understand it better. The service holds it's configuration in /etc/rsyslog.conf file, that we'll edit in a bit. First of all, we should make a backup of the file:

sudo cp /etc/rsyslog.conf /etc/rsyslog.conf.old

If anything wrong happens, we simply overwrite the bad configuration file:

sudo cp /etc/rsyslog.conf.old /etc/rsyslog.conf

and reload the rsyslog service.

Open the file in the editor of your choice (I'm using nano):

sudo nano /etc/rsyslog.conf

Within the file, you should see the lines:

# /etc/rsyslog.conf Configuration file for rsyslog.
# For more information see
# /usr/share/doc/rsyslog-doc/html/rsyslog_conf.html

Scroll down somewhere near line 60, to the RULES section. At the end of the section, add the line:

<span style="color: #ff0000;"><strong>local0.* -/var/log/security.log</strong></span><em># Set the path that suits you. Remember about the '-' in front of the path.</em>

This will tell the rsyslog to log the messages from local0 facility (as indicated in the RPiSecurity.py script), to a separate file. But, still, we'll get the output into /var/log/syslog and /var/log/messages files. We don't want this to happen, so we have to tell our system not to do so by adding local0.none to syslog and messages facilities. This will "log" no output from the local0 facility to those files.

In the same rsyslog.conf file find a line that looks similar to this one:

*.*;auth,authpriv.none      -/var/log/syslog

and add local0.none:

*.*;auth,authpriv.none<span style="color: #ff0000;"><strong>local0.none</strong></span>  -/var/log/syslog

Next, find a line starting with # Some "catch-all" log files (near the line 90) and the same local0.none to /var/log/messages part:

<span style="color: #ff0000;"><strong>local0.none</strong></span> -/var/log/messages

Those two entries will prevent the script from logging elsewhere than it's own log file.

At the next script start, your system will automatically create the /var/log/security.log and log there. It is also a useful thing to create a logrotate entry for the log file we will be using. Simply get this FILE and put it into /etc/logrotate.d/. With this done, we restart rsyslog:

sudo service rsyslog restart

And we're done! Our simple security system hosted on Raspberry works!

What comes next

I intend to add more usability to the system, create a CCTV of my home, add an automated doorbell, and some scheduled tasks like water heating or morning of the lighting would be a nice add-on. Also, I'm thinking of writing an android app in the near-ish future as a front-end of the security system.

PiFace Security

Till then, have a great time with your Raspberry Pi!

  • LinkedIn
  • Tumblr
  • Reddit
  • Google+
  • Pinterest
  • Pocket