Create project Need help?
  • Labs project

Raspberry Pi Home automation

Status: Proposal
June 7, 2015

Home Automation on Raspberry Pi
You have your raspberry pi computer ready and sends you streaming images all over your home. It has motion software installed. You may have more than one camera installed. In case it senses some movement in front of it's vision angle which is above the threshold limit set by you ,it takes a snap and then sends it back to you anywhere in the world. You have the camera setup on a tracking device (small stepper motor) which you control from anywhere of the world. Your Internet connection is on 24*7.
Well, that was all about Raspberry Pi world web cam (see my complete article on Raspberry Pi world web cam)
Now let's imagine with the camera you want to control the garage door , porch light or the window curtains. Practically this is no problem at all. Just extend a few GPIOs up to the switch locations and control them.
But no, I hate cluttering of my house with wirings also neither my wife nor my daughter would allow me to do that. I want to do it free from wires besides I don't have free GPIOs and here comes the brilliant solution to it – 433 MHz. radio link connected to Raspberri Pi and communicating through coded word.
Connect a 433 MHz. Tx to the Raspi and the 433 MHz. Rx to the remote switches. When you want to control a switch , issue a 7 digit codeword from the Tx connected to the Raspi. All the receivers listen and the particular Rx acts on it because the codeword is meant for it. So simple yet very very precise. The usual range with the special miniature antenna (see pic) I get about is 40 meter in the indoor location on 5Volt supply from Raspberry Pi (pin no-2). However on open space I could get 70 mtr. By increasing the input voltage upto 12 Volt to the Tx the range can be increased to 400 mtr open space. One Raspi can control several switches located within the transmission range. For controlling more number of switches just add them in the gpio.php program and create more remote switches on Arduino.
However, for only testing of switches the camera tracking control part can be ignored in the gpio.php program.
A few extra line of code is attached in the weathergsm.php to take temperature data to switch on or switch of the fan very easily !
if($temp>0 && $temp>30)
shell_exec('sudo /home/pi/433Utils/RPi_utils/codesend 7938041');
if($temp>0 && $temp<=29.5)
shell_exec('sudo /home/pi/433Utils/RPi_utils/codesend 7938040');
Schematic: Raspberry Pi end connections (other than the CAM & Tracking motor)
connect the Tx to the Raspi as shown in the picture. The 5V connection is taken from pin no 2 & 6. The data pin of the Tx is connected with the GPIO pin 0 or the physical pin 11.
Arduino remote switch schematic:
You can add as many relays as you have free digital pins available on your arduino. I've added only two relays here using two Digital pins-3 & 4 which are actually physical pins 5 & 6 on ATMEGA328. The 16 MHz crystal is connected on pin no 9 &10 of ATMEGA328 and they are stabilized with a 10pf capacitor each. For a different location build up oa separate remote switch using one more Arduino.
The software is based on combination of wiringpi & 433 MHz Utilities for Raspberry Pi. First installed wiringpi . The guide to wiringpi installation can be found from here.
It is also assumed that your Raspi has Apache and PHP and PHP-GPIO is already installed
Next install 433 MHz. Utilities for Raspberry Pi. Follow this steps.
$> sudo apt-get update
$> sudo apt-get upgrade
$> sudo apt-get install git-core
$> git clone
The last command will create a directory 433Utils. Navigate to that directory. Inside it, navigate to RPi_utils sub directory. Here issue the following command. This will compile the codesend.cpp program.
$/433Utils/RPI_Utils> make codesend
With the error free execution of the last command your Raspi is set for issuing 433 MHz communication commands to the slave Receivers.
$> sudo ./codesend 1234567 //this will send a 1234567 at 433 MHz wave length.
Arduino part :
Connect a 433 MHz. Receiver to an ATMEGA328 as shown in the picture. Keep the connections small and use sugar cube 5V relays as this will go inside the switch section. The receiver data wire is connected to digital pin no 2. Only one 16MHz. Crystal is connected to the ATMEGA328 chip to make it a bare minimum Arduino on board. See the picture of my prototype. The chip is programmed outside using ISCP (see my other article on “Arduino on board” ) . The LED connected to D13 of Arduino will be on when it gets signal from the Raspi. You can easily modify the program to put different color LEDs with actuation of different switches.
Arduino sketch:
#include "RCSwitch.h"
RCSwitch mySwitch = RCSwitch();
long x,y,z;
int pin1=13,pin2=3,pin3=4;
….... …......
Get it from my repository mentioned below this page. The 5V sugar cube relay is connected thru' a BC547 switch to ground and the +5V connected to the other end of the relay. The codeword 7938011 or 7938010 is my configuration for first switch.
7938 -------- is the code word
01 -------- is the 01 switch
1 or 0 -------- 1 is on 0 is off
so 7938021 is the 2nd switch on.
or 7938020 is the 2nd switch off.
7938041 is the 2nd switch on when the temperature is >30
7938040 is the 2nd switch off when the temperature is <=29.5
[this is summer time ; therefore, whenever, temperature goes beyond 30 Degree the fan starts and whenever, temperature reduces below 29.5 the fan becomes off ]
That way all my switches are controlled one by one. The specific codeword is required otherwise the 433 MHz being an open protocol , the switches can get signal from anywhere. Now lets come back to Raspberry Pi where few more issues are still pending to complete the project.
The program codesend needs a superuser permission to execute which is not available at the browser output. Therefore, to issue a codesend command from the browser interface from remote we need to give www-data a superuser permission for executing this command.
And here's the way to achieve it...
Open the sudousers file in /etc/sudousers and then add the following line below it. But sudousers is one of the most sensitive and important file in wheezy linux. Any unintended error in this file may corrupt your entire system that's why it is recommended to edit this file using
$> sudo visudo // this command will open the sudouser file in nano editor.
# add the following line
www-data ALL = (ALL) NOPASSWD: /HOME/PI/433Utils/RP_utils/codesend
Close nano editor by control+x. Save it and relax !
www-data or the browser is now capable to issue codesend command in superuser mode. Create a php program or modify the existing gpio.php program of Raspberry Pi world webcam to have it the additional muscle power of controlling 433 MHz. remote switches laid all over your home.
[The gpio.php program controls everything now ]
Restore the last status:
I've additionally created two text files - sw1.txt and sw2.txt to read / write the last switch status as otherwise this status is not available in the browser. Also in the event of a power failure of the remote switches as soon as the power is restored the remote switch gets the last status command which is taken from these two text files. Other way when a button is pressed the corresponding text file is appended at the bottom. You can modify the PHP program very easily to stamp the date & time into these text files to make it as a database for the switching actions. The browser window needs to be on else this command will not be transmitted from the Raspi.
Remember to change the group of these two files status so that www-data can write into it.
$> chgrp www-data sw1.txt
$> chgrp www-data sw2.txt
The entire project including the Arduino RCSwitch.h header file , Arduino sketch & Raspberry Pi side PHP files is located here.
Power supply:
For Raspberry Pi you need to have a minimum 0.8 Ampere 5 volt supply however, for the remote switches the cheap ubiquitious 5 volt phone charger can work pretty well. Thus eradicating the need for an elaborate 7805 regulator IC combined with diodes and capacitors.
Contents of files:
remote_switche.ino …... Remote side switches based on Arduino UNO
gpio.php …... Raspberry Pi side PHP script to be dropped at server document root directory (usually /var/www/)
http_auth.php …... Authorization file
RCSwitch …... Directory containing RCSwitch files to be put in to Arduino libraries
RCSwitch.h …... RCSwitch.h header file
RCSwitch.cpp …... RCSwitch c+ file
Refresh rate:
The gpio.php page has an auto refresh of 5 seconds. Therefore, if one switching action misses in one cycle in the next 5 seconds it will actuate again.
To avoid inadvertent execution of the switches I've put an authorization file at the beginning of the main program (gpio.php) . The http_auth.php is to reside in the document root directory of your Raspi which is usually /var/www/ . In the http_auth.php directory you can add as many id and password as you like. However, there are more intelligent ways of putting this authorization in use in apache webserver which you may find out on your own.
My Prototype – Raspberry Pi side
My Raspberry Pi Tx - (Range 400 mtr inside house , 750 mtr open space on 5 volt )
My Prototype - Arduino side
My Raspberry Pi controlled remote switches
How it looks from the browser window:
The total cost of the project: Aproximately Rs:12K [$200 ] including the world web cam tracking devices and excluding the Internet connections and modem.
But thing does not end here.......
------------------------ -------------------------- -----------------------------------
02-01-2015: 18:30 Hrs.
That's the time I usually log on to my Raspi from Howrah, Pune, Bankura, Darjeeling and Sikkim wherever we traveled during the year end and then switched on the lights at our porch, front yard and back yard.
Then at morning 5:30 I logged on again and then switched off those lights at our D-type sprawling bungalow at Vindhyanagar.
I could tell our neighbor to open the house and then do that chores during morning and evening while we are out of station but this time we spared them with the help of Raspi.
One morning I thought why not do this routine task by Raspi himself, so I logged on to Raspi again. Created two PHP programs and then put them in crond task scheduler and then restart the crond task.
And everything is on auto now.... even from 1300 km away at Tsangu lake at Sikkim.
first make a php program to send the switchon signal. Very simple indeed
$> sudo nano switchon.php
$myFile1 = "/var/www/sw1.txt";
$myFile2 = "/var/www/sw2.txt";
$myFile3 = "/var/www/sw3.txt";
$file = file($myFile1);
$data1 = $file[count($file) - 1];
$file = file($myFile2);
$data2 = $file[count($file) - 1];
$file = file($myFile3);
$data3 = $file[count($file) - 1];
$f1 = fopen($myFile1, "a");
$f2 = fopen($myFile2, "a");
$f3 = fopen($myFile3, "a");
shell_exec('sudo /home/pi/433Utils/RPi_utils/codesend 7938021');
fwrite($f2, "ON \n");
shell_exec('sudo /home/pi/433Utils/RPi_utils/codesend 7938031');
fwrite($f3, "ON \n");
now put the php file in a shell file
$>sudo nano
php -f /var/www/switchon.php
and then make the shell file executable
$>sudo chmod
Now make a switchoff php program. So simple
$>sudo nano switchoff.php
$myFile1 = "/var/www/sw1.txt";
$myFile2 = "/var/www/sw2.txt";
$myFile3 = "/var/www/sw3.txt";
$file = file($myFile1);
$data1 = $file[count($file) - 1];
$file = file($myFile2);
$data2 = $file[count($file) - 1];
$file = file($myFile3);
$data3 = $file[count($file) - 1];
$f1 = fopen($myFile1, "a");
$f2 = fopen($myFile2, "a");
$f3 = fopen($myFile3, "a");
shell_exec('sudo /home/pi/433Utils/RPi_utils/codesend 7938020');
fwrite($f2, "OFF \n");
shell_exec('sudo /home/pi/433Utils/RPi_utils/codesend 7938030');
fwrite($f3, "OFF \n");
Put the switchoff php in a shell file.
$>sudo nano
php -f /var/www/switchoff.php
and then make the shell file executable
$>sudo chmod
Now the programs are ready to be deployed in crond scheduler. To put them in the crond scheduler I first open the crontab in su mode.
$> sudo crontab -e
And then towards the end of the file I add the following two lines.
#this 4 lines are different crond tasks
0-59/3 * * * * /var/www/ >/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/ >/dev/null 2>&1
*/15 * * * * /var/www/recorder/ ./dev/null 2>&1
#below this line two additional lines are added as our crond task
30 18 * * * /var/www/ >/dev/null 2>&1
30 5 * * * /var/www/ >/dev/null 2>&1
Save the crond task by control +o and then exit nano by pressing x. One small last task remains to complete this adventure now. Restart the crond task....
$> sudo /etc/init.d/cron restart
and the job will be done endlessly like an extremely faithful dog. Every time it actuates , it also updates the text file so that when you open the browser based program - gpio.php it will read these text files and then will let you know the state of the light switches – whether on or off at the remote corner of Pelling at Sikkim.
The physical distance between Raspi and the remote switches can be 800 to 1200 mtr on 433 mhz radios with proper antenna.
S. Bera

Read the full post
Show less
Browser output
Arduino controlled remote switch
Connection on Raspberry Pi
Connection receiver side

Loading comments...