Thursday, November 21, 2013

Raspberry Pi Home Security System - Phase 1


I wanted to make an inexpensive home security and automation system with the following goals:
  • Home security system controlled by the internet or a 4x3 keypad
  • Use 433MHz Door/window sensors to trigger an alarm
  • Use 433MHz wireless switches to control home lighting
  • Actions on alarm trigger - Siren and send out an email/tweet
  • Future enhancements -  Laser trip wires!
Update 1: I added 2 webcams to the system and any motion now triggers an alarm as well.
Update 2: Check out phase 2 of the project here, which adds internet controlled outlet switches.
Update 3: Check out phase 3 of the project here, which adds a wireless siren + a demo video of the system.
Update 4: I swapped the 433Mhz receiver for a much better one based on reader Kris's feedback. Updated original post below.
Update 5: The information from all the phases of this project have been consolidated and written up as an instructable here: http://www.instructables.com/id/DIY-Home-Security-Automation-using-a-Raspberry-Pi/

System Overview:



For Phase 1 - I plan on enabling the home security functionality. Phase 2 will be the home automation (wireless light control etc.) and additional security features like webcam motion detection and potentially laser trip wires.

Hardware:

I picked up an inexpensive set of 433MHz transmitter-receiver modules ($2.77) and 433Mhz  door/window sensors ($12.99) from Ebay (links below)



433MHz Tx-Rx pair (set of 2)








I am now using a 433MHz receiver ($5.99) suggested by Kris (Thanks!) which is much better than the one originally recommended.  Link for that item is here:

Updated module - 433MHz super heterodyne receiver

433MHz Door/Window Sensors (set of 4)











For the input interface I picked up a 4x3 membrane keypad available on the Adafruit website ($3.95)












I have 2 webcams that I use for this project:

  1. An inexpensive USB webcam connected to the Pi directly
  2. A Tenvis JPT3815W IP Webcam connected to my home internet.


Software:

433 MHz RF communication on Pi:
I found this article on using 433MHz Tx-Rx modules with the raspberry pi. Follow the instructions here to install the 433Utils package.
http://ninjablocks.com/blogs/how-to/7506204-adding-433-to-your-raspberry-pi

A couple of points to note here:
  1. The outputs of the receiver are 5V TTL and I wasn't so pleased connecting them directly to 3.3 V tolerant Pi inputs.So I used a simple voltage divider circuit (220 ohm/440 ohm) to drop the voltage to something around 3.3 V. 
  2. The receiver from Ebay does not come with an antenna. This threw me off initially because I wasn't receiving any code unless my transmitter was few inches away from the receiver. The recommended length for an antenna for this module is a 172mm long one (1/4 wavelength).
  3. I modified RFSniffer.cpp to write to a file, if the code matched my specific transmitters.
 
  if (value == 0) {
          printf("Unknown encoding");
        } 
  else {

          printf("Received %i\n", mySwitch.getReceivedValue() );
          if (mySwitch.getReceivedValue() == enter your transmitter code here ) {
                  system("sudo echo -n 1 > trigger.txt");
          }
       }

       Run a "make" in the RPi_utils directory to create an updated executable.

You will need to install WebIOPi for the web interface. 

You will also need motion to detect movement using any webcam. Check out my previous post here to see how to set this up. 

Python code for rest of the software can be found on github:
https://github.com/tkmaker/RaspberryPiHomeAlarm

A shout out to Chris Crumpacker for enabling the 4x3 keypad code on the Pi. Thanks!

You can modify the passcode to arm/disarm the system from the keypad via keypad.py

You need to modify intruder_mail.sh with your email settings. You can also customize your email subject/message in this script.

Code overview:

There are 2 main scripts of importance -
  1. control.py - Calls keypad.py and alarm.py. Arms/Disarms the system based on web/keypad interface.
  2. alarm.py - Monitors the system status and if armed, checks for triggers before sounding an alarm.





Setup:

I have the RF receiver hooked up to my Pi (rev B) with data input on GPIO 21.

The 4x3 membrane keypad is connected as follows:
  • Pin 7 on the keypad is ROW 0- GPIO 18
  • Pin 6 on the keypad is ROW 1- GPIO 23
  • Pin 5 on the keypad is ROW 2- GPIO 24
  • Pin 4 on the keypad is ROW 3- GPIO 25
  • Pin 3 on the keypad is COL 0 - GPIO 4
  • Pin 2 on the keypad is COL 1 - GPIO 17 22
  • Pin 1 on the keypad is COL 2 - GPIO 22 10
Update: I had to change the GPIO being used here since GPIO 17 will be used by the RF transmitter (see post for phase 3) to send a code to the RF siren.

Green LED is on GPIO 7

The 2 webcams are monitored as 2 different threads via the motion software. You can check out this post here to see how to set that up.

I have modified /etc/motion/motion.conf to write to trigger.txt on motion detection.

# Command to be executed when a motion frame is detected (default: none)
on_motion_detected sudo echo -n 1 > /home/pi/trush_workdir/scripts/homealarm/trigger.txt

I also have it set up to upload images and videos to my Google drive (see my previous post for details)

Start control.py as a webiopi process:
 
sudo python control.py -m webiopi -d 8085
You can use any port of choice above. I used 8085.
You should see an interface like this once you go to your Pi IP address:8085

If you pull the website up on a touchscreen phone connected to your home internet, you can press the arm/disarm buttons to activate/de-active the system. The rest of the buttons are not enabled yet.
You can also enter the passcode (default 123) on your keypad to arm/disarm your system.
To test out the system -
  1. Arm it via the web interface or keypad - The terminal should show the status here
  2. Trigger a fake intrusion via the door sensor - Look for the alarm triggered message. The LED will blink 20 times and an email will be sent out 
  3. Disarm and arm again
  4. Trigger a fake intrusion by moving in front of the webcam - Look for the alarm triggered message. The LED will blink 20 times and an email will be sent out 
For now, the alarm action doesn't include a siren - I plan to add this as part of phase 2 as well. 
Please feel free to leave your questions and comments Any suggestions to improve or enhance the system are welcome.

82 comments:

  1. So many thanks for this useful post. I found the Membrane Keypad.It seems like an order of magnitude less expensive than conventional EM keypad, and was wondering how to use it, when I found your post.

    ReplyDelete
    Replies
    1. Good sharing, Sharp Cloud Smarthome System is now with the latest addition of the Smart Starter Kit (YL-SU50M), the aim is to give security to the masses and democratize it in a way it hasn't happened before with its multi-function, multi-users, multi-location security. For detail visit:
      http://kidbuxblog.com/stay-safe-with-sharps-cloud-smarthome-system/

      Delete
  2. Glad you found it useful elec flex. I also like the membrane keypad for its flexibility. It can be installed in a system easily.

    ReplyDelete
    Replies
    1. It's an awesome conceptualize well-explained post on the Membrane Keypad. Thanks again for your kind response and I expect the more similar informative article from you.

      Delete
  3. That's awesome. Devices like these can provide safety to the user and people can barely compromise any of their privacy using new technological devices. I think with time, the safety of the consumer will never be breached by any means.

    Regards,
    Arnold Brame
    Health And Safety Consultant Norfolk

    ReplyDelete
    Replies
    1. Thanks Arnold. With technology surrounding us in this age, it is probably put to its best use improving the quality of our lives.

      Delete
  4. Nice work man, glad I could help out a bit. Would love to see pictures of it all wired up

    ReplyDelete
    Replies
    1. Thanks Chris. Yes, that was on my list of things to do. I just uploaded one picture. I am working on cleaning a few things up and adding control for lighting soon.

      Delete
  5. Imprnt are engaged in offering a quality range of Biometric Attendance Systems . Due to their efficient performance, minimum maintenance and high identification speed, the systems are highly appreciated by our clients.

    ReplyDelete
  6. Hi! This is a great project / tutorial but I'm struggling with 433 MHz. How can you get the transmitter code (or any 433 MHz transmitter device code) to write it in the RFSniffer.cpp? I've got the same 433 MHz RXTX and door sensors. Thank You!

    ReplyDelete
    Replies
    1. Thanks Ani. The blog also sites this source for 433MHz code - http://ninjablocks.com/blogs/how-to/7506204-adding-433-to-your-raspberry-pi . If you go to this link and follow the instructions to get the 433Utils code from github, you can access the transmitter & receiver code. RFSniffer.cpp is part of that package. That link also talks about how to use the 433 MHz transmitter and receiver with a Raspberry Pi

      Delete
    2. Sorry! English isn't my native. I have been talking about:
      ...
      if (mySwitch.getReceivedValue() == >!>!>!>!>!>!>!>!>!>enter your transmitter code here<!<!<!<!<!<!<!<!<!< ) {
      ...
      but I figured it out the line above that print out what i need if i run rfsniffer and disconnect the 433 mhz door sensor to send data (i guess so).

      Delete
    3. That is right - You need to enter the code that you get from your door sensor when you run RFSniffer.cpp standalone (without the rest of the project code). This is similar to the example stated in the Ninjablock link. Good luck!

      Delete
  7. your article is very interesting for the alarm system construction is very thought out and worthwhile. www.synagermos.eu

    ReplyDelete
  8. I've always wondered how those keypads work. I really love how you were able to demonstrate what goes on behind the scenes. Every time that I use mine I am going to think of this article now. It really inspired me to research more of this stuff. http://www.alarmresearch.com/commercial-solutions/

    ReplyDelete
  9. This comment has been removed by a blog administrator.

    ReplyDelete
  10. This comment has been removed by a blog administrator.

    ReplyDelete
  11. Your security system is very complicated and small size i really like it . Thanks for your information and also its very useful for me.

    voip line Toronto

    ReplyDelete
  12. The given information is really nice...thanks for sharing....

    ReplyDelete
  13. Thanks for this information! I started a little project that uses a part of this information. But I have one problem: I use the same receiver as you do (well, it looks exactly the same), but even with an antenna (17 cm copper wire), I cannot get it to receive signals from the door sensors in another room. Reception is just really bad. Do you have a solution for this?
    Thank you,
    Kris

    ReplyDelete
    Replies
    1. Yes the range for the receiver from eBay is pretty poor. I have it working across 1 room with this setup. I am experimenting with other transcievers like cc1101 to get better range. The transmitters work fine across my home with 2 levels

      Delete
    2. Thanks for your reply. The transmitters indeed work fine for me as well, nice range. Will you keep us up to date with your findings with other receivers? Thank you!

      Delete
    3. This comment has been removed by the author.

      Delete
    4. Good news: I picked up the receiver below from eBay and now the range is good: I can receive signals from the end of my garden and from other floors in the house. It's a few euro's more, but at least now it works as it should.
      http://www.benl.ebay.be/itm/280936648330?ssPageName=STRK:MEWNX:IT&_trksid=p3984.m1497.l2649

      Delete
    5. That is great! I am going to try that out as well and will update this blog with the results. Thanks for the update! What Antenna did you use with this?

      Delete
    6. I had the original 433Mhz receiver and RFSniffer was picking up my codes at VERY small distances. I replaced the original receiver with the one you suggested and do not pick up any codes. Is there anything to do except swap receivers because I am confused why this didn't work with the new receiver when things were working with the old receiver? (do both 5v and ground pins need connected on the new receiver?)

      Thanks for sharing this info.

      Delete
  14. I noticed that the RFSniffer is using a lot of CPU. How are you guys dealing with that?

    ReplyDelete
    Replies
    1. I have noticed this as well. Has anyone find a good resolution to this?

      Delete
  15. Thanks for the information.
    I will bookmark it.
    Lloyd Security

    ReplyDelete
  16. Hi!
    Very good project, thanks!
    However I can't seem to get the door sensors to trigger the alarm. I have changed the RFSniffer.cpp with the code above and inserted my correct code from the door sensor. I do not get any "received code" from the door transmitter when the alarm is armed, if I open it. I do get the code from door transmitter if I run sudo ./RFSniffer.cpp
    The alarm is however trigged by motion in front of the webcam.

    Any suggestions?
    I second the above regarding very short receiving distance of the receiver. Any news regarding the cc1100 tranceiver?

    ReplyDelete
    Replies
    1. I am trying to understand your issue better - So for the same distance between the sensor and the Pi, you were able to get a code from the door sensor when you run it with sudo but not when the alarm is armed ? That would rule out receiving distance and would point to a software issue. You could add more debug/print statements in the python code to make sure it is going through all the desired code and running RFSniffer correctly.

      Regarding short receiving distance, I am going to try out the receiver suggested by Kris and will update the blog if it works out better.

      Delete
    2. This comment has been removed by the author.

      Delete
    3. Hi thanks!

      I got it working!
      I think the Alarm.py script was not able to activate RFSniffer because of uncomplete compilation of the 433Utils software, which may have happened when I connected the receiver before complete software install. At first I was using the exact same setup as you did.
      The receiving distance of the receiver was at 1m. I adjusted the frequency and got it working across one room.
      However I did order a different receiver - It look like the same receiver as Kris - the Superheterodyne 3400 receiver, from China. When connected at 3.3V it worked across two floors without problems. I did use a 17,3 mm coaxial copper wire as antenna. I also changed to other door transmitters without external antenna and it also connected with the new receiver.

      Delete
    4. How do you setup the RFSniffer.cpp file if you want to connect more than one door/window sensor?
      After some more testing I believe I am getting false alarms from neighbours sensors. My own sensors does trigger the alarm, but a door sensor not yet mounted or setup in RFSniffer.cpp was triggering my alarm. How is that possible?
      In RFSniffer.cpp I wrote like this:
      printf("Received %i\n", mySwitch.getReceivedValue() );
      if (mySwitch.getReceivedValue() == 99300 or 336001 or 887700 ) {

      Delete
    5. It should work the way you have it unless someone else around you or some device in your home is transmitting the same code. Try to isolate which exact code is causing the false triggers.

      Delete
    6. Thanks for your reply! The strange thing is it seems that a door sensor with the code: 5575735 is triggering the alarm -this is on of my door sensors not yet mounted. It is even not mentioned in the RFSniffer.cpp file yet, but it triggers the alarm anyway. I also had other code not mentioned in the RFSniffer.cpp file trigger the alarm which I simply don't understand

      Delete
    7. I think the problem might be related to writing these, or statements in RFSniffer.cpp. It seems when I tested if I write more than one code separated by, or, it open up the alarm to any 433 mhz code to trigger it. Is there any other way that works better?

      Delete
    8. Problem is solved as far as I can see. I changed RFSniffer.cpp with a series of, else if, statements, one for each door transmitter. This fixed the problem with random codes triggering the alarm.

      Delete
    9. It would have worked if you had used '||' instead of 'or' as well

      Delete
    10. I also tried it like this but got the same problem as when using 'or'
      if (mySwitch.getReceivedValue() == 99300 || 336001 || 887700 ) {
      I don't know why it doesn't work. Did you use this method?

      Delete
  17. You can even control your program using GTALK if you would like to.. That is much simpler way, if going thru web page is tedious.
    Follow this link to create a Gtalk Bot and run in parallel to your main thread. You can pass messages between them using Queues. It can be 2 way communication - You can send commands to pi OR pi can alert you when there is an alarm.. :)
    http://mitchtech.net/raspberry-pi-google-talk-robot/

    ReplyDelete
  18. Issues surrounding Wireless Security can never be over analysed. There are many factors which influenced the development of Wireless Security. Cited by many as the single most important influence on post modern micro eco compartmentalism, there are just not enough blues songs written about Wireless Security.

    ReplyDelete
  19. Wireless συστηματα συναγερμου are very good and with high technology and we provide very high security

    ReplyDelete
  20. The caliber of information that you're offering is merely wonderful. surveillance camera poles

    ReplyDelete
  21. I've been thinking about getting a home security system to make sure that when I go on trips, nothing happens to my home. I remember that growing up, we had break-ins in our neighborhood frequently. I think that the best course of action is probably to get a security system that will really help me to know if someone is trying to break into my house. http://www.videotecsecurity.com/

    ReplyDelete
  22. Security products are very important for safety of our house and family. Contact us for purchase & installation of various Home Security Devices.

    ReplyDelete
  23. I'm impressed that you actually put together your own security system. I don't know enough about wiring and all that to make my own system. How long did it take to put this together? http://www.alarmresearch.com/residential-solutions/

    ReplyDelete
  24. Good idea, I always like this type of posting. Your home security system is very helpful to protect home. Anyone can use your security system easily. I think many people will use this home security system.

    ReplyDelete
  25. It is fascinating that as technology advances, we have ways to explain it and understand it better. Trying to understand a home security system would have probably been easier a decade ago, but now we are much more familiar with the terms these days. Even though the systems are getting complex we understand the words they use to explain it better.
    http://www.marshallalarm.com/securitysystems.asp

    ReplyDelete
  26. Thanks for sharing such a nice knowledge with us. A must read blog. home security companies

    ReplyDelete
  27. Splendid information you have given here in the blog, you have actually impressed me yes!! best home security

    ReplyDelete
  28. Thinking about trying this project. Is there away to determine which door sensor is triggered? Do they send different codes?

    ReplyDelete
    Replies
    1. Yes each transmitter sends out a different code that will show up on the pi on RF Sniffer

      Delete
  29. The procedure of doors installation is very important so, i want to fine exact type of blog. Thanks for commenting. Security Doors London

    ReplyDelete
  30. Really nice information about security protection. Thanks a lot.
    emergency alert system georgia

    ReplyDelete
  31. Your blog is very interesting and informative.Security systems are becoming more and more available, and the technology behind them is evolving every single day.
    Wireless intruder alarm

    ReplyDelete
  32. I have made it my business to watch out for subtle security alarm industry changes over the years. Each and every year I can see these changes slowly take shape and take hold on the Alarm Companies that provide systems for consumers like you and me.
    The majority of these changes in the past went unnoticed by the average Alarm System protected household and business. They were slight adjustments made by the Alarm Dealers, Manufactures and Service Providers and unless you were looking, you missed most of them

    ReplyDelete
  33. Hi buddies, it is great written piece entirely defined, continue the good work constantly.pop over to this website

    ReplyDelete
  34. Thanks for always being the source that explains things instead of just putting an unjustified answer out there. I loved this post.
    wifi sport camera

    ReplyDelete
  35. It's really a nice and helpful piece of information. I'm glad that you shared this helpful info with us. Please keep us informed like this.
    home security cameras Installation los angeles

    ReplyDelete
  36. This is easier and surely gives comfort to internet users. Thanks for sharing. Post like this offers great benefit. Thank you!
    wifi inspection camera

    ReplyDelete
  37. More homeowners and business owners than ever are planning on installing security systems ranging in sophistication. Kathy Lott, Complete Home Alarms

    ReplyDelete
  38. I was very pleased to find this site.I wanted to thank you for this great read!! I definitely enjoying every little bit of it and I have you bookmarked to check out new stuff you post.
    gutter vacuum cleaning

    ReplyDelete
  39. He correctly acted to bypass the missing door contact so he could continue to arm the remaining alarm sensors. Security quote

    ReplyDelete
  40. You have got some great posts in your blog. Keep up with the good work.
    security cameras company

    ReplyDelete
  41. Hello, U write some extraordinarily attractive blogs. I always check back here frequently to see if you have updated
    wifi inspection camera

    ReplyDelete
  42. Great blog, keeping me from working. All the Best  
    wifi inspection camera

    ReplyDelete
  43. It contains useful and beneficial content. To me, you are doing the excellent. Your way with terms is elegant and relatively attractive. so keep it up.
    IMT ref cam

    ReplyDelete
  44. Cool blog you got here and thank you for the valuable information.
    Door locks

    ReplyDelete
  45. Thank you so much, this a great help. brief description for submission thanks.
    κατασκευη e-shop

    ReplyDelete
  46. Super blog and very nice information which i really appreciate it,
    roofers

    ReplyDelete
  47. I appreciate your blog and very nice presenting your all information about the quality fence in Australia it's really very good.
    fence company Australia

    ReplyDelete
  48. Your blog's all information are really very nice and interesting about the Best survival gear websites i like it.
    Emergency shelter

    ReplyDelete
  49. yes definitely this type of blogs are really help for people great job.
    roof leak repair

    ReplyDelete
  50. Wow!nice article and containing allot of information about the Sugar Land Roofing well done.
    Sugar Land Roofing

    ReplyDelete
  51. Such a nice blog and i appreciate your all efforts about the Home Improvement it's really good work. well done.
    Anti fog film for kids bathroom

    ReplyDelete
  52. Interesting blog and very amazing detail about the Urban Construction Pack well done keep it up.
    Urban Construction Pack

    ReplyDelete
  53. You have some honest ideas about the st louis Commercial Quote share here I really get many information and discovered most peoples will agree with your blog.
    st louis Commercial Quote

    ReplyDelete
  54. I am expecting more interesting topics from you. And this was nice content and definitely it will be useful for many people.

    Home Automation in Chennai
    smart home in Chennai
    Home security in Chennai
    Burglar alarm in Chennai
    Wifi Camera Chennai

    ReplyDelete
  55. These conventional unmonitored systems have various kinds of sensing units placed around your house: for instance, SecurityInstallers.com.au

    ReplyDelete