When you scan this page, it looks daunting. It really isn’t. It shouldn’t take more than an hour to go from a bare Pi to a full motion detecting security camera, including installing the OS on the Pi. A bit of patience and follow it methodically.
What you’re going to need:
- A Raspberry Pi Zero – This is small and cheap. The link is for a kit that’ll include everything you need apart from the power supply. It also comes with a heatsink and some other bits that you won’t use. The important bits are the Pi, the case, the SD card and the camera cable.
- A Raspberry Pi Camera – I went for a 5mp version that plugs directly into the camera socket on the Pi. You can get an 8mp version as well. The link here takes you to the version with IR filtering. This is a daylight only camera so you’ll need to make sure the area is well lit at night. There is a version with no IR filtering that works with IR lights. This has a black circuit board rather than green. I’d prefer to use the daylight one with a normal security light as the IR lighting can be a bit hit and miss.
- Power – You can save a couple of quid by buying a 3rd party PSU but do yourself a favour and get the genuine one. It’ll save you corrupt SD cards and a dead Pi in the long run.
- Etcher – This is a piece of software that you can use to write the operating system image to the SD card
- An operating system – I’d recommend downloading the latest version of Raspbian Lite over the desktop version. You want to keep things as streamlined as possible on this as you’re not going to be using it as a desktop computer.
- The software to handle the camera – I’m basing this project on something called RPi Cam Web Interface – more on this later
- Wide angle lens – This is optional but I find that it helps get a better field of view depending on your camera. All I do is use the lens I want and carefully superglue it to the front of the Raspberry Pi case.
Operating system and Pi Preparation
Don’t put everything in the case just yet. Plug the camera in using the cable that was supplied with the Pi kit, not the one supplied with the camera. And plug the other cables in as below.
As mentioned, my recommendation would be to go for Raspbian Lite. It’s going to be a little trickier to configure than the desktop version but there’s less to worry about in terms of storage space and CPU resources. You can get it from the official Raspberry Pi download page.
To install it, the best thing would be to follow the official instructions. It can change from time to time so the page I’ve linked to will get updated when required. In brief terms, this is what your going to do:
- Put the SD card into something you can use to plug it into a computer.
- Use Etcher to flash the card with the operating system you downloaded.
- Put the SD card into your Pi
- Using the supplied HDMI converter, plug the Pi into an HDMI port on your TV or monitor
- Using the supplied USB adapter, plug a keyboard into the Pi
- Plug in the power supply
On your monitor, you’ll see some raspberries at the top of the screen followed by a lot of text scrolling past. Wait until you get your login prompt. The user name is pi and the password is raspberry.
Now, assuming you’re logged in, you need to set a few things up.
sudo apt-get update
Let that run and then type in:
sudo apt-get dist-upgrade
This will make sure all the latest security patches and bug fixes are applied.
Now type in:
This will give you a screen like this:
You can use the arrow keys and the tab key to move around. Use the enter key to select the option you want to play with.
The first thing you’re going to do is change the password. This is non-negotiable. You’re going to be opening your Pi up to the world a bit later. I did a test where I left it as default. An hour later, some Russian schoolboy had hijacked my Pi, denied me access and turned it into a Bitcoin mining computer. So do it or everything you’re doing now will be wasted.
Next, go into Network Options. You can change the hostname if you want. the default will be raspberrypi. You will also need to set up your WiFi. This will ask you for the SSID and the passphrase. It won’t scan and ask you to select one so you’ll need to get this from your phone or computer. The Zero only supports 2.4ghz so make sure you get the correct one!
You can leave boot options and go into Localisation Options. There are 4 things you’ll need to set here depending on your country. They might be correct already but better to go in and check. Pis can be particularly sensitive about your WiFi country to take care to set that correctly.
Lastly, go into Interfacing Options. In here you want to enable the camera and SSH. SSH will mean that you can login to your Pi from another machine rather than having to bring it back to your TV and plug in a keyboard of you want to do any work on it.
Now your Pi should be ready to go. When you get back to the main menu, select “Finish”. Your Pi will reboot and will hopefully connect to your WiFi.
To get to this point shouldn’t take more than 15 to 30 minutes. You’ll probably be waiting longer for the updates at the start than you will actually doing stuff. Don’t unplug anything until you’ve checked that your Pi is connecting to your WiFi. So, once it’s rebooted, login again. Now type in ping google.com . You should see it return Google’s IP address and a time in milliseconds at the end. It’ll keep doing this until you press ctrl + c on the keyboard. If you get an error then you need to check your settings in raspi-config. If it works then we can move on.
You’ll get some stuff back. The important thing will be the IP address for wlan0. It’ll look something like 192.168.0.10. Make a note of it.
What you want to do now is set your Pi up to use a static IP address on your network. Type in:
sudo nano /etc/dhcpcd.conf
In this file, you’ll see lots of stuff but have a look for a section that says something like:
# Example static IP configuration:
But the lines will have a # at the beginning. Remove the #. Change the static ip_address to the IP address – just the bit before /24 – you found when you did the ifconfig. Change the static routers and static domain_name_servers to the IP address of your router. I can’t tell you want this will be. 192.168.0.1, 192.168.1.1 are often used. If you don’t know what it is, leave the # at the beginning of those lines.
Now press ctrl + x on your keyboard. Hit the y key and just hit enter to keep the same filename.
sudo shutdown -r now
This will reboot your Pi so once this completes, login again. Just to make sure you haven’t buggered anything up, type in:
If this works then you now have a static ip address on your Pi.
What you want to do now is make it secure. There’s a great page about this on the Raspberry Pi site. Really, you should do all of this. If you’re comfortable with a bit of Linux system administration then go ahead. You’ve already changed the password so that’s one thing. Personally, I’d add key based authentication. This means that you have a key on your computer that has to match one on the Pi. If you do this then it disables the username and password login so you can only login from a machine with the key. This is good, but it means you’ll have to add the key to every computer you want to SSH in from. This might be a pain but it’s advisable from a best practice point of view.
I would also install UFW and fail2ban. fail2ban will just do its thing but UFW will need a bit of tweaking which I’ll cover at the end of the next section.
The software I use is RPi Cam Web Interface. When I started, I was using something called MotioEye. This has a pretty interface but I found the motion detection and recording to be unreliable. RPi Cam also has buttons so you can manually take pictures or video through the web interface.
So point your web browser at their wiki page. This has TONS of information but focus on the Installation section for the moment. Don’t worry about doing another update for now as you did that 10 minutes ago. You can start at step 4. This has 3 commands to run:
git clone https://github.com/silvanmelchior/RPi_Cam_Web_Interface.git
When you run the install.sh command, you’ll get a screen a bit like the raspi-config one. In order, this is what I have mine set to:
Cam subfolder: cam <<< you can call this what you want though.
Server: (apache/nginx/lighttpd) – apache <<< Unless you know what you’re doing, I’d recommend apache
Webport: xxxx <<< Choose a number that’s in the high 4 digits. Maybe something like 8720. If you install more cameras, remember to increase this by 1 for each new camera
User: xxxxx <<< Choose a username
Password: xxxxx <<< Choose a secure password
phpversion: 7 <<< This is current at the time of writing
Now when you tab to ok and hit enter it will set everything up for you, installing all the bits you need. This might take several minutes so be patient.
You’re almost there!
The last little bit is that you’ll need to allow access to your camera through UFW. There’s just 2 commands to do this:
sudo ufw allow 22
sudo ufw allow xxxx/tcp
The first allows you to ssh in on port 22. The second, you’ll need to replace xxxx with the port number you used when you set the cam software up – could be 8720.
This is the bit where you cross your fingers.
Go to your computer, open a browser and in the address bar type in the IP address for your Pi. Remember? It was something like 192.168.0.10? Don’t hit return yet. What you’re going to type in is:
This assumes you used 8720 as the webport and cam as the subfolder when you set RPi Web Cam Interface up a few minutes ago.
Have you got a login prompt?
Now enter the username and password you chose when you set up RPi Web Cam Interface. Not the ones for the main pi account.
If it’s let you in then you should see something like this:
Only you won’t. Firstly, that’s my front garden 🙂 . Secondly, the aspect ratio will be wrong and you’ll probably find the image is cropped weirdly. Now, when it’s the the case, your camera is going to have the cable in the side and the power cable is going to plug in the top. So, click on camera settings. These 2 images are the top and bottom half of my settings. You can click on the thumbnails for a bigger version and copy these as a good starting point.
There are a few important ones:
- Everything in the resolutions section
- Image quality
- Preview quality
- Motion detection mode
Now, turn on motion detection by clicking on the button. Wave your hand in front of the camera and it should start recording. Once the recording button turns off again, you can then click on the button to download videos and images to see what it did. By default, it’ll record a few seconds after it’s stopped detecting motion. All this can be tweaked.
There are lots and lots of things that you can fine tune. I’m not going to go into them here. If you just do this, you’ll have something that will detect motion and record video. You can use this for security. You can use it on a bird table. What you do now is up to you.
I 3D printed a little mount for mine and it sits in my porch so it picks up everything that goes on in front of the house. Below is a shot of the camera, a shot of it in place, and a video that was captured 5 minutes ago when we had a delivery. This is quite useful as they often leave parcels in the cupboard on the right and don’t put a card through the door. I have the video set to 15fps which is why it’s a bit jerky but this keeps filesizes down and it’s still perfect for this sort of application.
Access from outside your network
If you want to access your Pi from outside your home, you’ll need to do a bit of extra work. The simplest way is to find out what your public IP address is. Just type in “what’s my ip address” into Google and you’ll get an answer. This will be different to the IP address you found earlier.
The next step is to login to your router. You need to set up port forwarding on your router. Let’s say your public IP address is 22.214.171.124. You’ll access your camera using 126.96.36.199:8765. At the moment, your router probably has your phone, a couple of tablets, computers and consoles connected to it. So it needs to be told which device it needs to send people to if they want port 8765. You do this by setting up a port forward on the router. This is going to depend on the router you have so I can only give you rough instructions.
You need to create a rule for TCP traffic that forwards traffic on port 8765 to the INTERNAL IP address of your Pi. I have the following set up on my Virgin router:
- Port 8765 is my RPi Web Cam Interface
- Port 22 is so that I can SSH in from outside
- Port 8081… This is a port that just shows your video stream. So, if you want to embed your stream on a page or just send it to someone to have a look without giving them access to anything then you’d send them to, for example, 188.8.131.52:8081
That’s just about it. Hopefully, there’s enough info here to get you going!
There’s a package called rclone that you can install so that you can upload to something like Dropbox or Google Drive. I use this and it works well. If you want to have a look, this is their wiki page with instructions. In all honesty, everything you need should be on that page. I’m trying to think if there were any “gotchas” but I’m pretty sure it was all straight forward.