The Raspberry Pi is an inexpensive ARM based computer that is ideal for use as a home security camera.
What will you need?
- Web server CPanel or dedicated web server to upload images to. (I use a $2.5 a month Vultr server).
- Raspberry Pi (Raspberry Pi 2/b setup guide here).
- Raspberry Pi 8MP camera.
Remote Web server
Install and configure FTP on the remote web server. This is where the Raspberry Pi’s will upload new images. It is important that you setup a folder for images (e.g/webcam/1/ and configure a new dedicated FTP account to log into that location by default.
Raspberry Pi
Wifi dongle (TP-link TL-WN821N), 2500ma power supply, Raspberry Pi 8MP Camera.
Other
You should have booted into your Pi and configured it, connected it to your wireless network and obtained it’s Mac network address (use terminal: ifconfig), enabled remote ssh access, changed the default password, set a static IP address on your local LAN/router and configured the date. If you did not want to stratx and use the GUI to setup the Pi you van use raspi-config command.
Previously I had a working security camera using a bash and a python script but this time decided to use Perl (thanks to Marc Fearby already writing a Perl script. and my old python script failing).
Install Packages and Pre-Requisites.
Type the following to install Perl and ImageMagick
sudo apt-get update sudo apt-get dist-upgrade sudo apt-get install -y perl sudo apt-get install -y imagemagick
ImageMagick is used to add images and text on to an image.
Also installed wput (to download an image) and ftp and other tools. You can skip this step if you don’t need Python, local ftp or wput.
sudo apt-get install -y wput sudo apt-get install -y vsftpd sudo apt-get install -y rpi-chromium-mods sudo apt-get install -y python-sense-emu sudo apt-get install -y python3-sense-emu sudo apt-get install -y python-senseemu-doc sudo apt-get install -y python-sense-emu-doc sudo apt-get install -y realvnc-vnc- sudo apt-get install -y python3-picamera sudo apt-get install -y python3-pip
I made a scripts and images folder on the raspberry pi
mkdir /scripts mkdir /scripts/1/ mkdir /scripts/2/ mkdir /scripts/3/ mkdir /scripts/4/
I did set permissions on the local folders
sudo chown -R 777 /scripts/ sudo chown -R pi /scripts/
I made image folders on the remote web server
mkdir ./webcam mkdir ./webcam/1/ mkdir ./webcam/2/ mkdir ./webcam/3/ mkdir ./webcam/4/
I placed an empty index.php in each folder.
A reboot is a good idea here
sudo reboot
Raspberry Pi Script.
Make the sync image Perl script.
sudo nano /scripts/syncimage.pl
Here is my final Perl script. Hopefully, it is self-explanatory and debug information is clear. Printf if debug information and system is where Perl runs a command in the background.
use strict; use Net::FTP; use warnings; use Time::Piece; use Time::Seconds; use Time::Piece; use 5.010; printf "Security Camera: Your Camera Name Here\n"; printf "Getting Timestamp\n"; my $mytimestamp = localtime(time) -> strftime ( "%d-%m-%Y-%H-%M-%S" ); printf "timestamp: $mytimestamp \n"; printf "\nTaking new image.\n"; printf "Saving image to /scripts/1/$mytimestamp.jpg\n"; system("raspistill -vf -hf -o /scripts/1/$mytimestamp.jpg"); printf "Captured: /scripts/1/$mytimestamp.jpg\n"; my $filename = "/scripts/1/$mytimestamp.jpg"; my @stat = stat $filename; printf " Size: $stat[7]\n\n"; printf " Modified: $stat[9]\n\n"; printf "Adding Text to image.\n"; system("/usr/bin/convert /scripts/1/$mytimestamp.jpg -pointsize 36 -fill white -annotate +1+40 'Your Camera Name Here: $mytimestamp' /s$ printf "Done\n"; printf "Adding image to image\n"; system("/usr/bin/convert /scripts/1/$mytimestamp.jpg /scripts/avatar.jpg -geometry +1+1 -composite /scripts/2/$mytimesta$ printf "Done\n\n"; printf "\nUploading /scripts/1/$mytimestamp.jpg to ftp site ftp.yopurservernamehere.com\n"; my $ftp = Net::FTP->new("ftp.yopurservernamehere.com", Debug => 1, Passive => 1) or die "Can't open ftp.yopurservernamehere.com: $@ \n"; printf "\nLogging in to ftp..\n"; $ftp->login("yourloginusernamehere", "yourloginpasswordhere") or die "Cannot login to ftp.yopurservernamehere.com\n", $ftp->message; printf "\nChanging ftp directory to /1/.\n"; $ftp->cwd("/1/") or die "Cannot change directory to /1/ on ftp.fearby.com", $ftp->message; printf "\nSwitching ftp mode to binary.\n"; $ftp->binary(); printf "\nUploading $mytimestamp.jpg to ftp site.\n"; $ftp->put("/scripts/1/$mytimestamp.jpg","/1/$mytimestamp.jpg"); printf "\nDisconnecting from ftp.\n"; $ftp->close(); printf "\nDone.\n"; printf "\nRemoving old captured image: /scripts/1/*.jpg\n"; system("rm /scripts/1/*.jpg"); printf "\nDone..\n";
Multiple Pi’s
You can duplicate this setup on other pic and just change the /1/ path to /2/ or a higher number.
Security
You should have A+ rated SSL as a minimum (guide here). you should not be using the default Pi password.
index.php Viewer on the web server
displaying the latest uploaded image in a web server folder as an HTML image tag. This PHP code chunk can be inseRted into your own PHP files and it will show the latest uploaded image file.
<?php $show = 1; $files = glob( '/home/yourwebserverpath/public_html/webcam/1/*.{jpg}', GLOB_BRACE ); usort( $files, create_function('$b, $a', 'return filemtime( $a ) - filemtime( $b );') ); for ( $i = 0; $i < $show; ++$i ) { $image = $files[$i]; $image2 = str_replace('/home/yourwebserverpath/public_html/', '/', $image); echo "$image2<br />"; echo "<img style=\"outline : none;max-width:100%;max-height:100%;\" src=\"" . $image2 . "\"></a><br><br>"; //display images } ?>
More code will be added to this article on displaying all server site images as [5m][10m][15m][420M] (minutes ago) etc
Automating the syncing of images
Open the terminal and type
sudo contab -e
Enter this into the last line of the crontab and save (this will call the script every 5 mins automatically). Change the 5 to a 1 to have the Perl file called every 1 minute.
*/5 * * * * /usr/bin/perl /scripts/syncimage.pl > /dev/null 2>&1
Manual syncing images
sudo perl /scripts/syncimage.pl
output:
Security Camera: Your Camera Name Here Getting Timestamp timestamp: 29-05-2017-06-19-25 Taking new image. Saving image to /scripts/2/29-05-2017-06-19-25.jpg Captuered: /scripts/2/29-05-2017-06-19-25.jpg Size: 3719361 Modified: epochtimeremoved Adding Text to image. Done Adding image to image Done Uploading /scripts/2/29-05-2017-06-19-25.jpg to ftp site ftp.yourwebservernamehere.com Net::FTP>>> Net::FTP(2.79) Net::FTP>>> Exporter(5.71) Net::FTP>>> Net::Cmd(2.30) Net::FTP>>> IO::Socket::INET(1.35) Net::FTP>>> IO::Socket(1.38) Net::FTP>>> IO::Handle(1.35) Net::FTP=GLOB(0xafaef0)<<< 220---------- Welcome to Pure-FTPd [privsep] [TLS] ---------- Net::FTP=GLOB(0xafaef0)<<< 220-You are user number 1 of 150 allowed. Net::FTP=GLOB(0xafaef0)<<< 220-Local time is now 06:19. Server port: 21. Net::FTP=GLOB(0xafaef0)<<< 220-This is a private system - No anonymous login Net::FTP=GLOB(0xafaef0)<<< 220 You will be disconnected after 15 minutes of inactivity. Logging in to ftp.. Net::FTP=GLOB(0xafaef0)>>> USER yourftpusernamehere Net::FTP=GLOB(0xafaef0)<<< 331 User yourftpusernamehere OK. Password required Net::FTP=GLOB(0xafaef0)>>> PASS .... Net::FTP=GLOB(0xafaef0)<<< 230-OK. Current restricted directory is / Net::FTP=GLOB(0xafaef0)<<< 230 750756 Kbytes used (36%) - authorized: 2048000 Kb Changing ftp directory to /1/. Net::FTP=GLOB(0xafaef0)>>> CWD /1/ Net::FTP=GLOB(0xafaef0)<<< 250 OK. Current directory is /2 Switching ftp mode to binary. Net::FTP=GLOB(0xafaef0)>>> TYPE I Net::FTP=GLOB(0xafaef0)<<< 200 TYPE is now 8-bit binary Uploading 29-05-2017-06-19-25.jpg to ftp site. Net::FTP=GLOB(0xafaef0)>>> PASV Net::FTP=GLOB(0xafaef0)<<< 227 Entering Passive Mode Net::FTP=GLOB(0xafaef0)>>> STOR /2/29-05-2017-06-19-25.jpg Net::FTP=GLOB(0xafaef0)<<< 150 Accepted data connection Net::FTP=GLOB(0xafaef0)<<< 226-754131 Kbytes used (36%) - authorized: 2048000 Kb Net::FTP=GLOB(0xafaef0)<<< 226-File successfully transferred Net::FTP=GLOB(0xafaef0)<<< 226 32.397 seconds (measured here), 104.18 Kbytes per second Disconnecting from ftp. Done. Removing old captured image: /scripts/1/*.jpg
Misc
- Make sure the Ppi has a 2500+ ma power pack minimum
- Ensure the Pi lens of protected from the sun.
- Test
Images
Device Choices
Using a full Raspberry Pi has the benefits of using full-size USD Devices while debugging but it may draw more power (still 5v but more amps/watts).
A Raspberry Pi Zero W is a good choice if you have micro USB devices (I did not and the bits I ordered were not compatible with older Rasbian installations, read my setup guide here). A Raspberry Pi Zeo W is a single core device and used a tiny bit of power.
I would like to setup a camera on a NodeMSU ESP8266 (read my setup guide here ) device or an ESP32 sometime soon for using even less power.
Misc
downloading an image with wget.
sudo /usr/bin/wget http://youwebserver.com/avatar.jpg
Todo:
- Only capture in set hours (exit Perl script)
- Show last images (server)
- Auto remove images older than xx hours (server).
- Local web interface.
- SMS alerts
- Raspberry Pi zero
Donate and make this blog better
Ask a question or recommend an article
[contact-form-7 id=”30″ title=”Ask a Question”]
v1.7 fixed typo, checking guide (Jan 2018)
v1.6 add info on device choices.