This is a real simple project to get started on. Partly inspired from Alasdair Allan‘s work in the book Distributed Network Data. While the book concentrates on Adrunio I’m also interested in sensors from the Raspberry Pi side of things because the likes of storage and network is already there.
So, what I’m aiming to do is to wire up a basic passive infrared (PIR) motion detector to the Pi and with Python have it tell us when movement is detected. That’s easy enough. Broadcasting to the world, easy enough but I’m not really sure that all of Twitter really needs to know when there’s movement detected in your house, it’s the first step to being unfollowed.
To make this useful though I’ll make it send an SMS message direct to the phone instead. Then I’ll know when I’m being broken in to.
The hardware
It’s a very basic setup, the PIR is wired to the breadboard and the Pi connects also:
Pin 2 – 5v is connected to the red rail of the breadboard.
Pin 6 – Gnd is connected to the black rail of the breadboard.
Pin 26 – GPIO 7 is connected to my row 30/a of the breadboard.
The PIR motion sensor is connected very similar with the 5v going to the red rail, Gnd going to the black rail and the output pin (to trigger motion) is connected to 30/f on the breadboard (the same line as the GPIO7 pin from the Pi.
As hardware goes, we’re done. The rest is software.
The code
The basic code has been based on the work that Matt Hawkins has done, no point reinventing the wheel on this one. Using the RPi.GPIO library in Python he gets the basic PIR sensor talking to the Pi.
You can see Matt’s original blog post here.
I’ve made a couple of small amendments:
Firstly I’ve added a method that will send a text message to the TextLocal service. What we want to do is send an SMS message to a predefined number when the motion sensor is triggered.
So to Matt’s code we can add:
import urllib def sendSMS(uname, pword, numbers, sender, message): params = {'uname':uname, 'pword':pword, 'selectednums':numbers, 'message':message, 'from':sender} f = urllib.urlopen('https://www.textlocal.co.uk/sendsmspost.php?' + urllib.urlencode(params)) return (f.read(), f.code)
And within the if statement block where motion is detected, alter it so it reads:
if Current_State==1 and Previous_State==0: print " Motion detected!" sendSMS("jasebell@xxxxxxxx", "xx_password_xx", "447900xxxxxxx", "447900xxxxxx", "There's been movement detected in the house.")
Basically the sendSMS method expects the arguments of username, password, recipient number, sender’s number and the message you want to send. This is then sent as a URL POST request to the textlocal.com server. You’ll need an account and credits setup first before you can try this out. Remember that SMS costs money.
Testing it out….
You’ll need to run this as the root user so Python can access the GPIO pins.
pi@raspberrypi ~ $ sudo python pir2sms.py Waiting for PIR to settle ... Ready Motion detected!
When the script reports that the sensor is ready then try gently waving your hand in front of the sensor. A text message will be sent to you:
Leave a comment