Intro: Simple Pi CCTV With Online Video
Ever wanted to have an eye on your room, holiday home, own home, garage, work place or anywhere else for that matter but the facilities available (e.g. limited internet supply) prevented you from having a sustainable CCTV system from working, or you simply did not want to spend big?
Then this tutorial is for you.
This guide is to help you produce a multi-camera system with a raspberry pi that takes snapshots at specific time points each hour, uploads these simultaneously to a website. The website will show the latest snapshots with a time-stamp as well as store all previous uploads into a separate folder for your reference online and on the Pi.
I have tried to provide a lot of basic information and links so that anybody with even ZERO knowledge can pick themselves up and do this tutorial - as I had learned the hard way! (so why should you!).
So, let's go.
Step 1: Some Notes Prior to Starting:
- I have used Ubuntu MATE as my OS in this tutorial but any other linux-based OS should be fine (it has been tested successfully on Raspian).
- Try and decide where you want the cameras to be (remember web cameras are not water proof, but I am sure with some ingenuity you could make them so). In all seriousness, the webcamera cables are short for a house, so consider using USB extension cables (either standard USB or USB-over-ethernet).
The very, very basics of navigating in terminal is explained quickly here as it may be of use to you:
typing 'ls' displays files where you are
typing 'cd' changes the directory to the starting directory of terminal
typing 'cd' followed by a folder name (e.g. cd Downloads), will move you to that particular folder.
pressing 'Tab' key as you start to type a folder name will type the rest for you (as long as no two folders are named similarly).
Step 2: Equipment List:
Raspberry Pi 3 Model B (this version comes with built-in wifi, but other versions fine as long as you buy wifi adaptor)
Keyboard, mouse, micro-usb power supply, HDMI cable (to supply the Pi)
Targus USB 2.0 Y Cable Powered 4-port Hub (to supply power and act as a data input for the web cameras)
USB Webcams (almost ANY will do)
USB Cable Extendors (5m is the limit for standard USB cables unless you use USB-by-Ethernet wires)
A wifi internet source.
Step 3: Setting Up the Webpage Beforehand:
- Make a website (one I recommend is with https://www.000webhost.com)
- I have included a html source code that you can use as a template.
- An easy way to make the html file is to add it to use notepad or textedit, depending on your OS, you may have other options. (link: http://www.w3schools.com/html/html_editors.asp).
- The good thing about using 000webhost (apart from being free) is that the lftpwebcam script that I have made can easily be edited with your website, login details and password and the directories are half-made for you already (in the sense that the 'public_html/' part is the same for everyone), so all you have to do is add the extra directories yourself. 000webhost provide basic file management support to help you do this.
- Finally, if using 000webhost, for instance, place the document into the 'home' folder online. In this case, it would be the /public_html/ directory as this is the designated home folder. The way the code works is that as long as the other folders and subfolders are in the correct places, it can find the latest image and show it on this html page.
All you then have to do is access the webpage and your cameras are there to view!
Step 4: Installing the OS
I assume you know how to install Raspian, Ubuntu MATE or other OS' onto the Pi (otherwise, this is very helpful: https://www.raspberrypi.org/documentation/install... )
Step 5: Installing the Programs
Install fswebcam and lftp
- fswebcam (this is to take photos with the webcam)
- lftp (this is to upload your files onto your website)
I assume you know how to install applications (if not, here's a link: https://www.raspberrypi.org/documentation/usage/w... )
Step 6: The Set-up
Often I think that pictures are more confusing than just saying things clearly.
Hence, I am just going to write it out:
- Connect the keyboard, mouse, HDMI cable and 4-port hub to the Pi
- Join the webcameras to the 4 port hub's ports (extending the length of the cameras prior to connecting, if need be) and then the USB device to the Pi
- Finally, connect the power supply to the Pi to turn it on
Step 7: Introducing Webcam_cron and Lftpwebcam
After loading, copy the following code onto your home folder - webcam_cron and lftpwebcam.
Use a pen drive, ethernet network, drop box...whatever you fancy to get the files across. I used http://www.filedropper.com
Step 8: The Webcam Routine
Edit webcam_cron to state how many webcams you are using and where you want to save them.
This step only matters if you want to change it from its current settings.
To edit documents, you will need to use a script editor such as nano (link: https://www.raspberrypi.org/documentation/linux/u...
#!/bin/bash //assigning the date variable DATE=$(date +"%Y-%m-%d_%H%M") //taking photos with the webcameras fswebcam -d /dev/video0 webcam1/$DATE.jpg
fswebcam -d /dev/video0 webcam1/still/image1.jpg fswebcam -d /dev/video1 webcam2/$DATE.jpg
fswebcam -d /dev/video1 webcam2/still/image2.jpg
fswebcam -d /dev/video2 webcam3/$DATE.jpg
fswebcam -d /dev/video2 webcam3/still/image3.jpg
There is one file from each picture that is saved as the 'most recent' - the image[x].jpg file. Plus, there is a command to produce, simultaneously, the image in a separate folder with the date.
NB: if you want to keep the script, as is, you must make the directories that exist in the script. (e.g. webcam 1 needs to have a 'webcam1' folder followed by a 'still' subfolder - all in the user folder location. (e.g. if you called your computer Sam, then the folders need to be in the Sam user folder, found in "/home/Sam/")
Step 9: Setting Up the Ftp Transfer
Edit lftpwebcam to state what your website ftp login details are, where your files are on the hard drive and where you want them to be online. Your website control panel will give you your ftp details that you can input here in the second line of code below.
But you also need to make the subfolders (e.g. livest, webcam1_storage etc.).
Remember that the code here is half-made for 000webhost already (i.e. the public_html part).
lftp -u username,password subdomain.domain.com <<EOF
mirror -R -x RX webcam1/ public_html/livest/webcam1_storage
mirror -R webcam1/still public_html/livest/webcam1
mirror -R -x RX webcam2/ public_html/livest/webcam2_storage
mirror -R webcam2/still public_html/livest/webcam2
mirror -R -x RX webcam3/ public_html/livest/webcam3_storage
mirror -R webcam3/still public_html/livest/webcam3
The code is such that the files are copied from the hard drive to the website IF they are not already there - that is, only the new files are uploaded.
This should help with bandwidth issues if you are restricted with this.
Step 10: "The Executables"
Then, once you have copied the code in and done editing, you need to make these files "executables".
Do this by typing in the folder they are located:
sudo chmod 755 lftpwebcam
sudo chmod 755 webcam_cron
Now that this is done, they can run as commands.
Step 11: Checkpoint
You can test the scripts out by typing in the full directory of their location followed by the file name.
Treat this as a checkpoint as if there is an issue here, then you can look back and see if:
- you have made all the correct folders and subfolders
- you have put in correct password and login ftp details
- or any other mistakes
Step 12: Cron-time!
Copy the following into sudo cron and this into cron. (I assume you know how cron works, if not, here's a great link: https://www.raspberrypi.org/documentation/linux/u...
5 * * * * /home/[YOU]/webcamcron
5 * * * * /home/[YOU]/lftpwebcam
35 * * * * /home/[YOU]/webcamcron
35 * * * * /home/[YOU]/lftpwebcam
//edit as seen fit (e.g. when you want more photos taken more frequently) [YOU] means the name of your directory (based on the name you give your computer).
For those new to this, the 'cron' will run scripts to simultaneously take photos and then upload them. This can be customised. See the following link for customisation information.
first type sudo crontab -e put in your password and get in type the following two lines:
@hourly service network restart 30 * * * * service network restart
The purpose is that wifi often drops, so the 'sudo cron' ensures that wifi is always on. I have set it so that there is a network restart before each upload to make sure it's definitely uploaded. So far this has worked perfectly for me.
NB: @hourly is the same as 0 * * * *.
Step 13: Check ... and Done! Oh, Don't Forget To...
So you have loaded, connected and programmed the scripts and the cron tasks accordingly.
Now just enjoy!
Remember you can add or delete extra webcams from the script as you wish. Although I have added 3, I would be interested to know how many others have managed.
Perhaps a powered usb hub to each of the 4 Raspberry Pi Model B ports could total 16 cameras overall?...
Oh and one last thing...don't forget to back up your OS (you don't want to see your hard work disappear!).