Garage Door Status Server Prototype

Do you ever wonder if you remembered to close the garage door? With an Arduino micro controller board, an ethernet shield, network connection and a few basic components, you can make a simple web server that detects the status of your garage door.

There are many home automation projects across the internet where an Arduino is used to open and close a garage door. Today, I’m just interested in the most basic part of garage door control: determining whether or not the garage door is open or closed. We’ll leave controlling the garage door remotely for another time. Also, I’m not ready to start running wires in the garage quite yet, so we’ll just build a table top prototype.

Here’s what you’ll need:

  • Arduino Uno or similar board
  • Arduino Ethernet Shield
  • Solderless breadboard
  • One LED
  • One momentary push button
  • Two 320 OHM resistors
  • A few jumper wires
  • ethernet cable
  • USB cable

Step 1: Building the Circuit

The circuit for our garage door status server prototype is simple. Coming off the the Arduino and Ethernet Shield, there is a circuit for the  LED and a circuit for the momentary push button. These share the negative rail for ground connections. Here’s a photo:

Here are the connections:

  • Ground on the Arduino is connected with a jumper to negative rail of the breadboard
  • Pin 3 on the Arduino is connected with a jumper to the positive end of the LED
  • 320 Ohm resistor connects negative end of the LED to negative rail of the breadboard
  • 5 volt pin on Arduino is connected with a jumper to one end of the momentary push button switch
  • Pin 2 on the Arduino is connected with a jumper to other end of the momentary push button switch
  • 320 Ohm resistor connects the other end of the momentary push button to the negative rail

Step 2: Programming the software

First, copy and paste the following code into a new Arduino sketch:
/*
 Garage Door Protype

 Arduino Uno + Ethernet Shield
 Button or switch on pin 2
 LED on 3
 When button is pressed, LED lights, web server reports door closed

 Based on examples from Arduino.cc
 joesgizmos.wordpress.com
 */
// Libraries to be included
#include <SPI.h>
#include <Ethernet.h>
// pins for LED and Switch
const int LED = 3;
const int SWITCH = 2;
int state = 0; // state of switch, high = closed, low = open
// Enter a MAC address and IP address for your controller below.
// The IP address will be dependent on your local network:
byte mac[] = {0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED};
IPAddress ip(192,168,1, 177); // Static IP needed for webserver
// Initialize the Ethernet server library
// with the IP address and port you want to use 
// (port 80 is default for HTTP):
EthernetServer server(80);
void setup() {
 // Setup input & output pins
 pinMode (LED, OUTPUT);
 pinMode (SWITCH,INPUT);

 // Open serial communications and wait for port to open:
 Serial.begin(9600);
 while (!Serial) {
 ; // wait for serial port to connect. Needed for Leonardo only
 }
// start the Ethernet connection and the server:
 Ethernet.begin(mac, ip);
 server.begin();
 Serial.print("server is at ");
 Serial.println(Ethernet.localIP());
}

void loop() {
 // listen for incoming clients
 EthernetClient client = server.available();
 if (client) {
 Serial.println("new client");
 // an http request ends with a blank line
 boolean currentLineIsBlank = true;
 while (client.connected()) {
 if (client.available()) {
 char c = client.read();
 Serial.write(c);
 // if you've gotten to the end of the line (received a newline
 // character) and the line is blank, the http request has ended,
 // so you can send a reply
 if (c == '\n' && currentLineIsBlank) {
 // send a standard http response header
 client.println("HTTP/1.1 200 OK");
 client.println("Content-Type: text/html");
 client.println("Connnection: close");
 client.println();
 client.println("<!DOCTYPE HTML>");
 client.println("<html>");
 // add a meta refresh tag, so the browser pulls again every 5 seconds:
 client.println("<meta http-equiv=\"refresh\" content=\"5\">");

 // Web info goes here
 client.print("The garage door is ");
 if (state == HIGH){
 client.print ("closed");
 }
 else {
 client.print("open");
 }
 client.println("</html>");
 break;
 }
 if (c == '\n') {
 // you're starting a new line
 currentLineIsBlank = true;
 } 
 else if (c != '\r') {
 // you've gotten a character on the current line
 currentLineIsBlank = false;}
 }
 }
 // give the web browser time to receive the data
 delay(1);
 // close the connection:
 client.stop();
 Serial.println("client disonnected");
 }
 // Check state of switch
 state = digitalRead(SWITCH);
 if (state == HIGH) { 
 // turn on LED
 digitalWrite (LED, HIGH);
 }
 else { // turn off
 digitalWrite(LED, LOW);
 }

}
Next, find the following section of code:
// Enter a MAC address and IP address for your controller below.
// The IP address will be dependent on your local network:
byte mac[] = {0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED};
IPAddress ip(192,168,1, 177); // Static IP needed for web server

For the line beginning with byte mac[] =, put the Ethernet MAC address of your ethernet shield between the curly brackets {}. The MAC address is often found on a sticker on the box or actual ethernet shield. It’s okay to use the default MAC address, if no other device on your network has that exact address. For more about MAC addresses, see: 
http://en.wikipedia.org/wiki/MAC_address
.

For the line beginning with IPAddress, enter an IP address that is open on your network between the parenthesizes. Make sure it’s not an IP address that your DHCP server would give out to another client. 192.168.1.177 should be fine for most simple home networks. For more on IP addresses, see: 
http://en.wikipedia.org/wiki/IP_address
. Remember this IP address for step 3.

Connect the ethernet cable or your network and the USB cable to your computer’s USB port. Upload the sketch.

The device is now properly programmed!

Step 3: Operation

To operate our Garage Door Status Server, navigate your web browser to the IP address you specified earlier. The default is 192.168.1.177.  Here’s what you should see:
This web page shows you the status of your garage door. It defaults to stating that the garage door is open. It also automatically reloads every five seconds and checks the status of the door. Watch what happens when you press and hold the button.
The web server now reports the garage door as being closed and the LED on the breadboard lights up. Notice you have to keep pressing down the button for it to report as closed.
Another item to note is that this web page is only available within your local network. It will take some work with port forwarding on your router to make it available publicly. We’ll leave that for a another post.
And there you have it!
To take this beyond the prototype form, I plan on using a magnetic reed switch to determine if the door is open or closed. It should be as simple as replacing the momentary push button switch with a magnetic reed switch.
Let me know in the comments if this worked for you or if you have any suggestions for improvements.
Tagged , ,

One thought on “Garage Door Status Server Prototype

  1. [...] For more information and to get started with your own Internet-connected project, visit Joe’s site here.  [...]

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Connecting to %s

Follow

Get every new post delivered to your Inbox.

Join 264 other followers

%d bloggers like this: