The Wayback Machine - https://web.archive.org/web/20150315012649/http://www.codeguru.com/columns/experts/maker-project-creating-a-garage-door-opener.html

Maker Project: Creating a Garage Door Opener

Introduction

Have you ever wanted to control your garage door from your smartphone? By using a basic Arduino Uno, an Ethernet Shield, and a Relay Module, you can open and close your garage door from your smartphone too, from anywhere in the world (granted you have internet access!).

What You'll Need

  • Arduino Uno
  • Arduino Compatible Ethernet Shield
  • Relay Module (I used a SainSmart 4 Relay Module)
  • Web Server (Optional, but recommended)
  • Ethernet connection

Before You Get Started

This tutorial assumes that you have a basic understanding of electronics and the Arduino platform. It also assumes that you have a web server available to host the garage opener interface that is accessible from your smartphone. A web server is option because you can connect directly with the device to control the garage, but this tutorial will be utilizing a web server. You will also need an Ethernet connection in the garage, or a long connection from the garage door motor to your device. Additionally, one port will need to be opened on the firewall on your home network to allow our web page to talk to the Arduino.

How It Will Work

Garage door motors have connections on the back of them to hook up door block sensors and buttons to open the door from inside the garage. Our relay module will be connected to the same connection points as the button used to open the door. The relay is acting like an electronic button; the Arduino will tell it to open only for a second so the garage motor thinks the button was pressed.

The smartphone will load up a web page with a form button. When the button is pressed, the web page will connect with the Arduino and tell it to click the relay for 1 second, allowing the garage door to either open or close.

Arduino and Relays

You should have an Arduino Uno and an Arduino-compatible Ethernet Shield. Connect the Ethernet Shield to the Arduino device; all of the header pins should line up and it should fit snugly on top of the Arduino Uno.

The relay module I am using has 6 pins on it: VCC, Relay 1, Relay 2, Relay 3, Relay 4, and ground. Connect VCC to 5v pin, ground to gnd pin, and relay 1 to pin 2 on the Arduino. No breadboard is necessary for this project because the relay module contains all the components needed to make the relays switch with just logic-level power from the Arduino.

Garage1
Figure 1: A closer look at the relay module

Arduino Code

Here is the full code. I have added comments to explain what is going on. You will need to set a unique IP address and a MAC address. There is a good chance the numbers below are already unique to your network, but you should confirm this before putting this device live on your network.

#include <Ethernet.h>
// NETWORK SETTINGS
//MAC ADDRESS
int port = 84;byte mac[] =
   { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xEE };
// DEVICE IP ADDRESS
byte ip[] = { 192, 168, 1, 241 };

// SET ETHERNET PROPERTIES
EthernetServer server(port);
EthernetClient client;

// VARIABLES
// READSTRING FOR ETHERNET READ
String readString;
int garageDoor = 2;  // RELAY PIN


// ********** START CODE **********


void setup(){
   // SET RELAY PIN AS OUTPUT
   pinMode(garageDoor, OUTPUT);


   // ETHERNET CONNECTION
   Ethernet.begin(mac, ip);
   server.begin();

   //PRINT TO SERIAL
   Serial.begin(9600);
   // PROJECT NAME
   Serial.println("GARAGE CONTROLLER");
   // IP ADDRESS
   Serial.print("IP Address: ");
   // IP ADDRESS
   Serial.print(Ethernet.localIP());
   Serial.print(":");
   Serial.println(port);   // PORT

   // EVERYTHING IS LOADED - WAIT FOR REQUESTS
   Serial.println("Ready for requests...");
   Serial.println();
}

void loop(){
   // CREATE A CLIENT CONNECTION
   EthernetClient client = server.available();
   if (client) {
      while (client.connected()) {
         if (client.available()) {
            char c = client.read();

            // READ CHAR BY CHAR UNTIL FINISHED
            if (readString.length() < 100) {

               // SAVE EACH CHAR TO STRING
               readString += c;
            }

            // WHEN END OF READ IS REACHED, PARSE
            if (c == '\n') {
               // DISPLAY NEW PAGE
               client.println("HTTP/1.1 200 OK");
               client.println("Content-Type:
                  text/html");
               client.println();

               client.println("<HTML>");
               client.println("<HEAD>");
               client.println("<TITLE>Garage
                  Control</TITLE>");
               client.println("</HEAD>");
               client.println("<BODY>");


               // CHECK TO SEE IF GARAGE DOOR REQUEST
               // WAS SENT

               if(readString.indexOf("flashgarage") >0) {
                  // "flashgarage" IS QUERY STRING KEYWARD

                  // SET RELAY PIN TO HIGH
                  digitalWrite(garageDoor, HIGH);
                  delay(500);   // WAIT
                  // SET RELAY PIN TO LOW
                  digitalWrite(garageDoor, LOW);
                  // PRINT TO SERIAL
                  Serial.println("Garage Door Opened.");

               }


               // END PAGE
               client.println("</BODY>");
               client.println("</HTML>");

               delay(1);
               client.stop();

               Serial.println
                  ("-------------------------
                  -------------------------");
               //clearing string for next read
               readString="";

            }
         }
      }
   }
}

Build and upload that code to your Arduino Uno!

Test Your Arduino

Once you have your Arduino code uploaded and connected to the network, try controlling the device manually via a web browser by going to http://192.168.1.241/?flashgarage (the IP should match whatever you chose earlier). Relay 1 should click to close the circuit and click again to open the circuit. Success!

Smartphone Web Page

To control the garage door from your smartphone, a web page will be used with a basic script. The script has a security feature built in to ensure that only the intended users can gain access to the site and control the garage door. The page I created is a basic web page with a single button and it's optimized for mobile web browsing.

Garage2
Figure 2: The controller, as seen on a smartphone

That web page is uploaded on my personal web server. To access that page, I open my browser to http://my.webserver.com/garage.php?securitycode=MYSECURITYCODE. If MYSECURITYCODE is wrong or missing, an error message is shown and nothing will be opened. I have this URL saved on my iPhone home screen with the security code already saved in the URL so I do not need to remember it each time.

Web Page Code

Below is the PHP portion of the web page script. To see the entire script, go to the downloads section and open the PHP document.

session_start();
// Secret code required to open the door
$securitycode = "MYSECURITYCODE";

// URL to Arduino device
$arduino_url =
   "http://MYHOMEIPADDRESS:84/?flashgarage";

// Check to see if button was pressed, security
// code is correct, and random hash is current.
if ($_GET['action'] == 'garagedoor' &&
   $_GET['securitycode']==$securitycode &&
   $_SESSION['random_hash'] == $_GET['hash']) {
   // CURL the Arduino URL
   $ch = curl_init();
   curl_setopt($ch, CURLOPT_URL, $arduino_url);
   curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
   curl_close($ch);
}
// Create random hash to prevent multiple
// submissions on refresh
$random_hash = md5(time().$securitycode.time());
$_SESSION['random_hash'] = $random_hash;

You will need to set a security code as well as your Arduino URL. Depending on what port you chose for your device, you will need to port forward that port on your Firewall to your Arduino device. Google "what's my ip address" to get your home IP address and put that in the preceding code with the port you selected.

Connect Your Garage

Check the back of your garage door motor; there should be several connections on the back, one of which will be a place to connect a push button. Connect one wire from the positive power connection to the relay 1 center connection on the relay board. Connect another wire from the push button connection to the normally open connection of relay 1 on the relay board. Your switch is now complete!

Garage3
Figure 3: Wiring the relay to the garage door opener's motor

Conclusion

Creating a garage door opener with an Arduino is a relatively simple project that will add convenience to your life. Take this project further by adapting it for use with services like IFTTT (If This Than That) so your garage door will open automatically when you arrive home at night!



Related Articles

Downloads

Comments

  • Arduino garage door opener

    Posted by Dragan Nikolic on 02/17/2015 10:15am

    Wow, I've never realised that this could be done and from scratch, you've even compiled a simple app.These guys often pop up (URL REMOVED) when I search for garage remotes, but this is taking it to a whole new level.

    Reply
  • Php code

    Posted by Andrej on 02/13/2015 09:30am

    Hello, very promising project itself would need something like that to complete my project. Where can I find the whole php code? For which version of PHP code is written? greetings

    Reply

Top White Papers and Webcasts

  • Thanks to wide spread cloud hosting and innovations small businesses can meet and exceed the legacy systems of goliath corporations. Explore the freedom to work how you want, with a phone system that will adapt to your evolving needs and actually save you lots of expense—read Get an Enterprise Phone System without High Cost and Complexity. The article clearly illustrates: The only hardware you'll need is phone equipment for advanced voice and fax. How to join all your employees, mobile devices, …

  • Boost productivity, increase uptime, and enhance business agility by standardizing your IT environment.

Most Popular Programming Stories

More for Developers

RSS Feeds

Thanks for your registration, follow us on our social networks to keep up-to-date