Home Security With Raspberry Pi
by BoguszJ1 in Circuits > Raspberry Pi
34731 Views, 92 Favorites, 0 Comments
Home Security With Raspberry Pi
That is a simple solution which can make you feel more relaxed when you leave your apartment - receive emails with pictures of your property being visited by unwanted guests, arm and disarm your security system the most easy and reliable way (press a switch and approach a RFID tag). And it costs nearly nothing - I pay more monthly for Internet access. You need a clone of Raspberry Pi, a few electronic parts and ... Internet access.
Please note that Zoneminder is not used in this guide. If you want to use Zoneminder, have a look here:
Hardware You Need
1. Raspberry Pi or its clone, see also:
https://www.instructables.com/id/Home-Security-With-Orange-Pi/
The cheapest one which will suite you network access and the number of cameras you need. Don't forget to buy proper power supply with suitable connector
2. RFID reader with antenna
3. PIR sensor(s)
4. a momentary switch which connects a circuit only when you press on it (with spring?)
5. two LEDs - green and red. Or one RGB led.
6. two 1k resistors
7. USB camera(s)
8. a UTP cable to connect PIR sensors, the switch, leds and RFID reader (I have managed to connect all with one cable with 8 wires, or 4 pairs if you like)
9. a small box or two if you want to protect your electronic parts or don't want to brag about your soldering skills.
10. a relay to switch on a light source - for incidents during night
Install Postfix
After having installed Linux you will need to install a few software components to run my example snippet. First you need to install Postfix if you want to send emails:
1. apt-get install postfix (you will be asked to chose for example 'local only')
2. go to /etc/postfix and create file sasl_passwd and put one line into it :
[smtp.gmail.com]:587 john.smith:pass1234
Replace user name and password with your credentials; you have noticed that is a line for a Google Mail account. This account is used to send alarm notifications (sent-from).
3. postmap hash:/etc/postfix/sasl_passwd
4. rm /etc/postfix/sasl_passwd
5. replace the content of /etc/postfix/main.cf with following lines (you might want to adjust hostname):
smtpd_banner = $myhostname ESMTP $mail_name (Ubuntu)
biff = no
append_dot_mydomain = no
readme_directory = no
smtp_tls_session_cache_database = btree:${data_directory}/smtp_scache
smtp_tls_security_level = may
smtp_use_tls = yes
smtp_tls_CAfile = /etc/ssl/certs/ca-certificates.crt
myhostname = raspberrypi
myorigin = $myhostname
alias_maps = hash:/etc/aliases
alias_database = hash:/etc/aliases
mydestination = raspberrypi, localhost.localdomain, localhost
relayhost = [smtp.gmail.com]:587
mynetworks = 127.0.0.0/8 [::ffff:127.0.0.0]/104 [::1]/128
mailbox_size_limit = 0
recipient_delimiter = +
inet_interfaces = all
smtp_sasl_auth_enable = yes
smtp_sasl_password_maps = hash:/etc/postfix/sasl_passwd
smtp_sasl_security_options =
smtp_sasl_tls_security_options = noanonymous
6. /etc/init.d/postfix restart
7. you might test the configuration of Postfix by sendmail some.name@some.address test content .
Prepare Software
For my Raspberry Pi B+ and Raspbian Jessie I needed to go through the following additional steps:
1. apt-get install python-setuptools
2. easy_install pip
3. pip install pyserial
4. apt-get install mailutils
5. disable serial being used by console logging. I found a few different ways:
a) raspi-config → Interfacing Options → Serial → Login shell NOT accessible over serial
b) removing console=serial0,115200 from file /boot/cmdline.txt
c) systemctl stop serial-getty@ttyAMA0.service
systemctl disable serial-getty@ttyAMA0.service
6. apt-get install python-opencv
7. apt-get install imagemagick
Wire It and Run It
Connect your parts exactly as presented on the picture. If you don't then you will have to make changes in the source to reflect changed port numbers.
Warning! RPI IOs do not accept 5V, you should use eg. a TTL logic converter to decrease voltage coming from RFID or PIR sensors. My choice was a 74HC4050.
Ok, theoretically you could be able now to run rpi-alarm.py with:
nohup python rpi-alarm.py &
But before that you need to edit the code and change IDs to your RFID tags and email address too. You can get the code here:
https://github.com/boguszjelinski/rpi-alarm
The very first run will start with the learning mode with green and red LEDs blinking. Its aim is to create rfid.txt file with RFID codes - approach your tags to antena, a few times for each one, and press the switch as long as you get a longer green light. Then edit the file to see if its not corrupted - it should contain as many lines as you have tags, 10 characters each. You can create the file manually, of course, the learning mode will be skipped. Note that your long pressing on the switch may result in arming your system as after the learning mode is completed the system waits to be armed - see "signals in use".
Signals in Use
The updated version of code contains also a "learning mode" - the green and red leds blinking alternately. A longer green signal (after pressing the switch) confirms completion of the mode.
A Few Comments at the End
A few comments to the source code, or just hints for you to write your own:
- LEDs and PIR sensors are configured by standard GPIO.setup GPIO.OUT and GPIO.IN respectively
- for that wiring of switch you need GPIO.setup (?, GPIO.IN, pull_up_down=GPIO.PUD_DOWN)
- the RFID reader is connected to GPIO15 which is board's RX, this can be read with
ser = serial.Serial('/dev/ttyAMA0', 9600, timeout=0.1) and ser.read(12)
This works on Raspbian Jessie on RPI 1, but it might be changed to /dev/serial0 with other distributions.
- I used to use streamer to dump images from USB cameras:
streamer -c /dev/video0 -s 640x480 -o camdmp.jpeg
and streamer -c /dev/video1 -s 640x480 -o camdmp2.jpeg for the second camera
but it crashed on Orange Pi, so I now use CV. To install streamer type this:
apt-get install streamer
- write some alarming wake-up text into alarmmsg.txt file and send email with:
mail -s "Alarm" -t john.smith@gmail.com -A camdmp.jpeg -A camdmp2.jpeg < alarmmsg.txt
- change your email adress in the code (line 51)
Have fun!