Perfect Pi Cam

,

For some of the projects I want to try this year I need a small computer platform with video input capabilities, linux, wifi, and raw processing power (the more, the better). The Raspberry Pi computers (every single one of them!) fit this bill nicely. This page will show you how to set up a solid little video input platform you can use for experimentation.

I have been a Raspberry Pi (RPi) fanboi since the beginning, having bought one of the RPi 1B models hot off the presses in early 2012. I still have that awesome little (now 6 years old) machine running at my home (with a RaspiCam video camera and a USB WiFi dongle attached) as a security web cam. Unlike the commercial web cams purchasable in 2012, this little work horse runs a Debian Stretch variant I updated yesterday and it is securely locked down. DIY rules.

Below are a few examples of web cams at my house that I built with various RPi models and various case types, all running the software configuration described in this guide:

If you don’t have a Raspberry Pi yet, and you are trying to decide which one to buy, I have some advice. I will be trying to run various neural network programs on RPi this year. If you want to try that too, then I recommend buying one of the more powerful Raspberry Pi models like the RPi 3B, or the new RPi 3B+. Those two models cost about the same, around US$40 (without accessories), versus around $10 for the tiny Raspberry Pi Zero W. All three of these models have onboard WiFi so no need to worry about network wiring. You just need to get power (5V to it). Be careful when buying, 3B+ is better than 3B (the former has a 64 bit processor), and RPi Zero W is better than RPi Zero (the latter does not have WiFi).

A prime setup (see the parts list below) including an RPi 3B+, RaspiCam camera, a plastic case, microSD card, and power supply should set you back around US$80.

If you just want to make a very tiny web cam, then you can follow these instructions using a Raspberry Pi Zero W, RaspiCam camera, official plastic case, microSD card, and power supply, and spend less than US$50. The picture below, and the one at the top of this article, show an RPi Zero W setup like this. Since the RPi Zero W only has 512MB of RAM, it may not be capable of some of the projects I have planned. The RPi 3 models on the other hand each have 1GB of RAM.

Parts list:

Raspberry Pi US$10-US$40 (I suggest RPi3B+, RPI3B, RPi2B, or even an RPi Zero W)

(available widely now, often in kits with some of the accessories below)

RaspiCam $US10-US$30 for the RPi model you use, with a Camera Serial Interface (CSI) cable

I have used this $10 unit with RPi2B and RPi3B and RPi Zero W

5V power supply $US3-US$10 with microUSB connector and a minimum 2 Amps rating

microSD card, around US$10, with an adapter so you can mount it on a computer to flash it

I recommend 32GB, but probably minimum needed is about 8GB

protective case US$10-US$25, not necessary, but recommended

For an RPi Zero W, you need a special CSI cable, which is included with this US$10 case
For an RPi3B or 3B+ my favorite case is the one in this complete kit because it has a nice camera mount built-in. Or you can buy something similar and drill your own hole for the camera.

Hardware Setup Notes:

Please do not connect the power until later in the process when you are instructed to do so.

Be careful when handling the Raspberry pi and other electronics components since most of them are easily damaged by static electricity. If possible, ground yourself when working on electronics.

Treat the Camera Serial Interface (CSI) thin ribbon cable carefully. Do not fold it! The very tiny wires inside the cable can easily be invisibly broken.

When attaching the RaspiCam camera to the Raspberry Pi make sure you connect the CSI ribbon cable such that the connections on the cable touch the connectors on the circuit board. It’s easy to put the wire in upside down and then nothing will work.

On the camera end, and also on the end of the RPi Zero W board, just make sure the side of the ribbon cable that has metal wire connectors faces down, toward the circuit board.

On other Raspberry Pi models the connector is veritical, and you need to make sure the metal wire connectors face away from the end of the board with Ethernet and USB connectors.

The RaspiCams for which I provided an Amazon link above, come loosely attached to their circuit boards (a good thing if you wanted to mount them somewhere else, I guess). One of them was so loosely attached that the camera did not work until I realized this and firmly pushed the connector down. In the process I realized there is double-sided tape on the back of the camera component, as shown in the close up photo below. So I removed the white paper, reconnected the push-in connector (firmly!) and then stuck the camera module onto the board with the tape. I recommend that others do this too, to help hold the tiny camera connector together.

Software Setup Procedure:

This setup procedure is a “headless” approach. Headless means that we are not going to attach a keyboard, mouse and screen to your computer. We are going to do everything remotely instead. I prefer this approach because it is much less trouble if you know how to do it. I treat my Raspberry Pi (and other small single board) computers more like appliances than personal computers. So it seems alien to me to attach these peripherals. If you are using an RPi Zero W it is actually a bit awkward to add them because the connectors are nonstandard and there are not enough of them so you need a hub. So let me show you how you can doe this headless. Of course, if you prefer, you can attach a “head” and work in a more traditional manner.

Before we can do anything with the RPi, we need to set up the operating system software on the MicroSD card. You must do this on a different computer. Any typical desktop or laptop computer connected to the Internet should be able to do this.

Prepare the MicroSD Card

This section will show you how to flash the latest Raspbian image onto your MicroSD card. You will need an adapter to physically attach the MicroSD to your computer. MicroSD cards often come with standard sized SD card sleeves you can slip over them so they can be put into a standard SD card slot on your computer. Some instead come with a USB adapter form the MicroSD so you can plug it in to a USB slot on your computer. Some examples of MicroSD adapters are shown below:

Use your adapter to attach the MicroSD card to your computer now.

The next step is to download operating system software to “flash” onto your MicroSD card. I recommend that you use the latest Raspbian “Lite” software for this. The Raspbian images are the official software endorsed by the Raspberry Pi Foundation for the RPi computers, and the Lite version is designed for use with “headless” systems. The Lite version does not contain graphical desktop software. If you wish, you can install the full (not-Lite) version of Raspbian instead. You may also be able to choose other operating systems here, but they may cause problems. SO I recommend the latest Raspbian Lite. You can always re-flash something else later if, for example, you want to abandon this project a few months from now.

You can find the latest Raspbian (Stretch) Lite image here (download it to your computer now):
https://www.raspberrypi.org/downloads/raspbian/

Probably you will want to tap the “Download Zip” button. The download may take many minutes to complete. (The time it takes depends on many factors).

There are many ways to flash the Raspbian image onto your MicroSD card. You can find information for flashing MicroSD cards from Windows computers, Apple Mac computers, and Linux Desktop computers, on this page at the Raspberry Pi Foundation site. Follow the instructions there to flash your MicroSD. Note that flashing the MicroSD card can be very slow depending upon your computer, the adapter, and the type of MicroSD card you have. Don’t be surprised if it takes 20-30 minutes. If it takes hours, then probably something is not working properly and you may want to stop the process ad restart.

When the flashing process is finished, your MicroSD card should appear as a “disk” on your computer system. Now we need to put 2 more files onto that “disk” before you safely remove it.

First create an empty file named “ssh” in the top level of the MicroSD disk. You can use a text editor program to create a new file, containing nothing yet, then save it with that name onto the top of the MicroSD.

Next create another file, and put the text below into it, replacing the my_wireless_SSID with the SSID (name) of your wireless network, and replacing my_auth_key with the pass phrase you use to autheticate to your wireless network:

country=US
ctrl_interface=DIR=/var/run/wpa_supplicant GROUP=netdev
update_config=1
network={
ssid="my_wireless_SSID"
psk="my_auth_key"
}

Now save the above file with the name, “wpa_supplicant.conf”, also at the top level of the MicroSD disk.

After both those files have been placed at the top level of the MicroSD, use the appropriate procedure for your operating system to safely remove the MicroSD from your computer. Please note: this “safe” removal step is important. If you simply pull out the MicroSD (unsafely) your files may not be written properly.

Now your MNicroSD card is read to be used in your Raspberry Pi. Mount the MicroSD into the slot on your Raspberry Pi, and then attach the power to the RPi.

Finding your RPi’s Address

In a short while your RPi will boot up with this Raspbian software on your MicroSD. Now what? If you have gone with the headless approach, you now need to “find” your RPi on your network, so you can connect to it remotely from your computer. If your RPi has a head, then you can use the keyboard and screen attached to the RPi, and you can skip ahead to the bottom of this section where I explain how to do this on the RPi console.

Most likely computers on your network have IPv4 addresses with one of these forms:
10.___.___.___
192.168.___.___
or less commonly (and less desirable):
172.16.___.___

The way that I usually find new computers on my network is by logging in to the administrative web page of my household wireless router, and looking under “attached devices”. There all the computers attached to my router are itemized, with their Local Area Network (LAN) addresses shown. I just look for one named “raspberrypi” and that’s the new one.

If you have a computer setup with Bonjour, you may be able to find your new RPi computer using the Bonjor name, “raspberrypi.local”. To try this, open a terminal window on your computer, and execute the command:
ping raspberrypi.local

If that succeeds, you should see output similar to this:
PING raspberrypi.local (192.168.123.115): 56 data bytes
64 bytes from 192.168.123.115: icmp_seq=0 ttl=64 time=185.198 ms
64 bytes from 192.168.123.115: icmp_seq=1 ttl=64 time=4.172 ms
64 bytes from 192.168.123.115: icmp_seq=2 ttl=64 time=4.082 ms

The IPv4 address of your RPi would then be 192.168.123.115, as shown above. If that ping command continues running forever, type Ctrl-C to terminate it. (Ctrl-C means, press and hold the control key, which acts like a shift key, and while holding it down, press the C key).

If neither of these approaches works for you, you may want to connect a screen and keyboard to your RPi. Then you can login to the RPi, using the login name “pi” and the default password, “raspberry”. Once logged in you can use the command below to get your IPv4 address:
sudo ip addr show | grep global

It should produce output similar to the following, where the 4 numbers following “inet” are your IPv4 address (again, 192.168.123.115 in this case):
inet 192.168.123.115/24 brd 192.168.123.255 scope global wlan0

Note that no matter how you get the IPv4 address of your RPi, this address may change over time because it is most likely assigned temporarily to your RPi by the DHCP server on your WiFi router. If you want a static, permanent address for your RPi you can set that up in a later optional step in this tutorial. For now use the address you discovered.

Once you have the IPv4 address of your RPi you can connect to it remotely using the ssh program. This is easy to do from an Apple Mac computer or a Linux Desktop computer (simply open a terminal, and execute ssh commands). If you are working from a Windows computer ssh access will be a little more difficult. For Windows computers only you will want to install the putty program for this.

To ssh to your new RPi, use a command like this (substituting your address, of course):
ssh pi@<address>

For example, to connect to the computer I showed above, I would use this command:
ssh pi@192.168.123.115

Connect to your RPi with ssh now, or use the console (if you have a keyboard and screen attached).

The default password to use when you login over ssh (or on the console) is “raspberry”.

raspi-config

Now that you are connected to your RPi, we need to configure Raspbian Linux so it can access the camera. Start by running the command below:
sudo raspi-config

This should open up a colorful text interface for configuration of your RPi software. Use the arrow keys and/or the tab key to move the cursor, and the enter or return key to make selections. Use this program to do the following configuration:

1. Change user password for the “pi” account to something you can remember
2. Setup boot options
Optionally select “Console Autologin Text console, automatically logged in as ‘pi’ user”
3. Setup network options
Set the hostname (otherwise it will remain “raspberrypi”)
4. Setup localization options:
Set the locale (in US I recommend using “en_US.UTF-8 UTF-8” with default “C-UTF-8”)
Set timezone (I am in Silicon Valley, so “US”, and “Pacific New”)
Set the wi-fi country (for me it is “US”)
5. Setup interfacing options:
Enable the camera (i.e., the hard-wired serial RaspiCam)
6. Select finish at the end of all that and let it reboot

Verify the RaspiCam works

The command below should create an image file without errors:
raspistill -v -o test.jpg

If the file is created without error then most likely everything is set up correctly. You could optionally actually view the file to make sure the image is correct, but to do that you will need to get the file to someplace where you can view it. So I don’t bother with that. I’d just remove the file at this point.

Well Known IPv4 Address

Optionally, your may configure a static permanent IP address to make this RPi easy to find. If so, select an IPv4 address that is outside of the range of addresses offered by the DHCP server on your router, but within the valid range for your LAN. If you accidentally select an address within the range offered by your DHCP server then someday another computer may be assigned this address by the DHCP server, causing a clash with this RPi. If you select an address outside of the valid range for your LAN, you will likely prevent your RPi from accessing the network at all.

To setup a static IPv4 address, edit the /etc/dhcpcd.conf as the “root” user. For example, you could use the command below to do this:
sudo nano /etc/dhcpcd.conf

The sudo command is used to execute a command as “root” when the command requires administrative privileges.

The nano program is a simple text file editor. If you are familiar with more powerful editors like vi or emacs, feel free to use the editor of your choice instead of nano.

Add something like the following at the bottom of /etc/dhcpcd.conf. Replace <ADDRESS> with the IPv4 address you want to use. Replace <ROUTER> with the LAN address of your router (your gateway). Typically the address of your router will be the same as your IP address, except that the last octet will be 1 (or less common now, 254). 1 and 254 are the lowest and highest octet values allowed in an IPv4 address. If you don’t know your router access, look at the network configuration of any other computer on your network, and look for the router or gateway address in that configuration. So add this at the bottom of /etc/dhcpcd.conf:

interface wlan0
static ip_address=<ADDRESS>/24
static routers=<ROUTER>
static domain_name_servers=<ROUTER> 8.8.8.8

Save the file, then reboot and verify you can access the RPi at this new static IP address:
sudo reboot

Wait a short while, then on your other computer (using your new <ADDRESS>, of course):
ssh pi@<ADDRESS>

Make Your RPi More Secure

Passwords are not a very secure method of authentication. In this section I will show you how to set up an asymmetric key pair to use for authentication. Once that is setup and working, you can choose to make this key the only way to remotely access the RPi.

Execute these commands:

cd
ssh-keygen

Press the enter/return key a few times in response to the prompts from this command. Then proceed with the commands below:

cd .ssh
ls

In that folder/directory you should see 2 files, id_rsa (your new private key) and id_rsa.pub (your new public key). These are simple text files with long strings of what appear to be random characters. You can use the linux cat program to view them. E.g., cat id_rsa. Copy the contents of these 2 files to your other computer.

Now rename your public key to authorized_keys:

mv id_rsa.pub authorized_keys

And remove the private key file from the RPi:

rm id_rsa

Now reboot the RPi so it will pick up this new configuration, then we can test key access:

sudo reboot

Now try to login to this RPi from your other computer, using your new private key:

ssh -i id_rsa pi@<ADDRESS>

The above command assumes the id_rsa file you copied over is in the current working directory. If you put it elsewhere, use the appropriate file path instead of just id_rsa.

If this works, you will be immediately logged in over ssh, without being asked to enter a password. It is important that you are not prompted for a password here. If you are asked to enter your password then something is broken.

If it works, and you are not asked for your password, then (optionally, but recommended) you can permanently disable all password access over ssh by editing this file:

sudo nano /etc/ssh/sshd_config

In that file make sure these 3 lines (in various places in the file) contain exactly the values shown below:

PasswordAuthentication no
PubkeyAuthentication yes
ChallengeResponseAuthentication no

Now reboot so this configuration will be picked up:
sudo reboot

At this point you could verify that password access is disabled, using:
ssh pi@<ADDRESS>

If everything is working it should no longer be possible to login this way.

Congratulations, you have set up a more secure RPi. Please note that you must not lose your private key file or you will be unable to login to this computer over ssh! Of course, you can still login using the console. But to do that you require physical access to the RPi, and you must attach a screen and keyboard. Hackers without that direct physical access will be unable to get access by cracking your password to access this RPi, now no matter how insecure your password was.

Install the Web Cam Software

Now login to the RPi again, over ssh:
ssh -i id_rsa pi@<ADDRESS>

Execute these commands (each one can take a few minutes to run):

sudo apt-get update
sudo apt-get install -y git cmake libjpeg8-dev
git clone https://github.com/jacksonliam/mjpg-streamer.git
cd mjpg-streamer/mjpg-streamer-experimental/

Now you have installed the tools needed to build the web cam software, and you have pulled down a copy of the web cam software source code. Now let’s build this software (this will take a while). Execute the commands below:

make
sudo make install

When that is done, the web cam software is all ready to go. So let’s manually run a quick test of the web cam before we do the configuration necessary permanently set it up to always start up when this RPi is powered up. Run the command below:

LD_LIBRARY_PATH=/usr/local/lib mjpg_streamer -i "input_raspicam.so -rot 180" -o "output_http.so -n -p 5001 -w /home/pi/mjpg-streamer/mjpg-streamer-experimental/www"

That command should show a few lines of output then “hang”, appearing it is stuck. That is because it is working and will keep working forever until we manually stop it. Now let’s go to a web browser on any computer on your LAN and and open this URI:
http://<ADDRESS>:5001/?action=stream

For example, I use this URI:
http://192.168.123.115:5001/?action=stream

If everything worked, you should see in that browser window whatever your RPi RaspiCam is looking at. Cool, right?!

For image quality comparison purposes, below is a screen grab from a browser on my laptop, streaming from an RPi Zero W with the cheap camera I linked above (pointed at the only tidy corner of my office). In fairness I should mention that this is actually a snapshot still, not a frame grab from the stream. If you change “action=stream” to “action=snapshot” in the URI shown above, you will get a single snapshot image each time you reload the page. I think the WordPress software I’m using here dumbs it down a bit to make it a bit more “webbable” too, so this is probably a little worse than the original.

There are a couple of things within the command that you can reconfigure.

Try removing or changing -rot 180 (rotation) to other numeric values (only 90, 180, or 270 are allowed). These control the orientation of the video stream, relative to the camera. That is, you can orient your camera and its wires as you please, then you can correct the stream with this number.

Also, the TCP/IP port number where the streaming server listens for connections is set with -p 5001. I selected the number 5001, but you could use any other reasonable port number you wish here. Note however that you must pick a number larger than 1024, or you will have to run the streaming server as the “root” user, which creates a security risk.

Note that this streaming server is accessible within your LAN only. If you want this web cam to be accessible outside of your LAN, then you will need to use the “port forwarding” feature of your router/firewall to map some external port number to your RPi address and port. That is beyond the scope of this tutorial. Consult the documentation of your router, or Google for info.

If you wish, use Ctrl-C to terminate the streaming server, change the rotation and/or the port number, then run it again until you have the command the way you want it. Then we can work on making it permanent.

Automate the Web Cam Startup

At this point you need to manually run that long command over ssh any time you want the web cam software to run. Now let’s make it so the web cam software automatically starts up whenever the RPi is powered up. Then you can freely unplug it, move it, or it can automatically come back up after a power failure, etc. Also, I want to simplify the URI to make it easier to type.

Begin by saving the existing home page for this server, in case you ever want it back:

cd ~/mjpg-streamer/mjpg-streamer-experimental/www
cp index.html OLDindex.html

Now edit this page:
nano index.html

Delete everything in the file, replacing it with the exact content below:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN"
"http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en">
<head>
<title>3D Printer Cam</title>
<meta http-equiv="content-type" content="text/html; charset=iso-8859-1" />
</head>
<body>
<div>
<img width="100%" src="./?action=stream" />
</div>
</body>
</html>

Save the file and exit from the editor.

Now edit /etc/rc.local as “root” to configure automatic startup:
sudo nano /etc/rc.local

Add the following at the bottom of the file, but above the last line (which contains “exit 0”). If you have selected a different port number above, then replace “5001” below with that port number. If you need a different rotation setting, edit the number after “-rot” below as appropriate:

# Start up the streamer
touch /var/log/mjpg_streamer
chmod a+rw /var/log/mjpg_streamer
su pi -c 'LD_LIBRARY_PATH=/usr/local/lib mjpg_streamer -i "input_raspicam.so -rot 90" -o "output_http.so -n -p 5001 -w /home/pi/mjpg-streamer/mjpg-streamer-experimental/www" > /var/log/mjpg_streamer 2>&1 &'

Please note that the “-n” argument to the command is important, since this disables remote command access to the streaming server. Don’t remove this unless you know the web cam cannot be accessed by untrusted parties.

Save the file. Then reboot again:
sudo reboot

Give it a few minutes to start up, then use the browser on any computer on your LAN to access this URI (substituting your IP address and changing the port number if you are not using 5001):
http://<ADDRESS>:5001/

If you use a computer with Bonjour (e.g., any Apple Mac), you should be able to use your RPi’s hostname instead of its numeric IPv4 address. So for example if your RPi was named “marvin” and you are using port 5001, you should be able to use this URI on your Bonjour computer:
http://marvin.local:5001/

That’s it!

So there you go. You have a fairly securely configured web cam that you built all by yourself!

How did the process go for you? Were there any confusing parts in this guide? Did you get stuck somewhere? How could I improve this guide to make it more understandable or easier to follow? Please share your thoughts below…

Leave a Reply

Your email address will not be published.

This site uses Akismet to reduce spam. Learn how your comment data is processed.