Building Surveillance System Using USB Camera and Wireless-Connected Raspberry Pi

Read this post to learn how to build a surveillance system using a USB camera plugged into Raspberry Pi (RPi) which is connected a PC using its wireless interface.
By Ahmed Gad, KDnuggets Contributor.
c
comments
Image

Surveillance System Using Raspberry Pi Connected to a USB Camera and a Circuit that Lights Led When Change Occurs

This tutorial discusses how to build a surveillance system using a USB camera plugged into Raspberry Pi (RPi) which is connected a PC using its wireless interface. PyGame is used for accessing the camera and capturing images which are stored inside the SD card of RPi. A background model is created for the scene to discover the changes from the default state. A simple circuit, connected to the GPIO pins, lights a led as an indication of such states.

The tutorial has the following steps:

  1. Connecting RPi to a PC using the wireless interface.
  2. Connecting a USB camera to RPi.
  3. Capturing images using PyGame.
  4. Building the background model.
  5. Detecting changes to the background model.
  6. Building a simple circuit that lights a led when a change occurs.

The tutorial assumes that the reader has a PC connected to a wireless network and RPi connected to a router using the Ethernet interface. For doing such work, please read the tutorial titled ”Building an Image Classifier Running on Raspberry Pi” which is available at this link:

https://www.linkedin.com/pulse/building-image-classifier-running-raspberry-pi-ahmed-gad

You should be able to know the IP address of the devices using a software such as “Advanced IP Scanner” and familiar with establishing the SSH connection using the MobaXterm software. After that, then we can start discussing each of the above steps.

1. Connecting RPi to a PC using the Wireless Interface

 
In the previous tutorial “Building an Image Classifier Running on Raspberry Pi”, we built a network with three devices connected which are RPi, a router, and a PC. RPI was connected to the router using the Ethernet interface but the PC is connected to the router using the wireless interface. In this tutorial, we will modify such network by using the wireless interface of the RPi to connect to the router. This makes the network completely connected wirelessly and avoids the restrictions of the wires.

The wireless interface of RPi needs configuration before being able to use it. For such reason, we will still use the connection established using the Ethernet interface for configuring the wireless interface. Using DHCP in the router, the RPi Ethernet interface will be given an IPv4 address. Such an address can then be used for establishing a secure shell (SSH) connection. You can refer to the previous tutorial for more details about establishing the connection.

Inside the SSH session created using MobaXterm, we can start configuring the wireless interface using either terminal commands or using the GUI of the Raspbian OS. Both ways are simple.

Using terminal commands, we will start by scanning the available wireless networks to find their service set identifiers (SSIDs). This is done using the following command:

pi@raspberrypi:~ $ sudo iwlist wlan0 scan


The first few lines of the output of this command are given in the following figure. The SSID of the target wireless network is “TEData_864A”. Note that you do not have to use a router for connecting the PC to the RPi. Using a smartphone, we can create an access point for connecting them.

After knowing the SSID of the target network, we can use it for configuring the wireless interface. Such a configuration exists inside a file which can be accessed using the following command:

pi@raspberrypi:~ $ sudo nano /etc/wpa_supplicant/wpa_supplicant.conf


The file will be opened inside the terminal. Using the down arrow, go back to the end of the file to write the network configuration. This is by writing the SSID and the password of the network as follows:

network={

                ssid=”TEData_864A”

                psk=”YOUR_NETWOK_PASSWORD”

}


After that, press CTRL+X to exit the file followed by Y to save the changes. For my case, I configured 2 networks and thus the file content has two configured networks according to the next figure.

You might need to change the status of the wireless interface from up to down them up again to apply such configurations. This is done using these 2 commands:

pi@raspberry:~ $ sudo ifdown wlan0

pi@raspberry:~ $ sudo ifup wlan0


To check whether the wireless interface is configured correctly, enter the “ifconfig” command to retrieve the configuration of the interface.

pi@raspberry:~ $ ifconfig


According to the output of this command, the wireless interface is given an IPv4 address of 192.168.1.8. This is how to configure the wireless connection using terminal commands.


We can do the same using the GUI of the OS which can be accessed using the startlxdeterminal command. After the GUI opens, there will be an icon for the network connections located inside the right part of the OS bar. Clicking on it will open a menu of the available wireless networks according to the next figure. Clicking any of the items will open a window that allows you to enter the password. By entering the password for the network with SSID “TEData_864A” and entering the “ifconfig” command, the result will be identical to that shown in the previous figure.

At this point, we have both the PC and the RPi connected wirelessly to the router. The current network is illustrated in the following figure. We can now remove the Ethernet connection and just use the wireless one between RPi and the router.

2. Connecting a USB Camera to RPi

 
RPi has its own camera module that can be used. Rather than buying this module, which might be costly to many, we can just use a USB camera which is might be available everywhere at less cost. The configuration of such a camera is very simple. Just connect the USB camera to one of the USB ports of the RPi. After doing that, we can check whether the camera works well or not by capturing images using the “fswebcam” package. At first, we need to install that package using the following command:

pi@raspberry:~ $ sudo apt-get install fswebcam


After that, we can use it to capture an image. A very basic way to do that is as follows:

pi@raspberry:~ $ sudo fswebcam test_image.jpg


This accesses the camera, captures an image, and saves it to the SD card in the current directory with the name “test_image.jpg”. The output after executing the command is given in the following figure.


Here is the captured image: