Wednesday, July 26, 2017

Raspberry surveillance system, your house goes live on the internet

After leaving my brand new Raspberry 3 on the shelf for months (I admit being an impulsive buyer) I decided to give it some use by setting up a surveillance system with it. Raspberry 3 has more than enough power for easy image processing, and the camera developed for its GPU has nice quality at a very accessible price.



So, in order to start with this project, I got this PiCamera v2. I got that one with InfraRed (IR) filter, as I don't plan to take nightly shots with added IR lighting (maybe next prototype). It is really easy to connect its flex cable to the Raspberry (though unfortunately I'll have to leave the original case open and remove the top lid, as the case does not have a slot for the flex cable).

Once the camera is connected (beware to shut the system down before trying any connect/disconnect), I upgraded the whole Raspbian system with:

sudo apt-get update
sudo apt-get upgrade
sudo rpi-update

Now it's time to enable PiCamera module, this can be done by launching:

sudo raspi-config
Related option to toggle is under "Interfacing options".

Now that the camera is ready to be used, it's time to start playing with it. It is easy to take advantage of the official tool to take snapshots, raspistill.

You can take a snapshot with the bare:

raspistill -o pic.jpg
Ok, now we have everything to start playing seriously!

Next step is to install the bare minimum NGINX web server. Purpose of our prototype is to take a picture every n minutes, and show it on a web page hosted locally on your Raspberry and served by NGINX.

Before doing that, remember to:
  1. Make yourself with a dynamic DNS service, I tried Dynu which is free and easy to setup. Everything is clearly explained, you'll have to remember to setup "~/dynudns/dynu.sh" script with cron, which will update the dynamic DNS with your home IP. 
  2. Set a static IP for your Raspberry in your local network (e.g. 192.168.x.x)
  3. Log into you router configuration panel and set port forwarding to your Raspberry (port 8080 should reach the static IP set at previous step)
Once done, you'll probably be able to reach your third level chosen domain (e.g. "yourhouse.freeddns.org") and see the welcome page. Let's now change the HTML basic code under /var/www/html with something like:

<!DOCTYPE html>
<html>
<head>
<title>Welcome to my house!</title>
<style>
    body {
        width: 35em;
        margin: 0 auto;
        font-family: Tahoma, Verdana, Arial, sans-serif;
    }
</style>
</head>
<body>
<img src="my.jpg" alt="myhouse" style="width:100%;">
</body>
</html>


So this snippet is basically serving one picture captured by your PiCamera and stored locally to index.html file, this would be at "/var/www/html/my.jpg".

Last step is setting up a cron job with the related instructions to capture a periodical snapshot, this would be something like:

*/10 * * * * sudo raspistill -q 15 -w 1024 -h 768 -o /var/www/html/my.jpg

This will capture every 10 minutes a medium quality picture, quick to be served over the internet. Tune at your own preferred quality and capture rate!