I was inspired by scavix’s work with the instructable Raspberry Pi as low-cost HD surveillance camera so I decided to make my own Raspberry PI based IP camera that also features POE and supports integration with the Google Drive.
The video streaming can be simply viewed from a web browser. Most desktop browsers should work, I have also tested with Firefox for Android and it seems to work fine. It is also possible to view the video stream from multiple cameras the same time by making a simple HTML file, but more about that later.
The camera footage is saved in the form of JPEG images instead of video files in order to make the uploading to the Google Drive easier for Internet connections with low upload speeds. To prevent Google Drive running out of space, every one hour the system checks for any images that are older from a specific threshold (e.g. 2 days) and automatically deletes them. After that it syncs the local footage directory with the Google Drive.
All the partitions on the microSD are read-only to prevent corruption from power failures. An external USB drive is also used to keep the /var and /home partitions which they both need to be read-write. That way the system is unlikely to become unbootable from a power failure since the microSD is 100% read-only.
I built two of those IP cameras a while ago and they have been tested on production environment (a small retail store) for more than a year and they have shown absolutely no issues. Since the day they have been installed both cameras have 100% uptime. So I’m pretty confident that they can be used as low cost alternative to commercial IP cameras in a small business.
Step 1: Tools and Parts
To build the camera you will need the following parts:
- A Raspberry Pi Model B+[30€] – It will also work with a Model B but it won’t fit in the housing I used. I haven’t tested it with a Raspberry Pi 2 but I think it should work.
- A Raspberry Pi Camera Module[30€] – Plus at least two screws that fit its mounting holes.
- An 8GB Class10 MicroSD[5€] – I used a Kingston SDC10. A larger capacity MicroSD can also be used but it won’t make a big difference.
- A Low-profile USB Flash Drive at least 8GB [6€] – I used an 8GB SanDisk Cruzer Fit CZ33.
- A TP-LINK TL-POE10RPOE Splitter[16€] – You can use any POE Splitter that supports the 802.3af POE standard but this particular one fits perfectly inside the housing I used.
- A POE Injector (or a POE Switch) – This is not part of the camera itself but you will need it to power the camera. I used a TP-LINK TL-SF1008P POE Switch [40€] since I have more that one IP camera (it supports up to four), but for just one camera I recommend the TP-LINK TL-POE150S POE Injector [25€]. If you can’t find either of those, any injector or switch that supports the 802.3af POE standard should work.
- 1 x Dummy IP Camera for the housing [9€] – I highly recommend getting the same with me because all the hardware fits perfectly inside. You can find it fairly easily if you search on ebay for “dummy ip ir camera” (you can find it in either silver or black color).
- 2 x RJ45 jacks plus a small piece of UTP cable (Cat5e or Cat6) [less than 1€]
- 2 x Female to Female Jumper Wires [less than 1€]
- 1 x Hot Glue Stick
- Solder Wire and Solder Wick
And the following tools:
- A Soldering Iron
- A Hot Glue Gun
- A Desoldering Pump – Optional if you have solder wick.
- A Dremel with a disk that can cut plastic plus drill bits
- An RJ45 Crimping Tool
The total cost of the camera parts (excluding the POE Injector/Switch and the tools) is around 100€. The prices can vary depending where you live and from where you bought them.
Step 2: Preparing the microSD
Burning the Installer Image to the microSD:
As base for the system I decided to use a bare minimum Raspbian install and on top of it install only the required packages. That way the system is going to be more secure, more stable and there will be no waste of resources for things that are not required.
To get started first download the Raspbian NetInstall image from Github and burn it to your microSD.
If you are using Linux just download the latest raspbian-ua-netinst-<latest-version-number>.img.xz file and use the following command:
Where X is the letter of the device that corresponds to your microSD e.g. c. Before running the command make sure that there are no mounted partitions that belong to the microSD. In case there are use the following command to unmount each one of them:
In case you got a permission denied error try the following command:
But be extremely carefully here, using the wrong letter in place of X may do irreversible damage to your system and ruin your day. Before running the command double check that the letter you typed in place of X is really the one that corresponds to the microSD.
If you are using Windows download the raspbian-ua-netinst-<latest-version-number>.zip file instead. Format then your microSD as FAT32 and extract the installer files in it.
Installing the System:
When the installer is finally written to the microSD it’s time to install the system. The installation process is completely automated and non-interactive, the only thing you need to do is to insert the microSD to your Raspberry Pi, plug it in to your router using an Ethernet cable, connect the camera module, plug the power cable and it will automatically start to download and install the packages. The installation process takes around 45 to 60 minutes depending on the speed of your Internet connection. If you have an HDMI cable and a monitor you can keep an eye to the installation process. If you don’t have a display attached you can monitor the Ethernet controller LEDs to guess activity.
Step 3: Establishing an SSH Connection
When the installation is finally complete the Raspberry Pi will automatically reboot and then you will be able to connect to it through SSH. On the first boot the Pi is going to get dynamically an IP address from the DHCP server of your router, so at first you will need to discover its IP address. On Linux this is fairly easy, you can just run the following nmap command as root:
Where x.x.x.x is the IP address of your private network e.g. 192.168.1.0 and the y is the number of ones (in binary) of the network mask e.g. for the network mask 255.255.255.0 the number of ones is 24. So, for that particular network you would run:
Another way to obtain the IP address of your Pi is to check the logs of the DHCP server of your router. Every router is different so I’m not going to describe that process. There is also a version of nmap for Windows that you can try.
When you obtain the IP address of your Pi you can SSH to it using the following command on Linux
Or by using PuTTY on Windows.
The default root password is raspbian.
Step 4: Configuring the System
On the first boot the system is almost completely unconfigured so there are some tasks you will need to do first.
The very first thing you need to do is to change the default root password by running:
Then, you will have to configure the locales. You can do this by running the following command:
Go ahead and select all the en_US locales by using the space bar plus any other locales you want. When you are done hit Enter. Lastly, select the en_US.UTF-8 as the default locale and hit Enter.
Next you will need to configure the timezone. To do this run the following command:
At this point it’s a good idea to do some updates:
Next thing you need to do, is to install a text editor in order to be able to edit configuration files. One of the easiest to use terminal text editors is nano:
When you are done editing a file using nano hit Ctrl-X to exit, type y and finally hit Enter to save the changes.
Next you need to create a normal user to use instead of root to make the system more secure and also reduce the risk of human error:
And to change the default shell to bash run:
And replace /bin/sh with /bin/bash on the line that starts with raspcam.
Now you need to give that user the ability to do things that require root access so you need to install sudo:
Then to give your user the ability to use sudo run:
And right after the line:
put the following line:
From now on you will use that user for anything you want to do and when you need root access you will use sudo.
Next to make the system a little more secure it’s a good idea to prevent root login through SSH. So, run:
Change the yes to no.
Next, install the raspi-copies-and-fills package for improved memory management performance:
Now it’s a good time to give your Pi a static IP so you won’t have to search what IP address it has all the time. To achieve that run
Comment out or delete the line:
And after that line add following lines:
Next you need to set the nameservers. To do that run:
Typically, the primary nameserver will be your router, for the secondary nameserver you can use the Google Public DNS which is 22.214.171.124. To add the Google Public DNS as the secondary nameserver, at the end of the config file add the following line:
Finally, for the changes to take effect you need to restart the networking service:
After restarting the networking service the SSH session is going to hang, because the IP of the Raspberry Pi has now changed so you will have to reconnect, but this time using the new user you created instead of root.
After reconnecting to your Pi you need to enable the camera module. To do so first you need to install raspi-config:
And run it as root:
Select the Enable Camera option and then select Enable. Finally, answer yes to the question if you want to restart the Raspberry Pi now. After the reboot reconnect to it through SSH.
Step 5: Installing Motion
At this point you are finished with the basic system configuration, now it’s time to setup the video steaming. To do that you are going to use motion:
After installing motion you will also need to install some more libraries:
There is a known issue with motion and the Raspberry Pi Camera Module which can be fixed by replacing the original binary of motion with the one from this fork. To do that just run the following commands:
Now you need to configure motion. First make a directory to keep the camera footage and a second one for the log files:
Next, you need to create a log file in /var/log directory and give motion write access to it:
Enable the motion daemon by running:
And changing the no to yes.
Now download the attached motion.conf file and put it in the /etc directory:
There are a couple of things you may need to edit in the configuration file:
First, go on the line:
And change the username and password to what you want. You are going to use that username / password pair to access the video stream later from your web browser.
By default motion is going to take a snapshot image every 6 seconds and save it in the /home/raspcam/data/cloud directory. You can change that interval if you want by editing the following line.
Bare in mind though that the smaller the snapshot interval is the larger the USB drive and the available space on Google Drive need to be.
The settings I provide you are what I found it works best for me after a lot of adjusting and tweaking but that does not mean you have to keep it as is, there are many other options you may want to play around with. So, feel free to play around with the settings and if you mesh up something you can always redownload the configuration file.
Now that everything is configured you need to start the motion daemon by running:
For more detail: Raspberry Pi Cloud IP Camera with POE