Monday, January 15, 2018

Raspberry Pi Home Security Cam Project


Marcus and Agata are just documenting the latest security camera to enter our household.  We are not quite into double digits yet, but let's just say we now have cameras, sensors and a smorgasbord of Raspberry Pi computers controlling aspects of our home in England.

It's part of Agata's Home Automation initiative, whose focus is to make our lives easier in the longerterm, although quite complicated short term!

Security Camera
This is a Raspberry Pi Zero W powered security camera.

You can use it to just be a HTTP, i.e. web accessible camera of something e.g. another room, the outside world.

You can also set it to perform timelapse photography, or to record a video when motion is detected

It could then send that video to secure cloud storage for you to view, or as evidence to the Police in case of burglary

Additionally our configuration includes a web portal so given a routable Internet connection you can check your webcam from your Smartphone when you are not home

What You Need to Order

- Raspberry Pi Zero W   16 GBP

- Case   6GBP

- Camera V2    24 GBP

- Shim converter to connect keyboard   2 GBP

- SD Card   9 GBP

You will also need a USB power adapter and a Full size to Micro USB cable to power the Zero W

Build the Pi

- Get the parts and assemble

- Install the Camera using the correct ribbon cable for Zero W, instructions here

- Install the Micro SD card with the Raspbian OS already on it


- Put the Zero W into the case, you should not need to open it up again

- Find some duct tape and stick your Pi to something.  I chose my wonderfully inexpensive IKEA Tertial light.  Something that I also recommend!

I'm always forgetting but the micro USB power is at the end of the Raspberry Pi and the other micro USB, one closest to the camera, is the one that we put a Shim converter in for the keyboard dongle.


Basic Pi Housekeeping

- Update the OS

apt-get update
apt-get upgrade
apt-get dist-upgrade






- Use raspi-config program to set Gui logon for start

- Then when GUI booted using your attached keyboard goto top of screen and attach to your wireless network.  You must do this since after initial config you will do all the logon and config work not from the console but on a remote terminal logon (connected over the Wifi)



-  Enable the Camera SSH and VNC, then reboot at the prompt

- I set a Static (not Dynamic) IP address so that I can have this Camera server, serve the pictures to the internet.  In the example below I edited /etc/dhcpcd.conf

# mwb 20180112 static on 11net 100G 

interface wlan0
static ip_address=192.168.7.200/24
static routers=192.168.7.1

static domain_name_servers=8.8.8.8



Camera Software

Initially I used the motion software package.   Here are some instructions 

But my results were a very sluggish Webscreen, and the Pi Zero W running over 90% CPU.  So instead I uninstalled and then settled on this


RPi-Cam-Web-Interface

- Follow the instructions in the Link https://elinux.org/RPi-Cam-Web-Interface

- Personally I chose a non standard 8089 port and also a userid and password for security and Apache2 as the webserver

- On a tiny processored Pi Zero W the installation took over 10 minutes but did amazingly install and configure PHP7 and Apache2 as well as the application software

- The software unpacks into a directory called ./RPi_Cam_Web_Interface


- Once installed you can test it at the address and port you configured. So for Marcus this was

http://192.168.7.200:8089/html/



#Scheduler
http://192.168.7.200:8089/html/schedule.php

- There are buttons to start timelapse, video

- You can see a static Photo i.e. does not move at
http://192.168.7.200:8089/html/cam_pic.php


Connecting to the Internet



So you can broadcast you webcam to the world.  you need to channel the port you chose, for me 8089 from the router to the static Raspberry Pi Camera address.  From my router like this

Associating your IP with a name

- Get your IP address as seen from the Internet


 dig +short myip.opendns.com @resolver1.opendns.com

- Sign up with noip.com 

Then for free (monthly nag) you can sign up and associate a name with an IP address,  for example  myraspberrycamera.ddns.net  or whatever has not been taken!


- So in this example it would then be

http://myraspberrycamera.ddns.net:8089


4G Internet Connection Issues

Your home Internet connection should be able to port forward  your chosen port from the router to the Raspberry Pi.  But this will only work if your Internet Address is unique and routable.

Some ISP do not provide individual users with a unique IP address.  This is usually via CGNAT Carrier Grade NAT  If this is the case, for example in the UK using 3 mobile, then even when you use the option above it will not work.


Maintenance 
In the same install directory as start.sh stop.sh is cam.sh and this allow for maintenance and reconfiguration at the system level, example change media file location



Techy Twiddling Bits









Like any good product you can customise it or change so many things that it stops working   (screenshot your original configuration!)

Others and Costs
This worked example built a security cam from a Raspberry Pi but there are countless other webcams available.  Some of the others:

- Fully packaged product, no physical assembly or code installation

- Higher cost, often a lot higher cost

- May also ask you to stump up for a monthly subscription to store the video it takes when motion is detected.  And since those cameras (not ours) have a closed architecture if you want those motion events you HAVE to pay. Hmmm

- In the Raspberry solution motion files are stored in a known place for me  /var/www/html/storage/media . So you can write a shell script to take the last pictures or movies and push them to you via email or send them to your existing cloud storage.  And put it into the cron scheduler to make this check periodically. Or there are ways (I did not investiage yet, but looks like you can have it call your script on start and end of motion event)


In Summary
Yet again the Raspberry Pi is able to provide a decent Home Automation solution, this time as a motion sensing WebCam.  Really recommended & quite a lot of fun too.

Importantly once built there is no ongoing subscription cost

#RaspberryPi
#WebPortal
#SecurityCamera