DIY video monitoring system, Node.js, Raspberry Pi

DIY video monitoring system – Part II Taking and hosting pictures with Node.js

With the hardware and operating system properly set up and know the camera works properly, it’s time to start some Node.js coding!

This is part II of the “DIY video monitoring system” series. If you haven’t read the previous entry, I highly recommend doing so.

Preparations

Before we can move to the actual coding, we’ll have to install Node.js on our Raspberry Pi. Raspberry Pis come with a number of CPUs and architectures[source]:

  • Raspberry Pi 1 Model A and B, Raspberry Pi Zero and Zero W – ARMv6
  • Raspberry Pi 2 Model B version < 1.2 – ARMv7
  • Raspberry Pi 2 Model B version 1.2 and Raspberry Pi 3 – ARMv8

(I won’t mention compute modules as they aren’t very useful for such an application.)

Depending on the version you’re using, there are different ways of setting up Node.js I’ll describe below.

If you’re still unsure what architecture is your Raspberry Pi using, you can always check that with the uname command:

Node.js installation on ARMv6 devices

Skip this fragment if you’re using Raspberry Pi 2 or 3.

To simplify the installation process on ARMv6 devices, I’d suggest using one of the shell scripts which you can find in my repository (it’s a fork of Steven de Salas repo, but with slightly modified scripts and a few newer Node.js versions added). For this project we’ll use the most recent version – 7.9.0:

Once the script is done downloading and installing Node, you should see the following message:

This means you’re ready to go!

Node.js installation on ARMv7/8 devices

Skip this fragment if you’re using Raspberry Pi 1 or Zero

On newer Raspberry Pis we can use Debian’s package management tool – apt – to install Node.js. The only additional step we need to make before that is adding Node Source to our package repository:

Once done, run:

And, just to be sure everything went properly, let’s check our Node and NPM versions:

Now we’re almost ready to start writing some Node.js code, just one more step, which is…

Video4linux2

I must admit this isn’t the first time someone’s using Raspberry Pi camera with Node. Actually, the Internet is full of such examples. One common thing for most of them is the fact they’re using the raspistill command and file system to do that. I, personally, find such solution not elegant enough for my tastes and in my implementation I’ll be using a different method, but before we move to it, let’s talk about Video4linux.

Video4linux, or v4l for short, is a set of drivers and APIs allowing video capture on Linux systems. It supports many USB cameras, TV tuners and similar devices. What interests us, is the fact this list includes Raspberry Pi cameras! What’s even cooler, v4l2 (version 2) is already included in your Raspbian Jessie Lite! You can check that by running v4l2-ctl:

Once you run it, you’ll probably see the following error:

This happens because the appropriate driver module is not loaded yet. Let’s fix that with the following command:

and try v4l2-ctl again:

Perfect! Now we see the driver is loaded.

The only problem we may encounter is that v4l2-ctl will stop working after reboot. That’s because the driver module needs to be loaded every time we restart our Pi. Let’s address that by adding the modprobe bcm2835-v4l2 command to one of the startup scripts – /etc/rc.local:

And voilà, from now on the driver module will be added during the system boot up. Just make sure you add it right before the exit 0 command.

At this point, you may be wondering why were we doing all that v4l2 stuff above. The reason is, instead of following the popular raspistill + file system watching approach, we’ll be using the node-v4l2camera module. It’s a Node module using some native code to give us access to the v4l2 driver directly from JavaScript.

Now that we have all that stuff prepared, let’s move to Node.js, shall we?

First script

Let’s start with creating a directory, where we’ll be putting our scripts. Assuming we’re in the home directory (/home/pi), run:

Now, we need to install the node-v4l2camera module. We do that using the npm command:

The installation may take some time as the native module needs to be compiled. Once it’s finished, let’s create the first script:

This script will connect to the camera, take a picture and save it to a file. Let’s run it:

and see if the picture was created:

Sweet! We can download it now using SFTP as we did before. If you don’t know how to do this, check the “Taking the first picture” section in my previous post.

Mug picture
Here’s the downloaded picture, how appropriate given today’s date!

Sending pictures over HTTP

All we did so far was pretty cool, but we still have no access to the camera from outside our Raspberry Pi. Let’s change that by slightly modifying our script.

We’ll use Node’s HTTP module to start an HTTP server that will host a very basic HTML page with a picture taken from our camera. Each time you reload the page, the script will take a new picture and return it to the browser.

Once you start the script, go to you web browser and open http://<raspberrypi_ip_address>:8080/.

You should the picture from your camera. Each time you reload the page, the picture will update.

Summary

In this part we’ve learned:

  • how to install Node.js on various Raspberry Pi devices
  • how to enable the v4l2 driver
  • how to take pictures using the v4l2camera Node.js module
  • how to serve those pictures using Node’s HTTP server

In the next part, we’ll refactor the above code making it more reusable and configurable. See you soon!

by Greg

No Comments »

Leave a Reply

Your email address will not be published. Required fields are marked *