Monthly Archives: March 2015

Raspberry Pi – Simple Garage Door Opener

Hello everyone. Today I want to show you how you can build your own Garage Door Opener which will allow you to open your Garage Door by simply pressing a button on a website. Pretty cool, huh? What we will basically do is setup a computer controlled push switch/button. A pretty good explanation can be found here.

Okay let´s do this. These are the things you will need:

  • A Raspberry Pi Model B (Plus power adapter, SD card and a LAN cable)
  • Raspbian wheezy as Operating System. You can download it from here.
  • A Raspberry Pi Relay (like this one here) or a transistor (It should work exactly the same but I did not try it out) – I recommend to use a relay since they are not very expensive and you can use them to work with higher currents and voltages. If you use a relay for this project you can reuse the same hardware to create an intelligent AC light switch without making any changes to the hardware. All you would need to do would be to change some fragments of the custom.html code from this post.

Hardware setup:

The hardware setup is pretty simple. Take you Raspberry Pi and connect it to you Raspberry Pi relay like this:

  • Connect one 5V Power Pin to the relay´s VCC Pin
  • Connect one Ground Pin to the relay´s GND Pin
  • Connect the GPIO 17 Pin (You can use any GPIO Pin you want but for this project I used GPIO Pin 17) to the relay´s IN1 Pin

RaspberryPi_Garage_Door_Pins

Now connect you relay to your Garage Door Control as if you would add a new push switch/button. Wire the relay like this:

  • Connect one wire from your Garage Door Control to the relay´s COM contact
  • Connect the other wire from your Garage Door Control to the relay´s NO contact

There are some great tutorials out there which explain how to set up electric Garage Door Openers (like this one here) so if you have trouble have a look at them. All Garage Door Openers work pretty much the same.

That´s it. You have finished the Hardware setup.

Software Setup:

Now the Software Setup. This will be easy too.

The first thing you need to do is to update your Raspberry Pi:

sudo apt-get update
sudo apt-get upgrade

Now you need to download WebIOPi:

wget https://webiopi.googlecode.com/files/WebIOPi-0.6.0.tar.gz

Unpack the archive file:

tar xvzf WebIOPi-0.6.0.tar.gz

Change into the newly created directory:

cd WebIOPi-0.6.0

And run the WebIOPi setup: (This will take some time)

sudo ./setup.sh

After the setup has finished you can run WebIOPi by running the following command:

sudo python -m webiopi

Now you should be able to access the WebIOPi web interface by entering the following URL into your Browser: (Default User: webiopi and Default Password: raspberry)

http://<IP-of-your-Raspberry-Pi>:8000/webiopi/

Now end the execution of WebIOPi by hitting Ctrl+C.

Since we want WebIOPi to be always available we need to run the below command to make sure that it will be started at boot:

sudo update-rc.d webiopi defaults

Now reboot your Raspberry Pi:

sudo reboot

If you can access WebIOPi´s web interface after the reboot everything is working correctly.

The next thing I did was to create a directory where I store my custom made code for my project:

sudo mkdir /usr/share/webiopi/htdocs/custom

And here is my code:


<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<meta name="viewport" content = "height = 300, width = 300, user-scalable = no, initial-scale = 1, maximum-scale = 1" />
<title>Garage Door Opener</title>
<script type="text/javascript" src="/webiopi.js"></script>
<script type="text/javascript">

webiopi().ready(function() {
content = $("#content");
});

function setGPIO17()
{
webiopi().setFunction(17,"OUT");
sleep(1000);
webiopi().setFunction(17,"IN");
}

function changeButtonPressed()
{
document.getElementById('PowerButton').src="power_on.jpg";
}

function changeButtonReleased()
{
document.getElementById('PowerButton').src="power_off.jpg";
}

function RunFunctions()
{
changeButtonPressed();
setGPIO17();
setTimeout(changeButtonReleased, 500);
}

function sleep(milliseconds)
{
var start = new Date().getTime();
for (var i = 0; i < 1e7; i++) {
if ((new Date().getTime() - start) > milliseconds)
{
break;
}
}
}

</script>
</head>
<body bgcolor="#191f2f">
<div id="content" align="center">
<font face="verdana" color="white">
<h1>Garage Door Opener</h1>
</font>
<input type="image" style="outline: 0" id="PowerButton" src="power_off.jpg" onclick="RunFunctions();">
</div>
</body>
</html>

So, how does the above code work? Well, it will set GPIO Pin 17 to OUT which will allow the electric current to power our relay. When the relay is powered it will short the contacts COM and NO closing the electric circuit resulting in our Garage Door to either open or close. Next the Javascript code will sleep for 1 second before setting the state of GPIO Pin 17 to IN again. This will result in our relay getting un-powered thus opening the electric circuit between COM and NO and closing the electric circuit between COM and NC.

To access the custom.html page simply enter the following URL into your Browser:

http://<IP-of-your-Raspberry-Pi>:8000/custom/custom.html

Well, that´s it. Now everything is ready to use. Pretty simple isn´t it?

Important:

Please make sure to secure the network you are using to access this control website and make sure to change the user and password of webiopi to make sure that only you can open your Garage Door and to prevent theft. Please take care not to damage your belongings when trying to set this up. I will not take responsibility for any damages or problems when you use these instructions to set this up for yourself. Do this on your own risk. And please try not to get hurt.

Of course you can have my source code as always. You can find it here.

I hope my post was useful for you and I hope to see you again next time 🙂

Sources:

http://www.themagpi.com/issue/issue-9/article/webiopi-raspberry-pi-rest-framework/

http://store.raspberrypi.com/projects/webiopi

http://mark-amos.blogspot.co.at/2013/02/web-connected-outlet-part-6.html

http://nayarweb.com/blog/2014/control-raspberry-pis-gpio-with-rest-qt-webiopi/

http://www.raspberrypi.org/forums/viewtopic.php?f=44&t=45965

https://code.google.com/p/webiopi/wiki/JAVASCRIPT#WebIOPi.setFunction(gpio,_func[,_callback])

http://www.phpied.com/sleep-in-javascript/

https://raspbeerypi.wordpress.com/2014/01/15/webiopi-smartphone-computer-verbindung/ (german)

http://www.instructables.com/id/Raspberry-Pi-Garage-Door-Opener/step5/Wire-the-Circuit-to-the-Pi/

Advertisements
Tagged