DEV Community

Nick Shulhin
Nick Shulhin

Posted on

How to build your own primitive surveillance system with Raspberry Pi in less than one hour 🔥

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!


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 “"

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, 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!

Top comments (17)

siliconhippy profile image

Good simple project for the home user, and nicely explained.

My questions:

  1. I would like missing details about setting up the reverse SSH persistent access from a remote server/laptop ... A. WAN address= public IP address? B. How do you make RPi private IP "static"? C. Can the first time SSH password be skipped, or accessed from a pre-existing file on RPi? D. Any insights/code into having a few homes with one RPi each streaming to Cloud?

I think this multi home project will look great on GitHub with a detailed Readme as above !😃

vintozver profile image
Vitaly Greck

I would recommend buying a small server, installing openvpn there and establish persistent vpn tunnels from the pis to that server.

or use ipv6 and "registrar" service (kinda dynamic dns) which will keep the list of currency running hosts.

works well for me, easy to access every pi, no hassle, to manual ip config etc.

nickitax profile image
Nick Shulhin

Hi siliconhippy!

Thanks for your feedback, appreciate it :)

Yes, I probably had to be more specific in networking part: in my scenario I have a dedicated static IP address, which exposes my router.

Regarding a question of making Raspberry Pi IP static: almost in every router there is a functionality to assign static IP within LAN by MAC address, which means particular device will always have a reserved address for this kind of purpose.

Ahah, streaming to Cloud would be amazing, totally agree. However I specifically made this tutorial using only bare-bone solutions without much of third-party software or development. Sure, everything can be done with some amazing API + AWS on top with streaming and data storage/web interfaces.

I'll do my best to publish this project on GitHub, thanks for idea!

vintozver profile image
Vitaly Greck
nickitax profile image
Nick Shulhin

Thanks for sharing of your project, Vitaly! Looks really interesting.

I could see in comments people are requesting features such as video processing:

Recently had an experience with frame capturing and mounting videos from collection of images using a tool called FFMPEG =>

Hope it will be useful

siliconhippy profile image


It will be nice to have a Readme with all the steps explained. Especially for reverse SSH tunnel and any special notes.


vintozver profile image
Vitaly Greck

Readme added, enjoy.
Not sure whether you need reverse ssh tunnels here, service is pretty simple, I this setup on 4 of my cameras, was able to catch many things and notify the violators. Production ready!

I have Microsoft O365 Home subscription which comes with 1TB of OneDrive space, enough to save the 3 months of footage from all cams.

Thread Thread
siliconhippy profile image


Now it is clear, thanks for the quick response 😁🐵

  1. Sure reverse SSH is not needed here, but since I might need it on another project, could you describe the process and code?

  2. How about going from pics to videos with time limits?

Thread Thread
vintozver profile image
Vitaly Greck

Let's describe the project in a different topic. This is off-topic here.

Going from pics to video may require a bit more effort. Since videos are pretty large files, you may want to consider streaming options and processing the stream at the cloud. Depending on your connection limitations you may also want to have the quality adjusted (read, reduced) which is a clear disadvantage compared to the crispy pics. I would not consider steaming as a feasible option for the raspberry pi at this time.

lithiil profile image

Don't want to be a party pooper here but i think that motion is a far better solution

nickitax profile image
Nick Shulhin

Hi, Tudor!

No worries, thanks for a feedback :)

As I explained above, there is no limit towards development of this project => everything from streaming, cloud storage to machine learning and recognition algorithms.

This part is a very simple introduction based on mostly out-of-the-box tools shipped with OS.

If there is enough interest I may publish another hardcore version of it ;)

csells profile image
Chris Sells

It would be cool to add motion detection-based recording to this project and perhaps email notification?

vintozver profile image
Vitaly Greck

see above in my comment, separate project is available. email notifications are an extra menu item depending on the dish you're willing to cook. so far

davolesh profile image
Dave Olesh • Edited


Consider MotionEyeOS. Once installed, any usb camera connected will automatically be detected and connected.

It has streaming and even motion detection.

nickitax profile image
Nick Shulhin

Hey Dave!

Thanks for reply!

I've never heard of this project before, really interesting.

I could see it is basically another separate Linux Distribution.

If you are planning to use your Raspberry Pi as a camera only - definitely a go 👍

danielpuglisi profile image
Daniel Puglisi

Is that a Gremlin on your bed? 😂

nickitax profile image
Nick Shulhin

Considering the fact my room hasn’t been cleaned for a while, there are many possibilities ranging from mutated socks to zombie pillows... 😁