Aug 28

Arduino Controlled Garage Door – Part 2

In part 1 of this series I showed how I created an I2C relay module to open and close my garage doors. Now that I have the ability to do this, the next step is to make the user interface for this accessible from a laptop, smart phone, etc. To do this we will create a simple Arduino Ethernet based web page with a button for each door.

The libraries to run an Arduino Ethernet or Ethernet shield are included in the Arduino IDE. The sample sketches include the ability to tweet, telnet, and create a webpage. The example for a web server is fairly simple, however for projects that require more sophistication there is the Webduino library that adds the ability to use AJAX buttons, sliders, and login authentication on websites directly on an Arduino. Because this will eventually be a more complex system this is the method we’ll use in this project.

Materials

  • Arduino Ethernet or Ethernet shield (must be Wiznet-based)
  • relay control module
  • Webduino library

Installing Webduino

Before we can get started we need to install the Webduino library into the Arduino IDE.

  • Download the latest version from https://github.com/sirleech/Webduino/zipball/master
  • Create a “libraries” folder inside your Arduino Sketch folder
  • Extract the zip file to the libraries folder you just created.
  • Open Arduino program and paste the code below
  • Upload the below code to Arduino

The below code is a modified version of the buzzer sketch. It creates 2 buttons, one for each garage door. In this example the web interface can be reached at http://192.168.1.20/garage (this may need to be modified for your network).

// Garage Control Code
// Fork Robotics 2012
// Based on Webduino buzzer sketch

#include "SPI.h"
#include "Ethernet.h"
#include "WebServer.h"
#include

//randomly generated MAC http://sqa.fyicenter.com/Online_Test_Tools/Test_MAC_Address_Generator.php
static uint8_t mac[] = { 0x7C, 0xD5, 0x93, 0xAE, 0xB2, 0x76 };
static uint8_t ip[] = { 192, 168, 1, 20 };

#define PREFIX "/garage"
WebServer webserver(PREFIX, 80);

int garageToggle=0;
int ATtinyAddress=0x26;

void garageCmd(WebServer &server, WebServer::ConnectionType type, char *url_tail, bool tail_complete)
{
  if (type == WebServer::POST)
  {
    bool repeat;
    char name[16], value[16];
    do
    {
      repeat = server.readPOSTparam(name, 16, value, 16);
      if (strcmp(name, "garage") == 0)
      {
        garageToggle = strtoul(value, NULL, 10);
      }
    } while (repeat);
    server.httpSeeOther(PREFIX);
    return;
  }

  server.httpSuccess();
  if (type == WebServer::GET)
  {
    /* store the HTML in program memory using the P macro */
    P(message) =
      "Webduino Garage Example"
""
 "</pre>
<h1>Pick a Door</h1>
<pre>
"
 "</pre>
<form action="/garage" method="POST">"
 "
<button name="garage" value="1">Left Door</button>

"
 "
<button name="garage" value="2">Right Door</button>

"
 "</form>
<pre>
";
    server.printP(message);
  }
}

void setup()
{
  Wire.begin();
  Ethernet.begin(mac, ip);
  webserver.setDefaultCommand(&garageCmd);
  webserver.begin();
}

void loop()
{
  webserver.processConnection();
  if (garageToggle > 0){
    RelaySwitch(ATtinyAddress,garageToggle);
    garageToggle=0;
  }
}

void RelaySwitch(byte device,byte data) {
  Wire.beginTransmission(device);
  Wire.write(data);
  Wire.endTransmission();
}

The Result

The result of this code is a simple webpage with 2 buttons. When a button is clicked it will send the appropriate I2C command to the module created in part 1.

Next steps

While the Webduino library makes great strides towards making the Arduino Webserver more useable it is still limited in what it can do. I just recently got my hands on a Raspberry Pi; it has I2C, integrated Ethernet, a real time OS and Apache. Eventually the main control mechanism could be a switched over to this platform but for the moment the next step is to install this into my house.