Home Security With Raspberry Pi




Introduction: 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 (I will soon try to write about using a GSM module).

Please note that Zoneminder is not used in this guide. If you want to use Zoneminder, have a look here:


Step 1: Hardware You Need

1. Raspberry Pi or its clone. 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.

Step 2: 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 = [::ffff:]/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.addresstest content .

Step 3: 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 streamer

5. apt-get install mailutils

6. 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

Step 4: 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 TTL logic converters to decrease voltage coming from RFID or PIR sensors. It has worked for me without any damage for 2 months now maybe because of long wiring.

Ok, theoretically you could be able now to run myalarm.py with:

nohup python myalarm.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:


Step 5: Signals in Use

Step 6: 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_UP)

- 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.

- maybe there should be a resistor for the switch too

- I 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

- 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

Have fun!

Update 27.05.2018

Streamer sucks, it sometimes gets crazy, maybe only with my junk
cameras. After a week with CV2 - it works like a charm. In order to use the code I have just submitted to GitHub: apt-get install python-opencv

apt-get install imagemagick

Here is the code you could use instead of streamer (the parameter is either 0 or 1 for my two cameras; file names have changed):

import cv2

def take_picture (device):
dev = str(device)

camera = cv2.VideoCapture(device)

return_value, image = camera.read()

cv2.imwrite('foto'+ dev +'.png', image)


os.system ('convert foto'+dev+'.png -quality 70 foto'+dev+'.jpg > /dev/null')



    • Fix It! Contest

      Fix It! Contest
    • Tiny Home Contest

      Tiny Home Contest
    • Metalworking Contest

      Metalworking Contest

    5 Discussions

    nice one. I been an electro/mechanical technician for a few decades now. Have no experience with code really. Does anyone have any good tutorials they can link me.? I have just got an Orange Pi pc2 and a raspberry on the way too.


    1 reply

    Tutorials on Python ? Google that term and go thru examples. Try out a few tutorials and select the one you like. Don't wait with coding, don't try to read all before you code. Learn by doing !

    That's a neat setup :)