The broadband Internet that you use in your house for 24*7 might be costing you around $9 to $35 per month , add another $150 (one time ) to it and you have your own home surveillance system which will monitor your home 24*7 from a strategic point inside your home which you can watch 24*7 from Internet while you are away from your home. What more ? in case it senses something unnatural, it will capture a snap and then sends it all the way to your email. You can even order it to serve some older snaps or you can pan , tilt , zoom or swipe the direction of the cam. All from a remote location even from the other side of the world !
Raspberry Pi, USB web cam, wires, Stepper motor, fixtures . The Pi cam that is available with Raspberry Pi computer can also be used but it's a bit costlier now so I'm sticking to my old & cheap USB camera.
Raspberry Pi computer running a Wheezy Linux, a GMAIL account for sending / receiving email for Raspberry Pi , another email like your personal email where while you are on move would like to receive emails. 16 GB or bigger SD card – the bigger the better, VLC player (typically for Windows client for viewing world web cam), administrative rights to tweak your broadband modem – I hope it's your home so you have every right to tweak it, and a few open source softwares that are freely available in Internet and couple of small PHP scripts by me and an inquisitive mind to explore the world... (lol )
do an update and upgrade of your Raspi.
$sudo apt-get update
$sudo apt-get upgrade
I hope the USB webcam is working alright at this point. To check usb webcam install fswebcam and then see the output.
$> sudo apt-get install fswebcam // this will install webcam
$> fswebcam -d /dev/video0 -r 320x240 test.jpg
Normally the usb webcam will be found at /dev/video0 or /dev/video1
The last command will take a snap shot and save it in the test.jpg file. Check that file for status of your webcam.
Now install these additional softwares on your Raspi.
$sudo apt-get install apache2
$sudo apt-get install php5 php5-dev php5-imap
$sudo apt-get install motion
Install class.phpmailer.php: This is used for sending email using PHP.
Copy the following php mailer library file in the web directory of your Raspi . It's normally in the /var/www/ directory.
$> sudo cp class.phpmailer.php /var/www/
'class.phpmailer.php' //this file is bundled in the source directory
After installing all these softwares, lets start motion software. Ensure the webcam is inserted in the USB. You may use a powered USB hub in case you run short of USB ports. However, I always run Raspi in headless mode therefore, I use only one USB wifi adapter and the other USB port is always free for the USB webcam.
Now you need to edit the Motion configuration file to make it work, so enter the following code in the terminal.
$sudo nano /etc/motion/motion.conf
The Motion configuration file will appear, and you can change a few settings here if you like. Most importantly though, we need to change the following:
daemon OFF(to ON – this can be changed near the top of the file). Save it and exit nano editor and open the next file .
sudo nano /etc/default/motion
change start_motion_daemon=NO to start_motion_daemon=yes .
Now we need to start the Motion server:
$sudo service motion start ----- Wait for a minute and then navigate to your Raspberry Pi's IP address at 8081 port. Something like this.
8081 and beyond a shade of doubt you will find your Raspi webcam is churning out streaming pictures.
Check email every 3 minutes:
mailonce.php is our program which checks the GMAIL every 3 minute and if there's a query for IP address or the snapshot file there , it will send reply email with the IP address or the snapshot file to the sender. For starting our PHP program that will be checking & sending emails at the boot we add it at the crontab level like this. Crontab is a user level program so to start it at the super user level go to the su mode and then at su prompt add it in the crontab.
#crontab -e //this will open the crontab of su. Go to the bottom and add the following line
0-59/3 * * * * /var/www/mailonce.sh >/dev/null 2>&1
------ Save it and exit nano.
Prepare the mailonce.sh file now.
$sudo nano /var/www/mailonce.sh
php -f /var/www/mailonce.php
------ save it and exit -----
$sudo nano mailonce.php
[mailonce.php ,mailonce.sh is bundled in the source folder ]
To make the mailonce.sh file executable do the following.
$sudo chmod +x mailonce.sh
---- that's all for the mailonce program now. You may reboot your Raspi now or can restart the cron job by the following command.
$sudo /etc/init.d/cron restart
----- ----- -----
Well our first part ends here. Next we will ask Motion to fire up an email the moment it senses some motion in front of the lens. The technique that is used in Motion software to sense motion is the changes in number of pixels. Most simple yet very effective. One good way to understand Motion software is to read the motion.conf file from top to bottom. It's all written in simple way.
$sudo nano /etc/motion/motion.conf
find 'threshold' under Motion Detection Settings
the default value is 1500 but for our use make it 6000 so that the line looks like
Also at this point change the webcam port to 5678 as 8081 is very common port.
Now find 'on_picture_save' line and write in front of it so that the whole line looks like this.
on_picture_save echo 'Alert' | echo %f > /tmp/motion/img.txt | php -f /var/www/sendmail1.php
Well what it does is simple – when the number of pixel-change reaches >=6000 it will take snapshot, write the name of the picture file into /tmp/motion/image.txt and then execute sendmail1.php to send a mail to you ,using a google GMAIL account. That email also contains the URL of the streaming video using the port forwarding through the plain DSL broadband modem for your home Internet. [
Instead of snap shot you can also tell it to capture video for a set time ]
Sometimes while port forwarding (that we will talk about later) the external IP address changes. Therefore, while sending the email your Raspi will also take care of the external IP address where the streaming image will appear.
So far this is so simple. We have our mail checking PHP program mailonce.sh will be running at boot and at background to monitor Inbox of this GMAIL . While you on move seeing the email from Raspi simply returns back the same email to Raspi. The moment it gets an email from you with a subject line referring to a picture, it will pick out that file from /tmp/motion/ directory of Raspi and then returns back to you as an attachment, at the same time Raspi marks the mail as read else it will keep churning all the time.
Now what to do with all the GPIOs which we have not touched yet ? Well, they can be used for more greater purpose now. In the gpio.php we will control some GPIOs to pan our world web cam.
The PHP-imap can be configured such a way that it will purse only those emails that come from a particular email. Then search their subject line for a particular code and when it finds, it will switch off some garden lights (you know birds don't like lights !) or switch on a backyard light or open a grain box which will pour out some beans for those cute little birds. I've made provision for reading the sender's email but I've not used it (look in mailread.php) for any discrimination on the part of the sender.
In case your Raspi reboots then the port forwarding in the Modem will be dissociated and you have to associate those rules again. Open modem page and see figure-2. This happens only when the Raspi is awarded a new IP address by the DHCP.
Many times I find the streaming page was not showing besides no email from Raspi so genuinely I start doubting Raspi but actually what happened was that the external IP address was changed while there was no movement in front of the cam . So no mail to know it's external IP address. In that case open your office email and reply back one email to Raspi with a subject line as /tmp/motion/ . The return email body will contain the external IP address of Raspi – something like blowing one's own trumpet !!!
Also GMAIL does not allow to send more than 2000 emails per 24 hours . Therefore, keep the threshold limit in motion config file to a higher figures. I've made it 6000 which is barely 7.8% of 320*240 pixels else Raspi will send email every now and then and you will be blocked by GMAIL very soon. However, after 24 hours it will be able to send emails again.
Stepper motor: Servo or stepper motors are very powerful and requires more power to run. Normally weak energy from the GPIOs (which is at the level of 3.3 volt ) is boosted to the higher level by a Darlington or H-bridge package. However, for a small 5Volt stepper motor the output from the GPIOs can directly run it comfortably to pan our small light weight USB webcamera. So in the pan circuit we just put the GPIOs power into the stepper motor.
Ensure that the 5volt DC supply has 1 amp or more power. The bipolar stepper motors has minimum 4 wires or maximum 6 wires. When the central connections are joined together , forms the 5th wire or separated out to form the 5th and the 6th wire. In some low powered motors the central connections is completely ignored and only 4 wires are used. If there is a 5th or 5th or 6th wired together they are to be joined together and would go to 5Volt + . But at the same time one can jolly well ignore it and left unconnected (like my case).
[In case your stepper motor requires more power, put a darlington IC (ULN2003 for normal stepper ) or a double H-Bridge IC (L298 for bi-polar stepper ) in between the GPIOs and the stepper motor connections.
Raspi world webcam and social networking: Our mailonce.php program is so intelligent that if an email with subject as “/tmp/motion/” is sent to email@example.com it will in next 3 minutes reply back the external IP address of itself and the control URL to the sender so that he / she can see the streaming image at Internet. Therefore, you can jolly well publish your world webcam for the whole world at your social networking site. Because the whole thing comes from your Raspberry Pi computer only.
Testing of Motion:
Open your email where Raspi is supposed to send the emails. You may not find any email right now. Do some caricature in front of your Raspi cam and then check your Inbox. Raspi has sent a burst of small emails to you now. Select any one and reply back to Raspi. Wait a few seconds and see a new email has arrived with an attachment. That's the picture Raspi captured at that moment !
[Remember to enter the GMAIL detail that the Raspi will be using in the following php programs – mailonce.php, mailattach.php, mailread.php, sendmail1.php & ipaddchange.php ]
---- ---- ----- -----
Port Forwarding: When I asked help from our IT man, he said that he never tried it because he never required it. The problem with these IT guys is that they always come up with 'why' even before answering 'how'. So discounting any wise help from him ,I rolled out my sleeves and opened my Modem's home page. It's a GLB-502T Dlink DSL modem. The BSNL Internet comes from the telephone line which has entered into one side of the Modem while the other side has gone back to the Telephone set. The wifi unit is a separate Iball unit connected to the Modem by an ethernet cable. For almost all home type ,the DSL modem opens at http://192.168.1.1 the user name is 'admin' and password is 'admin'
However, the present wifi setup comes in one unit – modem+wifi but surprisingly the features are all the same.
So, Logged in and then open at the 'Advanced' section.
Click on 'Port Forwarding' section from left.
Here you will find the 'Wan connection' being used for Internet. For my case it's named as 'bsnl'. The LAN group is 'LAN group 1'. The computers connected is shown in the 'LAN IP:' pull down menu. In my case my laptop is 192.168.1.3 while the Raspi is 192.168.1.2. so I select Raspi (I.e 192.168.1.2) from the pull down then I select the 'user' radio button at the left. The 'Available Rules' was originally empty so I click the 'New' button at the bottom to create a new rule for my Port Forwarding.
[ for knowing IP address of your Raspi or Laptop just run ifconfig in a terminal ]
Give the rule a name – and Port detail.
Motioncam , Protocol – TCP, Port start - 5678, Port End – 5678, Port Map – 5678.
Webiopi , Protocol – TCP, Port start - 5679, Port End – 5679, Port Map – 5679.
Webpage, Protocol – TCP, Port start - 80, Port End – 80, Port Map – 80.
… Like that you can add more rules for more services.
Finally after adding all the desired services go to the bottom and press the apply button. To save the set up into the modem go to Tools – System Commands – Save all
Well that's the setup for my modem ,for other kind of Modems this is more or less same. One kind of Modem asks for an external port which will be connected to the internal Port , like 3456 external port is connected to 6789 internal port (for that matter here 5678 external port is connected to 5678 internal port or 8000 to 8000 - simple ) like this how configuration works in some kind of Modems.
Port-forwarding on old mobile phones:
In case you are using mobile broadband for Internet connection on a moderately 3 / 4 years old mobile phone,it may so happen that your old mobile phone modem has the port-forwarding in built in it. Once you use that phone to be on line it will automatically forward your IP address. You need not to do anything further on it. Just install motion and webserver and it will start working all-at-once. For that read my other blog at
Having done all this you can be sure that your Raspi web cam is available for the world now.
Before that you have to know your Modems external IP address. This can be known from Modem Status – Connection Status.
Alternatively this can be known from some websites like:
Point your browser to that URL and that URL when opens ,will tell you what is your external ip address. But our Raspi will use PHP ( ipaddy.php ) internally to know the external IP address and then in an email to you, it will write the same.
Now go to a separate Internet connection and in the browser copy paste that URL
[the email from Raspi will contain the following lines]
http://220.127.116.11:5678 for Raspi Cam
http://18.104.22.168/gpio.php for Raspi cam control
and then be amazed to see the Raspi Cam streaming the images. [Be careful to hold for support else you may fall from the chair due to over excitement !!!]
Oh no nothing happens !
Ensure that you are trying that URL from a different Internet connection. May be your mobile Internet can be a good testing ground. If it's Windows 7 or later version use it in VLC player. Media – Open Network Stream and then copy paste this URL. For Linux , Unix or Mac open the URL in firefox, safari or any other browser and it will work. For android phone or older Windows computer try with firefox browser.
Testing of Web cam control:
Again PHP comes handy for this. http://your-external-ip-address/gpio.php will open the Web cam control window. The user / Password is pi / pi or whatever you entered in the associated http_auth.php file. Press the control buttons and see the web cam pans from left to right or oscillate non-stop. At the extreme position the 'limit reached' indication appears by the help of another onboard GPIO. For controlling the GPIOS we use the powerful wiringpi shell commands in PHP. For that you need to have wiringpi installed on Raspi.
It's recommended to copy all the program files in your web document root directory. In apache2 it is usually /var/www/ directory.
Wiringpi & PHP-GPIO (The power of GPIOs together):
Two varieties of wiringpi – The C model and PHP model comes vary handy for Raspberry Pi computer. The wiringpi for PHP model is used to run the stepper motor in the camstepper.php program and the wiringpi for C model is used in camcontrol program in the gpio.php program. However, here we have used only some shell commands (gpio) of the C model by php_exec() commands of PHP. Both are to be installed for this program.
PHP-GPIO is the other useful library for manipulating the GPIOs of Raspberry Pi computer. It's a simple GPIO.php file , just copy it in your /var/www directory taking from the resources.
For installing the wiringpi C model here's the link https://projects.drogon.net/raspberry-pi/wiringpi/download-and-install/ follow the simple steps mentioned there.
For installing the wiringpi for PHP model follow the next few lines.
Use git to get the software bundle...
$>sudo apt-get install git
git clone https://github.com/WiringPi/WiringPi-PHP.gt
git submodule update -init
cp wiringpi.php /var/ww/
Now wiringpi is installed in your Raspi also it got copied to the /var/www directory but to get it inside PHP we need to load it in the ini file.
Open a wiringpi.ini file in /etc/php5/conf.d/ and write the following lines in it . Save it and restart apache server.
sudo nano /etc/php5/conf.d/wiringpi.ini
Once the wiringpi is installed you can control it by simple command line controls which we will do it in PHP using shell_exec command of PHP – So simple and it does flawlessly in PHP.
If you study the camstepper.php program you would find that the entire pan of the cam is controlled by just three GPIOs. These are GPIO-18, 21 & 25 .Any other GPIOs can also be considered. Funny, nothing is actually connected to them only internally they are influencing the stepper motor.
For GPIO-18 & 21
0 0 ------ that's no motion
1 0 ------ clockwise motion of cam
0 1 ------- anti clockwise motion of cam
1 1 ------- oscillating motion of cam.
The other GPIO-25 is used for indication of end limit. When one extreme end is reached the GPIO-25 changes state Just follow the camstepper.php program and it will be understood clearly.
Sending email & IP address.
--- get it from the source ----
Determining external IP address:
--- get it from the source ----
Monitoring Inbox, Attaching picture file & Sending email to world
--- get it from the source ------
Sending mail attachment
---- get it from the source -----
---- get it from the source -----
To determine IP address is changed by the Modem
---- get it from the source -----
Cam on Stepper motor - Complete control of camera movement by GPIOs.
The last part of this project is to place the camera on a tracking device which will track the view depending upon the remote command into the GPIOs. The camera can make, say 75 Degree clockwise , 75 Degree anticlockwise or 150 Degree oscillation on receiving command from remote. For this purpose I make a pedestal on my Stepper motor and then connect the stepper motor with the 4 GPIOs on the Raspberry Pi computer – they are the physical pins 16, 18, 24, 26. Just connect these 4 wires to the 4 pins of the bipolar stepper motor. If there are two central pins just leave them open and unconnected.
Here's the schematic diagram.
The two coils – M1 & M2 are connected to physical pin 16,18,24 & 26
The PHP script to swipe the front view is like this.
--- get it from the source ------
Cam on stepper motor: We will simply add a task at crontab level to start the camstepper.php at boot.
Crontab is a user specific facility. For adding additional cron tasks on your Raspberry Pi computer for a particular user . Go to that user terminal and then issue the following command .
$ crontab -e
for adding a task at super user level. Do a su and then issue the same crontab -e command.
$ sudo su
# crontab -e
---- now go to the bottom of the file and add the following lines.
0-59/3 * * * * /var/www/mailonce.sh >/dev/null 2>&1
@reboot php -f /var/www/camstepper.php &
01 01,03,05,07,09,11,13,15,17,19,21,23 * * * /var/www/ipaddchange.sh >/dev/null 2>&1
------ Save it and exit the editor.
Now do a reboot and place the cam on the pedestal atop the stepper motor like my prototype (figure-1 ).
[explanation of the above three cron commands:
The first command will check incoming email once in every 3 minutes and act accordingly.
The second command will start camstepper.php command at every boot.
The third command will check for change in external IP address in every hour and then if there is a change ,it will issue an email to you telling the same.
----- ------- -------- --------- --------- -------- -------
Lets summarize a little:
So we have now set the motion software running background, the Raspi 5678, 80 port is forwarded to the external world, the sendmail1.php program sends email to me the moment it senses motion on the front view , the mailread.php checks email and acts every 3 minutes. The ipaddy.php program determines the external IP address for the Raspi for the external world & ipaddchange.sh sends the new ipaddress to my email address, the mailonce.sh sends email and mailattach.php program sends the snapshot when a particular coded mail is sent to Raspi. Finally all.php puts everything in one browser window.
Wah ! It's a good amount of coding I've done this time. Now lets come to the last php program for controlling the cam motion from outside.
Well, earlier I thought to do it by webiopi but since I could not isolate two GPIOs in webiopi for my work, I shifted to plain old PHP scripting. Here's the last code to control the cam.
First have a look at this...
It's a single browser windows split into two. The big one on the left is the cam view on 5678 port while the small one on the right is the control window which controls the stepper motor connected to the Raspi and the Cam is sitting on a pedestal fitted on the stepper shaft. The buttons are self explanatory – on the press of each button the cam will pan the horizon.
Since we don't want every tom-dick-harry to control our cam ,we have included a small authorization program http_auth.php with the gpio.php so that every time you open the gpio.php program you have to supply the id and password (pi and pi here – read the http_auth.php and you will understand the id and passwords...)
Modem of some kinds does not allow to forward port 8080. In that case set it to port 8081 and in /etc/apache2/ports.conf make one more entry for 8081. So what comes now on port 80 will also be available at 8081. Very simple !
---- part of /etc/apache2/ports.conf file ------
----- ----- ------
Create a suitable rule in the port forwarding rules of the Modem configuration .
Press apply and save the modem commands.
Now open Internet from a separate connection – like your mobile and send an email to Raspi with subject /tmp/motion/ , in less than 3 minutes you will get the return email about the external IP address of the Raspi world cam and then follow that link and enjoy your cam view.
Open the control window at http://Raspi-external-address/all.php and then press those buttons to control your cam.
Download link: https://drive.google.com/
That's all folks,