CCTV, multiple webcams and a Raspberry Pi

INTRODUCTION

Raspberry Pi, a credit card sized computer was originally developed to change the way children were using computers by Upton, Mullins, Lang and Mycroft1, however, the Raspberry Pi is now widely used by all age groups and abilities for numerous projects. The Raspberry Pi uses Linux distributions and functions as a normal PC or Mac which can be used for numerous electronics projects, gaming or word-processing. The latter relies on BASH and Python scripts to carry out tasks. Projects carried out with the Pi include creating time-lapse videos, parent detectors and jukeboxes. Here, the Raspberry Pi was used to create time-lapse videos and live streams of multiple webcams.

There are several models of the Pi: A, B and B+, where B+ is the most recent model. This article shall describe the use of the B+ model. As with any computer, the Raspberry Pi requires an operating system (OS). The latter can be downloaded from the following website, http://www.raspberrypi.org/downloads/ where the NOOBS (New Out Of the Box) software, offline and network install version is the recommended download for beginners. This is saved onto a micro SD card with at least 8 GB, as recommended by the Pi Foundation2.

Another option is to purchase a pre-installed NOOBS micro SD card from the swag store (http://swag.raspberrypi.org/collections/frontpage/products/noobs-8gb-sd-card).

Before downloading the software you may find that the SD card will need to be formatted. On a Mac this can be done by using an SD card reader to connect it to the Mac. From here, open the disk utility which can be found in the Utilities folder in your Applicatioins. Select the SD card and select the erase tab in the main window. Select FAT32 from the Volume Format dropdown menu and enter a name for the card in the Name field. Click Erase and confirm the action. The micro SD card is now formatted.

If using Windows connect the SD card as with the Mac but now open Computer via Start/Windows menu. Next, locate the micro SD card, right click on its icon and click on Format… Then click on the File system drop down menu, select FAT32 (Default), click Quick Format and click Start. Select OK to confirm and once completed the SD card is ready for the NOOBS download.

Once downloaded the micro SD card can be inserted into the Pi via the dedicated slot.

The other peripherals can be added: monitor, micro USB power supply, ethernet cable, keyboard, mouse, webcams and/or Pi Camera, as depicted in figure 1.

FIG. I: A photograph of the Raspberry Pi B+ in a 3D printed case with the John Adams Institute (JAI) for Accelerator Science and Raspberry Pi logo. Peripherals: [1] – One of the four USB ports being used for a powered USB hub where the mouse, keyboard and webcam were plugged in. [2] – Ethernet socket. [3] – Pi Camera. [4] – HDMI (High-Definition Multimedia Interface). [5] – Micro USB power supply.

The optimum arrangement is to connect two webcams to the Pi via powered USB hubs and then the third webcam directly to the Pi.

The Pi can be turned on by switching on the power supply. If booting for the first time, the Raspi-config window shall appear with a list of operating systems, select Raspbian, the recommended OS. Once installed the Raspberry Pi will reboot and the enter password command line shall appear: raspberrypi login:. Type in pi. You shall be asked for the password which is raspberry. The prompt pi@raspberry ~ $ shall appear; the desktop can be started by typing in startx. You shall now find yourself in the desktop environment.

Next, on the left hand side double click on the LXTerminal icon to open the terminal.

Time-lapse videos are easily made using the Pi Camera module. The following code is typed into the LXTerminal and will initiate images to be taken at specific time intervals over a certain period of time:

raspistill -o image_\\%04.jpg -tl 3000 -t 3600000

raspistill is the command prompt for using the Pi Camera for still images, image_\\%04.jpg is the image file name, the number following -tl is the time between each image taken in milliseconds and that following -t is the total duration of time over which the images shall be taken in milliseconds.

Before typing in this code a file where the images can be saved can be made by using mkdir followed by the filename and the file can be opened using cd.

Once the images are taken the images can be compiled into a video by transferring the images to a USB and importing them into Windows MovieMaker or an equivalent program. The videos can be compiled using the Pi, however, this takes a few hours rather than a few minutes using another computer. Time-lapse videos can also be made using the webcams by typing in the following code into the LXTerminal:

fswebcam -r 1280×1024 -d /dev/video0 -l 10 image_\\%04.jpg

where fswebcam is the command for using the webcam to take pictures, -r specifies the frame resolution (width x height), /dev/video0 specifies the webcam you want to use (the rest are numbered video1 and video2) and -l specifies the time between each photo taken which is then followed by the image filename.

In order to make live streams of multiple webcams the LXTerminal must be used. Beforehand, the Pi must be updated by typing in the following code:

sudo apt-get upgrade

# updates the list of available packages and their versions, but does not install or upgrade any packages sudo apt-get update # installs newer versions of installed packages

The motion package is required for creating the live streams; the following line of code allows the package to be installed onto the Pi – this step requires an internet connection:

sudo apt-get install motion

Once installed the motion configuration file must be edited. It can be opened using the following code:

sudo nano /etc/motion/motion.conf

This will open the motion configuration file. From here, scroll down until you see daemon off, then change this to daemon on – this activates motion. Next, use the keys Ctrl and W on your keyboard to open the search input and type in webcam_ localhost to search for webcam_localhost on. Change the latter to webcam_localhost off. The following code must be added after webcam_localhost off:

webcam_localhost off

thread /home/pi/webcam/cam1.conf

thread /home/pi/webcam/cam2.conf

thread /home/pi/webcam/cam3.conf

where cam1.conf is the configuration file for the first webcam (how the configuration file was made shall be explained later).

Scroll down and find control_localhost on and change it to control_localhost off.

Now search for /dev/video you should find the part of the motion configuration file as depicted in figure 2:

The motion configuration file.

The code must be edited in the following way:

(default /dev/video^*)

and by adding the following:

videodevice /dev/video0

videodevice /dev/video1

videodevice /dev/video2

Use the keys, Ctrl and O to save the file and then Ctrl and X to exit. You shall be returned to the main terminal window. To create the configuration files for each webcam type leafpad followed by the filename in the LXTerminal. Each configuration file must include the following lines of code:

videodevice /dev/video0

webcam_port 8081

The preceding code is for one webcam, for the other two change video0 to video1 and video2 and the the port number to 8082 and 8083, respectively.

Once the configuration files have been created the following file shall be edited in order to enable motion to run:

sudo nano /etc/default/motion

The preceding code must be typed into the LXTerminal.

To enable the motion daemon to run change start_motion_daemon=no to start_ motion_ daemon=yes. Then save and exit the file using the same keys as aforementioned.

Now you can initiate the live stream by using the following code:

sudo service motion start

Similarly to stop motion use:

sudo service motion stop

To view the live streams first use,

sudo ip addr show

to show the IP (Internet Protocol) address that is specific to the Pi. Using this number as highlighted in figure 3, type this into your web browser (preferably Safari as it works best) followed by the USB port number, e.g. 129.31.149.142:8081 (where the port number is the last four digits, for the Pi it can take up to three webcams so 8081, 8082 and 8083).

The LXTerminal showing the IP address. The highlighted numbers must be typed into your web browser followed by the USB port number.

Once opened in your web browser you should be able to see a live stream of what the webcams are viewing, each in separate windows. By setting up the webcams in this way you can use it for CCTV purposes or even in the field of research where multiple parts of an experiment can be viewed from a computer.

1. Pi Foundation. Retrieved September 21, 2014 from: http://www.raspberrypi.org/about/ at 10:34

2. Pi Foundation. Retrieved September 21, 2014 from: http://www.raspberrypi.org/help/noobs-setup/ at 10:39

2 thoughts on “CCTV, multiple webcams and a Raspberry Pi”

  1. You are so cool! I do not believe I have read through anything like that before. So nice to discover another person with genuine thoughts on this subject. Seriously.. thank you for starting this up. This website is something that is needed on the web, someone with a bit of originality!

  2. Is there any chance that you still have the modified OS for this and can make a downloadable image? I am very poor at Raspian but need a Pi or Pi Zero to display multiple USB cameras. WiFi would be super but can live with cabled. Just need to display as many cameras as possible into one monitor. If there is already a link then please direct.

Leave a Comment

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