There are a couple of pages out there that tells you how to turn your Raspberry PI into streaming web cam and/or surveilance camera. Most of them focus on getting the software up and running, but so far I have not seen anyone with a full rundown on how to turn the PI into the streaming camera. There will be a few comments, but mostly it will be as straight forward as I can possibly make it.
In this tutorial I will be using a Raspberry PI 3 Model B, a 16gb SDHC card and a Logitech C270 webcam. The RPI will be connected to a screen/mouse/keyboard.
Unfortunatly nothing is ever easy, so I will be bringing up a couple of issues I have had to overcome during my deployment.
Raspbian is a fully fletched operating system with all the benefits and drawbacks that comes with it.
MotionEyeOS on the other hand have a much smaller footprint and is specifically made for surveilance. The reason I choose not to go with MotionEyeOS is that after installing it it seemed to require a hardwire connection to even boot and since I did not have one at the time I choose the Raspbian route instead.
In order to install either of them to a SD card you need a SD card reader/writer. Then you need software to write the image to the SD card. I choose Etcher for this, but there are other programs out there that can do the same thing.
Basic Raspbian setup
The out of the box version of Raspbian comes with UK keyboard and timezone. Since I live in Sweden I had to change these settings to match my own. These settings can be found in the menu Preferences/Raspberry PI Configuration and then selecting the Localisation tab.
I also opened up the SSH (command line) and VNC (GUI) interfaces so that I can access the Raspberry without having it hooked up to a screen/mouse/keyboard. This can also be found in the Raspberry PI Configuration under the tab Interfaces. These interfaces will require other software to interact with them, I use PuTTY for SSH and VNC Viewer for VNC.
We need to connect to the local network. If you have the RPI connected by Ethernet cable then the connection should be automatic. If you use WIFI however you need to connect to your router manually. This can be done by clicking the network icon in the top right corner of the desktop and selecting your network and then entring the credentials to access it.
First of all update Raspbian to the latest version this can be done by opening a terminal window and typing
sudo apt-get update
sudo apt-get upgrade
Let the update/upgrade run its course before continuing.
Static MAC address
In some of the tutorials I have seen online on how to set RPI up as a web cam there have been mentions of finding out what IP address the RPI has. This is all well and good, however it is also a fact that RPI Jessie out of the box will give the on board Ethernet and Wifi network cards random MAC addresses.
What this means in practice is that every time you restart your RPI your IP address will change. I also had a WIFI extender which complicated things immensly so I bashed my head over this part for quite some time before I got it working.
First of all we need to give the RPI a static MAC address. A MAC address is a series of HEX numbers, six to be precise in the following pattern 00:00:00:00:00:00.
To accomplish this I created a shell script that is run after the RPI reboots.
sudo nano /etc/init.d/mac.sh
This will open the nano editor window with a new empty file.
Add this to the file but change the MAC address to something you like. In my case I took a piece of text and converted it to HEX and put that as the MAC address. For instance using the word ‘RaspPI’ as a MAC address would make it 52:61:73:70:50:49
Add this to the mac.sh file
ifconfig eth0 down hw ether 52:61:73:70:50:49
ifconfig eth0 up
ifconfig wlan0 down hw ether 52:61:73:70:50:50
ifconfig eth up
Save the file by pressing Ctrl+x and answer yes to saving the file.
Now we need to give the file permission to be executed
sudo chmod +x /etc/init.d/mac.sh
Finally we need to make this file run when the RPI is rebooted
sudo update-rc.d mac.sh defaults
Done! Now we need to reboot the RPI before we continue.
Motion is a nice piece of software that turns your RPI into a surveilance unit. Basic installation is very simple but there are a lot of settings that can be made. I will bring up the changes I made to make things work but they can be set pretty much however you choose. There are a couple of them that are more important to make things work however, I will highlight these
sudo apt-get install motion
This will install the motion software. Take note that during the installation it will mention that the user motion has been added, and that the user motion has been added to the group video and the group motion. It also tells you about a default folder /var/lib/motion. We will have use of these later on in the tutorial.
Backup the config file
During my trials I made a stupid mistake which made the config file corrupt, which in turn disabled the camera software entirely. I never managed to find the error and fix it so I ended up having to reinstalling Raspbian from scratch and starting all over again… not fun, but it gave me the opportunity to make this detailed tutorial so something good came out of it anyway.
cp /etc/motion/motion.conf /etc/motion/motion.bak
Edit the settings file
Now we need to make som setting changes
sudo nano /etc/motion/motion.conf
The settings file is pretty big, but it is well commented. There are a few default values that have been set to other values than the default value say it should be. These are the settings I changed to get things running.
This say that default is off, but it is set to on anyway. This need to be on for the daemon to run.
Capture device options
These settings can pretty much be set to taste. Default image size is 320×240 which is different from what is said to be the default value. In my case I made the settings like this
Image file output
output_pictures off (on by default which outputs a LOT of images to the output folder)
Global network options
stream_motion on (I like the 1 image/s unless motion is detected since it conserves bandwidth otherwise it will stream at the given rate or as close to it as possible)
stream_maxrate 10 (Frames per second to be streamed)
stream_localhost off (IMPORTANT if you wish to use the camera outside localhost)
Save the file by pressing Ctrl+x and select yes when asked to save and overwrite the old config file.
Make motion start after boot
We wish to start the motion service after the RPI boots, to do this we need to make a small change in another file.
sudo nano /etc/default/motion
start_motion_daemon=yes (Default value is no)
Save the file by pressing Ctrl+x and answer yes to save and overwrite the old file.
It is possible to start the server now and get the camera to stream images… however, as soon as a motion is detected the camera daemon will crash and shut down. The reason for this is that the folder /var/lib/motion is created and owned by root, but the user running the motion daemon is the user ‘motion’. The user ‘motion’ does not have permission to write in the default folder which causes the daemon to crash.
What we need to do is to allow the motion user to modify the default folder.
sudo chgrp motion /var/lib/motion
sudo g+rwx /var/lib/motion
Reboot the RPI
Now the server should be up and running. Try by opening the Chromium web browser and navigate to 127.0.0.1:8081
If everything is OK you should get a stream of what your web cam is currently pointing at.
One very interresting issue I had during the course of getting my RPI up and running was that the SD card went completely bonkers. It suddenly showed up as being 40Mb instead of 16Gb. Reformating it in windows did not bring the lost space back. What I had to do to fix it was to put the SD card into my Android phone and format it there. The phone also said that it was 40Mb but after formating the original 16Gb was “visible” again.