A few months ago, I described how to use a Raspberry Pi to setup a domestic server. In this post we’ll see how to take snapshots using the Pi and a camera, so combining it with the server, we can create a simple monitoring system accessible from a phone.
First thing we have to do is to run another server, separate from nginx to handle the requests. nginx is a very basic server that can’t do much else, but it’s capable of forwarding requests/responses.
To install it, first we need to find out which architecture our Pi’s processor is. The Raspbian has the command
arch which tells returned
armv6l in my case. This tells us which version to pick from the Node.js site. We can install it manually by downloading and unpacking it:
cd ~/Downloads wget https://nodejs.org/dist/v6.9.2/node-v6.9.2-linux-armv6l.tar.xz tar xf node-v6.9.2-linux-armv6l.tar.xz
We can put it in a local directory (no need for sudo). We move it to a convenient place:
mv ~/Downloads/node-v6.9.2-linux-armv6l workspace/node
and then make sure that path is being looked for when running executables:
Now we need to tell nginx to forward requests to another IP address, which our Node.js server will be listening to, by adding this to `/etc/nginx/sites-available/default`:
Finally we create a very simple Node.js application that listens to localhost (127.0.0.1), port 3000, and servers an HTML file as response:
I got the Raspberry PI 5MP Camera Board Module to take the still photos. It’s an official product from Raspberry pi, so it comes with some nice integrations.
In particular, I was interested in the raspistill utility.
It has a few parameters and image transformation options, and we can specify an output file to which an image will be saved in JPEG format.
raspistill -w 600 -h 400 -rot 180 -o images/image.jpg
Putting things together
The basic idea of this system is that whenever we hit our webpage, it will take a snapshot using the camera and display the image.
Because it takes some time for a photo to be taken, we do it asynchronously. In the meantime it shows the latest snapshot it had on file.
To avoid overloading the Pi we debounce snapshot commands and only take a new snapshot after at least a minute since the previous one. I wrote a bash script to handle that:
Now we just need out Node.js app to call this script asynchronously:
This simple project was inspired by a friend, who used a Raspberry Pi + camera to monitor his apartment during his vacation. I copied the idea with the intention to learn about the process and put the Pi I bought a couple of years back to use.
I haven’t actively used this system, but it was fun to work on it and I’m now looking forward on working on other “home” projects.