Raspberry Pi Alarm

02Aug12

I set $son0 a summer holiday challenge of building an alarm system that would send an email (and it seems that I’m not alone). I was pleased to see that the latest edition (#4) of Magpi has an alarm project so we set about building it together.

The hardware came together pretty nicely, but the software proved to be more challenging, so I’ll use this post to describe the hurdles encountered, and how they were dealt with.

Python GPIO

The code provided is Python [1] (it would be nice if the article called that out) and uses the GPIO library. Sadly this (and its key dependency) aren’t part of Raspbian. The following commands got things going:

sudo apt-get install python-dev
wget http://is.gd/RPiGPIO
mv RPiGPIO RPi.GPIO-0.3.1a.tar.gz
tar -xvf RPi.GPIO-0.3.1a.tar.gz
cd RPi.GPIO-0.3.1a
sudo python setup.py install

I’ve used a shortened URL above (and then had to rename the resulting file) so that everything fits on the page properly. The original URL is http://pypi.python.org/packages/source/R/RPi.GPIO/RPi.GPIO-0.3.1a.tar.gz [2]

The code

To save a bunch of typing I simply cut and pasted the code from the PDF page of MagPi into a text editor. This created a few issues:

  • All of the indentation got stripped out, so I manually put it back in using Tab, and then replaced my tabs with 6 spaces.
  • The single quotes and double quotes had been turned into illegal characters (probably by an overenthusiastic typesetter) so I needed to search/replace to get them straightened out.

With the code looking like what was on the page it was time to run. Unfortunately I hit a few more issues:

  • The Python runtime complained about some of the indentation
  • The latest GPIO insists on an explicit setmode()

Here’s the code I ended up with, which seems to pretty much map the functional description in the article:

import RPi.GPIO as GPIO
import time
GPIO.setmode(GPIO.BOARD)
GPIO.setup (11, GPIO.IN)
GPIO.setup (12, GPIO.OUT)
GPIO.setup (13, GPIO.IN)
GPIO.setup (15, GPIO.OUT)

while True:
      if not GPIO.input(11):
            if GPIO.input(13):
                  print "The door is open - please close the door and try again."
                  GPIO.output(15, True)
                  time.sleep(.3)
                  GPIO.output(15, False)
                  flash = 3
                  while flash > 0:
                        GPIO.output(12, True)
                        time.sleep(.3)
                        GPIO.output(12, False)
                        time.sleep(.3)
                        flash -= 1
            else:
                  active = 'true'
                  activated = 'false'
                  time.sleep(.1)
                  if GPIO.input(11):
                        print "Alarm Armed"
                        while active == 'true':
                              GPIO.output(12,False)
                              if not GPIO.input(11):
                                    time.sleep(.1)
                                    if GPIO.input(11):
                                          print "Alarm Disarmed"
                                          time.sleep(.1)
                                          active = 'false'
                              if GPIO.input(13):
                                    print "**** Alarm !!! ****"
                                    activated = 'true'
                                    GPIO.output(15, True)
                                    time.sleep(10)
                                    GPIO.output(15, False)
                                    while activated == 'true':
                                          if not GPIO.input(11):
                                                time.sleep(.1)
                                                if GPIO.input(11):
                                                      print "Alarm Disarmed"
                                                      time.sleep(.1)
                                                      active = 'false'
                                                      activated = 'false'
                                          else:
                                                GPIO.output(12, True)
                                                time.sleep(.3)
                                                GPIO.output(12, False)
                                                time.sleep(.3)
      else:
            GPIO.output(12,True)

I saved it as ‘alarm.py’ then ran it (as root in order to access GPIO) using:

sudo python alarm.py

Conclusion

We now have a working alarm system on the RPi, but it doesn’t send emails when tripped – yet.

Notes

[1] Python 2.x to be precise, as the print commands aren’t function calls, which is one of the more obvious differences between Python 2.x and 3.x



3 Responses to “Raspberry Pi Alarm”

  1. domesday over at the Raspberry Pi Forums points out that there were preceding articles in the series that would have set the scene for Python GPIO (using v2). He also offers some pointers towards email libraries.


  1. 1 Review – BeagleBone Black | Chris Swan's Weblog
  2. 2 Brighton Mini Maker Faire Raspberry Pi FAQ | Chris Swan's Weblog

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s


%d bloggers like this: