Raspberry Pi : Home Control Center Part 2

In this second part of my series, I’ll go into configuring your PI to have:

A “security mode” that will record about 30-60 seconds of video everytime movement is sensed wherever a raspberry pi is installed

Display the local temperature, humidity security monitor arming on a simple web UI

In this photo, reposted from a previous article, you can see both the PIR (infrared sensor), and camera that will be used in this post.. note how tiny they are! For scale reference, that blue square just under the red LED of the camera on the right is an SD card….

… and showing network information

First off, have a quick read through the following from adafruit to get acquainted with the PIR sensor:

We will store the video file temporarily in /tmp, and then transfer this via FTP to a storage server (a QNAP in my case). To make things really fast, and save on the SD card life of the raspiPI, we use tmpfs, which by default is already installed on raspbian. This will ensure the data is stored in memory, making it quite fast. To store the /tmp file in memory via tmpfs, find the following line and uncomment it:

RAMTMP=yes

Next we install the python framework called “flask“. I find this framework extremely easy to use, and it’s fully featured, so it’s a winning combo in my opinion:

pip install flask

Last, we install a python based program that basically starts / stops arbitrary scripts of your choosing, and can even restart them when they die (this is very similar to the “forever” program used by node.js developers). The program is called “supervisor” and is installed with a simple:

apt-get install supvervisor

Let’s quickly build the webinterface using flask…

Note how in lines 32-33 in the above program, I am retrieving values from a pickle file wherein I’ve stored the sensor readings from the previous article to avoid over-taxing the sensor circuit. The script is quite easy. One function “index” serves a simple homepage, the other, “toggle” uses supervisor to turn the PIR sensor program on or off. The index.html file itself is nothing special:

The “unarmed” link will toggle the security system to start sensing movement. Going back to the flask python script, if you notice, on lines 19 and 22, I call on supervisor to start or stop the program called “pir”. This “pir” reference comes from the supervisor configuration file (/etc/supervisor/supervisord.conf) where I added a section similar to the following:

[program:flask]
command=python /home/pi/flask/main.py

[program:pir]
command=python /home/pi/PIR.py
autostart=false

The first program defined (“flask”) points to the flask script I present previously, and is started automatically on bootup. The second program is the “pir” program I reference in the flask script. Here’s the full PIR.py script:

I’ve inserted comments into the script which hopefully makes it clear enough to the reader what’s happening. In essence the script triggers an email alert, a 10-second video clip, which is then sent via FTP to a server.

That’s it!! try it out :) In the next article, we’ll setup a real-time feed from the camera using mjpg-streamer, stay tuned….

3 responses to “Raspberry Pi : Home Control Center Part 2”

Thanks for your interest! The code is hosted as a gist on github, and I noticed that sometimes it takes a while to load to get embedded into the page…. But, you can access the code directly here if you want: