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
/* 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); } }
// 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!



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