Friday, June 21, 2013

Control Your AirCon from a Raspberry Pi

  
Having an Aircon is good but the time it takes to cool down your apartment is frustrating!!! solution is to turn it on before you arrive right? let the PI do the job for you. there are two ways
  • you can assign a cronjob task to turn on the ac at a specific time of the day
  • you can run a php server giving control to the outside world etc android phone or a browser.
things you need
  • relay board,cables,insulating tape and casings.
lets hack the controls.

and make sure to get a photo before you remove the control from the wall. here's mine


raspberry pi connections are as follows

   
Now lets do some programming.

I'm not going to cover the basics here. you should first setup your raspberry pi with  raspbian OS.
to setup apache webserver with php type

sudo apt-get install apache2 php5 libapache2-mod-php5

if you get an installer error run

sudo groupadd www-data
sudo usermod -g www-data www-data

now to restart the service type

sudo service apache2 restart

after it finishes we can install WiringPi

cd /tmp
wget http://project-downloads.drogon.net/files/wiringPi.tgz
tar xfz wiringPi.tgz
cd wiringPi/wiringPi
make
sudo make install
cd ../gpio
make
sudo make install

lets give the pi a static ip from the router. remember to give an ip that is outside the DHCP range.
see this awesome video from YouTube. (all credit goes to Raspberry Pi for Beginners Author)


and we are done. lets control the power button of the ac via a simple web page.

create a web page in php with 2 simple buttons and save it as sam.php

<html>
<form action="sam.php?message=ON"  method="post">
<input type="submit" style="width:400px; height:180px; font-size:250%; margin-left:35%;"  value="AC ON">
</form>
<br/>

<form action="sam.php?message=OFF"  method="post">
<input type="submit" style="width:400px; height:180px; font-size:250%;  margin-left:35%;" value="AC OFF">
</form>
</body></html>


<?php
shell_exec("/usr/local/bin/gpio -g mode 18 out");
$name = $_GET["message"];


function bulbon(){

shell_exec("/usr/local/bin/gpio -g write 18 0");
usleep(250000);
shell_exec("/usr/local/bin/gpio -g write 18 1");
WriteName();
WriteDate();
}

function bulboff(){

shell_exec("/usr/local/bin/gpio -g write 18 0");
usleep(250000);
shell_exec("/usr/local/bin/gpio -g write 18 1");
usleep(250000);
shell_exec("/usr/local/bin/gpio -g write 18 0");
usleep(250000);
shell_exec("/usr/local/bin/gpio -g write 18 1");
WriteName();
WriteDate();
}

switch($name) {
case 'OFF':
bulboff();
break;
case 'ON':
bulbon();
break;
}
?>


log in to your pi using WinScp and put this file into var/www/ directory.
type the ip that you assigned in your browser and control the AC :)

if you want to control it from outside your house just forward port 80 and use  NOIP service





3 comments: