Here’s how I get an alert on my iPhone or iPad when my front door is opened, all from the Raspberry Pi. This is useful when I’m away as a sort of remote burglar alarm (I can then phone my neighbours to check on the house), but you could use the alerting part of this project to send whatever you like.
Here is how to do it:
1) Build a basic switch input circuit and install GPIO libraries to read it from Python
I used the excellent instructions in The MagPi magazine, Issue 2, but removed the LED and 470 Ohm resistor. I won’t recreate the instructions here - follow the guide in the magazine to setup the software and create the circuit and you should be fine.
http://www.themagpi.com/
2) Rather than a normal switch, connect a reed switch to this circuit
Reed switches contain a very thin piece of metal. When a magnet is in close proximity the switch is either open or closed. When it is moved, it flips to the opposite state (the magnet literally pulls the piece of metal to make or break contact). Mount the switch on your door frame and the magnet on the actual door right next to it. The switch will now change state when the door is opened.
3) Setup a means to receive alerts on your iPhone/iPad
Download the Prowl application:
http://itunes.apple.com/gb/app/prowl-gr ... 76271?mt=8
Now register your account, login, and set up an API key. This will be used as the ID for the application to receive messages. We will send a Prowl message from the Raspberry Pi through an API in Python and your Prowl account/application will take care of getting the actual alerts onto your iThing.
4) Setup a means to send Prowl messages from your Raspberry Pi
Install Python setup tools if you have not done so already:
Code: Select all
sudo apt-get install python-setuptools
https://github.com/babs/pyrowl/
Put the files into a suitable directory and change to it. Now install Pyrowl:
Code: Select all
sudo python setup.py install
Check out the test.py program that comes with Pyrowl and then write some code to send an alert. This is done by referring to the API key you set up earlier. Below is a basic test that I wrote. Possibly not the best coding, but hey it does a job of illustrating how this looks. Note you will need to supply your own API key and check the GPIO pin number being used matches where you plugged your circuit in.
You may also want to change the code to not to send an alert if one has been recently sent (e.g. someone opening and closing the door repeatedly), or otherwise limit the alerts to not annoy you too much (Prowl itself does have a timed limit but it’s quite high). However you decide to code it up, I suggest first writing a version that prints out the alerts, and only once tested do you revert this to actually using the Pyrowl API to send them.
Code: Select all
#!/bin/usr/bin/python
# Imports for Pyrowl
from pyrowl import Pyrowl
from pprint import pprint
import os
# Imports for GPIO
import time
import RPi.GPIO as GPIO
# Setup pyrowl with API key
p = None
p = Pyrowl("ENTER YOUR API KEY HERE!")
# Setup GPIO pin as input
# Check that this is the same pin you have your switch input plugged into!
GPIO.setup(11, GPIO.IN)
# Set initial state
previousstate = GPIO.input(11)
# Main loop
while True:
# Check that this is the same pin you have your switch input plugged into!
mybutton = GPIO.input(11)
if mybutton != previousstate:
if mybutton == True:
print "Door opened"
res = p.push("My Raspberry Pi", 'House Warning', 'Door opened', batch_mode=False)
else:
print "Door closed"
res = p.push("My Raspberry Pi", 'House Warning', 'Door closed', batch_mode=False)
previousstate = mybutton
# Print result if you like
#pprint(res)
time.sleep(.2)

I’d be interested to know if anyone else tries this/a variation and of course, if you have success. I didn’t have a lot of time to write this up so if you see anything that needs editing please let me know.
Enjoy!