Sunday, September 18, 2011

Open & Close Garage Door with your Netduino plus

There are many ways to control your garage door with your Netduino. I decided to hack a spare garage door opener (since I wasn't using it) with an Optoisolator. My total cost $1.25. If I didn't have a spare garage door opener I would have run two wires off the Optoisolator pin 3 & 4 to the control panel in the garage. To send commands to the Netduino I am going to use HTTP commands.

Supplies

  • Garage door opener
  • Optoisolator with Darlington Driver - 1 Channel. I purchased mine from Sparkfun.
  • 100 ohm resistor
  • 33 ohm resistor
  • Netduino plus
1. Build it

Here is the schematic for interfacing with the garage door opener. Excuse my unsophisticated schematic as I don't own any electrical CAD software.

  • Connect the Optoisolator pin 1 (Anode) to the Netduino plus digital pin 13
  • Connect the Optoisolator pin 2 (Cathode) to a ground pin on the Netduino plus with a 33 ohm resistor in-line.
  • Connect the Optoisolator pin 3 (Emitter) to one side of the garage door opener push-button with a 100 ohm resistor in-line.
  • Connect the Optoisolator pin 4 (Collector) to the other side of the garge door opener push-button.
Here is mine all built. You may notice mine has is wired for two garages although I only have one hooked up to the Netduino for this tutorial.

2. Code it

For the code, I am going to start with a stubbed out web server code from my previous blog post. To get more information on the basic code for a web server see my previous blog post.
If you have not installed all the necessary software see my previous blog post on how to setup your Netduino Plus development environment here (How to Setup Netduino Plus Development Environment).

After you download the code, open the project. Let's modify the code so we can send an HTTP request to the Netduino to activate the garage door opener. We are going to send a command to push the button on the garage door opener. We don't know if we are opening it or closing it since we are not yet monitoring the garage door status. We will combine this project to activate the garage door with the monitor code in a later post. Then we will be able to send the specific command "Open" or "Close".

Make the following changes to code
  • Line 6: Initialize digital port 13 to talk to the garage door opener
  • Lines 38-48: Parse the http request and confirm the user requested to door be activated with the command "HTTP:/192.XXX.X.XXX/activatedoor"
  • Line 49: Call our method to activate the garage door opener.
  • Lines 51-55: Send HTTP response.
  • Lines 56-62: send a HTTP response - command not recognized.
  • Lines 68-75: activate garage door method
    • Line 70: light the on-board LED for visual confirmation command is being sent to garage door opener
    • Line 71: Send voltage to the digital pin 13 to close the push-button on the garage door opener
    • Line 72: Wait for 1 second
    • Line 73: Turn off on-board LED
    • Line 74: Stop voltage to pin 13


public class WebServer : IDisposable
    {
        private Socket socket = null;
        //open connection to onbaord led so we can blink it with every request
        private OutputPort led = new OutputPort(Pins.ONBOARD_LED, false);
        private OutputPort Garage2CarOpener = new OutputPort(Pins.GPIO_PIN_D13, false);

        public WebServer()
        {
            //Initialize Socket class
            socket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
            //Request and bind to an IP from DHCP server
            socket.Bind(new IPEndPoint(IPAddress.Any, 80));
            //Debug print our IP address
            Debug.Print(Microsoft.SPOT.Net.NetworkInformation.NetworkInterface.GetAllNetworkInterfaces()[0].IPAddress);
            //Start listen for web requests
            socket.Listen(10);
            ListenForRequest();
        }

        public void ListenForRequest()
        {
            while (true)
            {
                using (Socket clientSocket = socket.Accept())
                {
                    //Get clients IP
                    IPEndPoint clientIP = clientSocket.RemoteEndPoint as IPEndPoint;
                    EndPoint clientEndPoint = clientSocket.RemoteEndPoint;
                    //int byteCount = cSocket.Available;
                    int bytesReceived = clientSocket.Available;
                    if (bytesReceived > 0)
                    {
                        //Get request
                        byte[] buffer = new byte[bytesReceived];
                        int byteCount = clientSocket.Receive(buffer, bytesReceived, SocketFlags.None);
                        string request = new string(Encoding.UTF8.GetChars(buffer));
                        string firstLine = request.Substring(0, request.IndexOf('\n')); //Example "GET /activatedoor HTTP/1.1"
                        string[] words = firstLine.Split(' ');  //Split line into words
                        string command = string.Empty;
                        if( words.Length > 2)
                        {
                            string method = words[0]; //First word should be GET
                            command = words[1].TrimStart('/'); //Second word is our command - remove the forward slash
                        }
                        switch (command.ToLower())
                        {
                            case "activatedoor":
                                ActivateGarageDoor();
                                //Compose a response
                                string response = "I just opened or closed the garage!";
                                string header = "HTTP/1.0 200 OK\r\nContent-Type: text; charset=utf-8\r\nContent-Length: " + response.Length.ToString() + "\r\nConnection: close\r\n\r\n";
                                clientSocket.Send(Encoding.UTF8.GetBytes(header), header.Length, SocketFlags.None);
                                clientSocket.Send(Encoding.UTF8.GetBytes(response), response.Length, SocketFlags.None);
                                break;
                            default:
                                //Did not recognize command
                                response = "Bad command";
                                header = "HTTP/1.0 200 OK\r\nContent-Type: text; charset=utf-8\r\nContent-Length: " + response.Length.ToString() + "\r\nConnection: close\r\n\r\n";
                                clientSocket.Send(Encoding.UTF8.GetBytes(header), header.Length, SocketFlags.None);
                                clientSocket.Send(Encoding.UTF8.GetBytes(response), response.Length, SocketFlags.None);
                                break;
                        }
                    }
                }
            }
        }
        private void ActivateGarageDoor()
        {
            led.Write(true);                //Light on-board LED for visual cue
            Garage2CarOpener.Write(true);   //"Push" garage door button
            Thread.Sleep(1000);             //For 1 second
            led.Write(false);               //Turn off on-board LED
            Garage2CarOpener.Write(false);  //Turn off garage door button
        }
        #region IDisposable Members
        ~WebServer()
        {
            Dispose();
        }
        public void Dispose()
        {
            if (socket != null)
                socket.Close();
        }
        #endregion
    }
Remember to make sure you are deploying to your Netduino board and not the emulator. You do that by right-clicking on the Netduino project in Visual Studio > properties > .Net Framework. Set "Transport" to USB and confirm "Device" is set to "Netduino Plus".

3. Run it


  • Now run the code in debug mode. 
  • Open another a web browser and enter the IP address that the Netdiuno board displayed in the output window. For me its "http://192.168.0.153/activatedoor"
  • You should see the LED on the Netduino plus light and the door should open or close.
Here is the complete code.



To learn how to write the Android app that will control and monitor the garage door read the step by step blog post here. http://androidcodemonkey.blogspot.com/2011/10/android-garage-door-app.html


40 comments:

  1. I am a newbie in this, but why did you choose pin13 and not pin0? Also for mine I used wires coming from the the garage door button itself connected them to the 2222A transistor with a 5.6kohm resistor between the base and pin0 on the netduino, it seem to be working fine, but I am not sure if it'll damage netduino. Thanks

    ReplyDelete
  2. You can use any pin. There is no reason for pin 13 so any pin including pin 0 is fine. Wiring the garage door button works as well. I used a controller because I had an extra one and I didn't have to run extra wire to the garage.

    ReplyDelete
  3. Thanks a lot. Could you also tell me how to make the simplest switch to tell if the door is open without sensors? I was thinking about just using 3.3v with resistor and connect it to one of the inputs, if the pin sees voltage the door is closed. What would be the safest pin to use as well as resistor?

    ReplyDelete
  4. @Vlad, I am not sure I fully understand how you want to detect the garage door is open. I created a sensor using a phototransistor. You could also use a physical switch that was closed with pressure when the door is open.

    ReplyDelete
  5. That is what I was talking about - a physical switch. How should I connect it to netduino? What pins, resistors, etc. to use? Thanks

    ReplyDelete
  6. Hi Greg. Excelent post :D Congratulations!!! I'm new to netduino microcontrollers and android programming but i already started to program my netduino plus and created a simple app using some of your code to simply control the blue led using my android cell phone. I'm using two commands "ledon" and "ledoff". Currently i'm dealing with some kind of delay that is retarding the reception of commands by netduino, i.e., only after some button pushes (3 or 4) the led wakes up / goes to sleep. Could you give me some hint about the possible cause of this problem? Adriano (ammfjorge@gmail.com)

    ReplyDelete
  7. Greg, Really great work.

    I have the new Netduino Plus 2 and it is running 4.2. Have setup the development platforms (micro framework and android) per the instructions. Used the code as is, except for two things. Changed the IP address and change the android platform to 3.2 (to support my Samsung Tab7.7). Loaded the code on the Netduino and Tab. The app appears on the Tab. When I tap the black box on the app, the progression circle spins. Using Ethereal I see the TCP traffic transmit from the Tab to the Netduino. Then I see Netduino response transmitted to the tab.

    The issue that I am having is that the "OPEN" graphic does not appear. I have built a few sensors and even tested them with a multimeter to see the sensor's output voltage in both hi and low states.

    So, I am assuming that the new MF4.2 on the Netduino is my problem and right now the porting kit isn't an option. Below is the code that I modified from your version to the 4.2. Please let me know if you think it is whacked and if so how would you write it? Thanks!

    //Set the analog pin to monitor Pin 0

    // OLD lanuage -> InputPort garageSensor = new InputPort.AnalogPort(Pins.GPIO_PIN_A0);

    AnalogInput garageSensor = new Microsoft.SPOT.Hardware.AnalogInput(Cpu.AnalogChannel.ANALOG_0);

    //Set sensor range

    // old 4.1 code -> garageSensor.SetRange(0, 1024);
    garageSensor.Read();

    //Program loop
    while (false)
    {
    Debug.Print(garageSensor.Read().ToString());
    Thread.Sleep(1000);
    }
    }

    ReplyDelete
    Replies
    1. The release of the 4.2.2 for the N+2 resolved the incompatibility issues that I was having with your original code.

      Pulling the "SecretLabs.NETMF.Hardware.AnalogInput" reference into the project is also key.

      I am far from complete on this project and a complete noob, so I still grin each time the garage door opens from a browser command.

      Delete
  8. Using your code, how can i access my netduino from an outside network? I love what you did here and it works great on my project, but I don't want to have to be in my local network to use my netduino, i would like to use it while at work. I can't seem to find anything about it online though and i would like to just use your code with it.

    ReplyDelete
  9. now i bought new netduino plus 2 board can any one plsssssssss tel me in which part the codings should be edited in netduino plus 2 and my android app with 2.3 version

    ReplyDelete
  10. Noob question: how do you know what types resistors to use? Maybe dumb question: how do you know you even needed to use them?

    ReplyDelete
  11. This is exactly what I was looking for as a base point for networking my whole house.

    Could you rehost the code on a different service than Skydrive? I'm attempting to download it but it will not let me because Skydrive is blocking it because it is unable to scan for viruses. Thank you.

    ReplyDelete
  12. Greg,
    Is the netduino plus required for this? Or could one use the regular neduino or netduino mini?

    ReplyDelete
  13. This comment has been removed by the author.

    ReplyDelete
  14. I’m sure you will provide the more awesome blogs like these blogs that I’ve enjoyed a lot.
    Homejoy

    ReplyDelete
  15. I like your blog post. Keep on writing this type of great stuff. I'll make sure to follow up on your blog in the future. garage supplies.

    ReplyDelete
  16. Hello

    One question.
    Some times when sending a parameter, i get a No data received page.
    I dont know why this happens so i dont have an ideia on how to fix it.

    Can you please help?

    ReplyDelete
  17. Garage Door Repair Fontana 212-0325 Garage Door Installation, Broken Spring Replacement, Liftmaster and Genie Garage Door Openers & Remote, 24 Hours Garage Door company in Fontana

    ReplyDelete
  18. How did you calculate these values for the resistors (the 100 ohm and 33 ohm ) ?

    ReplyDelete
  19. I really like your blog it excellent. this blog is very helpful and informative. Thanks for sharing with us. supplies

    ReplyDelete
  20. Introduction include modern day gadgets made us all engineering generally based entirely.
    There is not a single second once we will not be put into use any kind of type of engineering.
    Since the people include hardly time, therefore it is being a crying have to obtain every
    little thing done more rapidly than just before. At present, many individuals will not be
    holding funds; instead they are holding ATM minute card to get anything, which is why,
    they have come to be essential for every single company centre, no matter whether retail
    center or maybe take a trip firm, to be furnished with the device where funds can be taken
    from the actual minute card in combination with supplying expenses right away for the
    consumers.
    http://wacsystem.com/

    ReplyDelete
  21. I think this is one of the most vital info of Garage Door Services for me. And I’m glad reading your article.

    ReplyDelete
  22. This comment has been removed by the author.

    ReplyDelete
  23. Hell yes thanks! garage door repair oxnard that's right, its great stuff for me

    ReplyDelete
  24. There are many automatic mechanisms available that you can install in your garage door so that it closes and opens all by itself. You can consult any door specialist before installing any such system for your security doors or garage doors

    ReplyDelete
  25. Thanks for great information, it's much helpful for the new professional.
    Garage Door Opener

    ReplyDelete
  26. Garage Door and Garage Door Opener are a vital part of a modern home, A Garage Door is the single largest point of access to your home and can either enhance or diminish the value of your house.

    We are selling Garage Door Opener San Diego

    ReplyDelete
  27. I will to work on this project.I actually want to see how my garage door will work with this programming.I was looking for such information from last few days. Thanks for the help.
    http://www.automaticdoorspecialists.com/garage-door-and-gate-repair-oceanside-ca/

    ReplyDelete
  28. Are you find any garage door company in New York that provide these services and fix any issues in your garage door? garage door new york

    ReplyDelete
  29. Thanks for sharing information.Your blog has always been a source of great tips.
    garage door company
    garage door service

    ReplyDelete
  30. Good to see these helpful information here,Thanks lots for sharing them with us.
    garage doors ny
    garage door repair new york


    ReplyDelete
  31. I read your post a d i really like your post. Thank you for sharing this post.
    Garage Door Repair NY
    garage door queens

    ReplyDelete
  32. You can visit our showrooms at Fort Myers and Naples for all your carport entryway supplies.Clarks Garage Door & Gate Repair Riverside

    ReplyDelete
  33. I would simply say to you all “awesome information” thanks for shearing with us
    MARANTEC | BERNAL | TWINDOOR | AMARR | DOORTEC | 0503378900

    ReplyDelete
  34. Pretty article! I found some useful information in your blog, it was awesome to read, thanks for sharing this great content to my vision, keep sharing..

    Home Automation in Chennai
    smart home in Chennai
    Home security in Chennai
    Burglar alarm in Chennai
    Wifi Camera Chennai

    ReplyDelete

  35. Wow, I really like your post because the information which you given in this blog is really extra ordinary.

    Austin Garage door repair

    ReplyDelete
  36. To get more information on the basic code for a web server see my previous blog post.repairman

    ReplyDelete
  37. Great and insightful article as always! Love your dedication to keeping up your house!

    If you are a housekeeper or housekeeper seeker, click here to view Housekeeping profiles.

    ReplyDelete

  38. Wow, I really like your post because the information which you given in this blog is really extra ordinary.

    Austin Garage door repair

    ReplyDelete