It's All a Bet

Jason Bell – Author, Advisor and Practitioner in Machine Learning and Artificial Intelligence

Over 475 machine learning citations and 50+ patent citations on machine learning.

Raspberry Pi PIR Motion detection and alerting to SMS. #raspberrypi #sms #sensors

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

photo copy

 

It’s a very basic setup, the PIR is wired to the breadboard and the Pi connects also:

photo copy 2

 

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:

photo

24 responses to “Raspberry Pi PIR Motion detection and alerting to SMS. #raspberrypi #sms #sensors”

  1. Why not just use an email service such as GMail, or even a personal smtp server, to send a text to your phone?

  2. you forgot to drop down the voltage on your output signal into the raspberry pi. If you decide not to do that you can damage your little computer. There is no protection curcuit on those gpio pins. I would switch your VCC to 3.3V then use a 550 ohm resistor on your output signal. This will make sure you don’t burn up your raspberry pi.

  3. I almost forgot to say. Thank you for making the post! 🙂

  4. This is awesome, just got mine setup and running. Quick question, the black heads you have on the tip of your wire before it connects to the circuit, what are those called? Any idea where I could buy those online? They would come in tremendously handy.

    Thanks for the writeup!

  5. You could basically use any communication whether it be email, SMS, a Tweet or so on. For me I like immediacy (and I don’t depend on email/tweet notifications on my phone). I will, however, look at a text when it comes in.

  6. Can you tell I came from the Arduino? 🙂 Thanks for the heads up on that, I really appreciate it.

  7. Daniel, if you have a Maplin store nearby go to the computer connector section, all the fans and cases and stuff. There’s a bunch of disk drive connector leads and so on. I use those as they have male to female connectors on and connect to the PIR motion sensor really well.

  8. “I would switch your VCC to 3.3V then use a 550 ohm resistor on your output signal. This will make sure you don’t burn up your raspberry pi.”

    Is there any chance that you could post a photo of this modification?

  9. I’ve been looking into this with regards to a few commments I’ve had. In previous examples of this I’ve seen no mention of having to use any resistor on the output signal pin. Looking at the Adafruit pages on the PIR motion sensor the output pin is rated at 3.3v and can be used directly into the GPIO pin.

    http://learn.adafruit.com/adafruits-raspberry-pi-lesson-12-sensing-movement/hardware

    “Although the PIR sensor requires a 5V supply, its output is a Pi-friendly 3.3V, so it can be connected directly to a GPIO input.”

    Regards
    Jase

  10. New to python . Trying to figure out how to use smtp on my raspberry pi to send a text via my gmail account. also would you please post the finished activitiy so i can see where your amendments go? Thank you in advance

  11. […] Outstripping all other read posts 5 to 1 is the motion detection post I did at the end of June.  For those who haven’t seen it before you can read it here. […]

  12. Do you use the GSM chip, or just connect to the internet to use Carrier API or something like that ?

  13. Hi Pawel,

    I’m in the UK and textlocal.com have an easy to use account and API so I can send/receive SMS. It would be interesting to see how to do it with a proper GSM module though.

  14. I was thinking about plugin really cheap Android phone , (maybe even damged-> say with broken microphone or something) which you could get from few bucks on ebay or similar. And then run ADB (Android Debug Bridge) to run your App which would send appropriate message at appropriate time.

  15. Pawel, I’ve been giving this one some thought and I don’t know how if you can send data via ADB to the device? I know you could send an SMS via code from the device though.

  16. You can Invoke the app (for example the one I will made to send SMS with “motion detected”) via “am” command inside ADB.

    Take a look :

    http://stackoverflow.com/questions/4567904/how-to-start-an-application-using-android-adb-tools

    http://developer.android.com/tools/help/adb.html#am

  17. Thanks for the link, I’d never considered that approach to doing things. I usually have my Nexus S and Kindle Fire charged from the mains.

    I’ll give this a go the next time.

    Thanks
    Jase

  18. Getting alot of false positives, one every 2-7 minutes in an empty room.

  19. All down to the PIR sensor you are using. Things will need tweaking, do you have two small screw like pots you can turn? One will change the sensitivity of the sensor and the other how long it will hold it’s “on” state.

  20. […] quite a few tutorials on this out there. The best explanation for the hardware process was from a blog post on DataIsSexy. Follow this exactly and you’re in […]

  21. […] if we use the GPIO to add an infrared proximity sensor (like we did for Arduino), we can build our cheap alarm camera […]

  22. i am getting this error “ssl.CertificateError: hostname ‘www.textlocal.co.uk’ doesn’t match either of ‘www.txtlocal.co.uk’, ‘txtlocal.co.uk’” Any suggestions please

  23. Hi Kabell, this post is nearly five years old now so I’m assuming that the service might have changed since then. Might be worth reading TextLocal’s developer documentation as you may need to modify things.

    http://api.txtlocal.com/docs/sendsms

Leave a comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.