How to build your own primitive surveillance system with Raspberry Pi in less than one hour šŸ”„

Nick Shulhin - Apr 13 '19 - - Dev Community

Recently I got my hands on a Raspberry Pi 3 B+ which I purchased used from classifieds website for around A$30 dollars.

And straight away decided to make some mini project to get familiar with a system.
One of the first ideas was to make a use of my old Logitech C170 camera, which was collecting a dust in my wardrobe for many years.

Thatā€™s how surveillance system project came up!

In this tutorial I will explain how to build a very easy and primitive system to take pictures of your room and download them.

For this tutorial we will need only two components: Raspberry Pi and a web camera.

My Raspberry Pi was running Ubuntu Mate, but you can install any other disto according to your preference.

You can download Ubuntu Mate here

Once Pi is connected and running, letā€™s attach camera to one of the USB ports.

You can always check plugged devices by running lsusb command, which will output all connected devices:

Bus 001 Device 006: ID 046d:082b Logitech, Inc. Webcam C170
Bus 001 Device 005: ID 045e:00cb Microsoft Corp. Basic Optical Mouse v2.0
Bus 001 Device 004: ID 045e:07f8 Microsoft Corp. Wired Keyboard 600 (model 1576)
Bus 001 Device 003: ID 0424:ec00 Standard Microsystems Corp. SMSC9512/9514 Fast Ethernet Adapter
Bus 001 Device 002: ID 0424:9514 Standard Microsystems Corp. SMC9514 Hub
Bus 001 Device 001: ID 1d6b:0002 Linux Foundation 2.0 root hub

In Linux environment, attached camera hardware can be checked from /dev space on your system.

To ensure our camera is connected, run ls -lts /dev/ | grep video command:

0 crw-rw----  1 root video   238,   0 Apr 13 12:30 media0
0 crw-rw----+ 1 root video    81,   0 Apr 13 12:30 video0
0 crw-rw----  1 root video   240,   0 Jan 29  2018 vchiq
0 crw-rw----  1 root video   241,   0 Jan 29  2018 vcsm
0 crw-rw----  1 root video    29,   0 Jan 29  2018 fb0
0 crw-rw----  1 root video   245,   0 Jan 29  2018 vcio

Awesome, video0 is there!

Yay!

Now we need to find a way how to capture a single photo from our web camera!

In this tutorial for the sake of simplicity we are not going to write any custom Go solutions, but just to use already existing tool which is called fswebcam.

We can install it with apt-get (donā€™t forget to update packages beforehand):

sudo apt-get install fswebcam

Now, it is time to take a first picture!

fswebcam -r 640x480 --jpeg 100 -D 3 -S 13 /home/user/Desktop/picture.jpg -d /dev/video0

Command above will snap a VGA resolution picture in JPEG format with delay of 3 seconds for processing.

Let's see what we have got:

...I do need some cleaning in my room... but anyway...

We got it! Congratulations!!!

But how can we do it remotely?

Of course we can write an API to invoke this command + expose it with NGROK or similar tool, but letā€™s do something more simple.

At my home I had an access to router, as well as WAN address and NAT configuration.

WAN address represents a Wide Area Network IP address which can be reached from outside your home local network.

Network Address Translation (or NAT) allows us to translate incoming connections to particular local IP addresses (in our case a Raspberry Pi).

First step is to create a persistent lease of our local IP address for Raspberry Pi. We are doing this to ensure that local IP address will be always the same for our Pi. This binding is done to MAC address.

Second step is to make a NAT forwarding. For the sake of simplicity we can use port 22 binding to enable Secure Shell to our Raspberry.

Letā€™s check if it works:

ssh user@raspberry_pi

It does! But it asks for a password...

Now we are able to SSH to our Raspberry Pi from anywhere in the World. Good job!

To make sure we donā€™t have to constantly login for connection, it is a good practice to generate public/private keys.

On a host machine which makes a connection to Raspberry Pi, generate a public/private key pair:

ssh-keygen -t rsa -b 4096 -C ā€œyour@email.to"

And now you can copy a created key to your Raspberry Pi with:

ssh-copy-id user@raspberry_pi //of your raspberry pi

Voila! Now we can SSH to our Pi with a help of a key and no password! Sweet!

Finally, letā€™s try to write a small script which would SSH to our Raspberry Pi, invoke capture image command and download it to the host.

echo ā€œExecuting picture capture on Raspberry Pi..."
ssh -t user@raspberry_pi 'bash fswebcam -r 640x480 --jpeg 100 -D 3 -S 13 /home/user/Desktop/picture.jpg -d /dev/video0'
echo "Copying picture from Raspberry Pi to host desktop..."
scp user@raspberry_pi:/home/user/Desktop/picture.jpg ~/Desktop/

Create take_picture.sh, put above content inside and execute a script!

And now we have our remote picture :)

If you have any questions or suggestions - feel free to ask!

. . . . . . . . . .
Terabox Video Player