OpenSprinkler Forums Hardware Questions OpenSprinkler Pi (OSPi) OSPi – Interval – Garage Door Sensing and Control – Success

  • This topic is empty.
Viewing 16 posts - 1 through 16 (of 16 total)
  • Author
  • #22550


    Edit: 9/29/2013 – See below for getting a “Toggle Door” button working on the home page. Thanks for all the help Dan!

    See my post at viewtopic.php?f=28&t=355 which talks about keeping up to date
    with git while maintaining local changes and implementing the rain sensor.

    I have added some GPIO interfacing to the Raspberry Pi to get the garage door state, open and close the door, and to define a rain sensor input pin. I chose GPIO pins that were adjacent to each other and a GND that do not interfere with the OSPi connections. I spliced my +5v and GND connections for the relay coil to the 3 conductor cable that connects the OSPi power to the Raspberry Pi. There may be a cleaner way to do this, but it worked for me.

    I used the internal pullup capability on the inputs, so they would read HI until pulled to GND with the contact closures for the door sensor and rain sensor switches. I used a wired magnetically actuated switch that looks like the Radio Shack, 54-630 – Switch-Magnetic Reed Flange MNT, that I found at a local electronics surplus store. The magnet will actuate the switch from an inch away, so mounting the magnet on the edge of the door, and the switch on the frame was no problem.


    For the output, I used an old 4N25A optical isolator I had in the parts bin to drive a 5 volt normally open relay. The 4N25A specs show a typical 100% current transfer ratio from the photo diode to the output transistor. This means that it needs to be driven with about 20 ma to turn on the transistor and pull in the relay contacts. The contacts are wired across the push button switch that opens and closes the door in the garage. If I were going to buy new parts for this, I’d get a Darlington type opto isolator that has higher current gain so that it does not have to be driven so hard. I don’t think sourcing 20 ma is a problem for the Pi, since it’s only active for 1 second at a time to simulate a garage door button push. My local Radio Shack had the relay (Compact 5VDC/1A SPST Reed Relay, Model: 275-232) in stock. Its 250 ohm coil presents a minimal load on the +5 volt supply. If you use a higher gain opto isolator, you can increase the value of R1 to reduce the amount of current through the photo diode. Play around with the value.


    Edit 2/22/2014: The schematic and all the example code refer to pin numbers defined with GPIO.setmode(GPIO.BCM). Dan subsequently changed to use GPIO.setmode(GPIO.BOARD). If the version of that you are running uses GPIO.setmode(GPIO.BOARD), use the small pin numbers on the schematic that are on the header pins themselves.

    Here are python scripts to read the state of the switches and toggle the door up and down.

    cat /usr/local/bin/

    #! /usr/bin/python

    import time
    import sys

    import RPi.GPIO as GPIO

    # using BCM GPIO 00..nn numbers

    # Output to drive opto-isolator to control door open/close relay
    GPIO.setup(23, GPIO.OUT)

    # Input with pullup to read garage door state, 0, Low, False = Closed
    GPIO.setup(24, GPIO.IN, pull_up_down=GPIO.PUD_UP)

    # Input with Pullup to read rain sensor, 0, Low, False = Rain
    GPIO.setup(25, GPIO.IN, pull_up_down=GPIO.PUD_UP)

    if GPIO.input(24):
    print('Garage Door is Open.')
    print('Garage Door is Closed.')

    if GPIO.input(23):
    print('Garage Door drive is HIGH.')
    print('Garage Door drive is LOW.')

    if not GPIO.input(25):
    print('Rain = true')
    print('Rain = False')

    cat /usr/local/bin/

    #! /usr/bin/python

    import time
    import sys

    import RPi.GPIO as GPIO

    # using BCM GPIO 00..nn numbers

    # Output to drive opto-isolator to control door open/close relay
    GPIO.setup(23, GPIO.OUT, initial=GPIO.LOW)

    # Simulate button push by turning relay on for a short time
    GPIO.output(23, GPIO.HIGH)
    GPIO.output(23, GPIO.LOW)

    I was able to follow the excellent post “Interval Website & CPU Temp”, by doczaius, to see how to add a Garage door status to the home page of the Interval web interface.

    Added to at the bottom of the section #### pin defines ####

    pin_door_control = 23
    pin_door_sense = 24
    pin_rain_sense = 25

    Added to under #### Function Definitions ####

    def getDoorState():
    if GPIO.input(pin_door_sense):
    res1 = "'Open'"
    res1 = "'Closed'"
    return res1

    Added to after the section #### setup GPIO pins to interface with shift register ####

    GPIO.setup(pin_door_control, GPIO.OUT, initial=GPIO.LOW)
    GPIO.setup(pin_door_sense, GPIO.IN, pull_up_down=GPIO.PUD_UP)
    GPIO.setup(pin_rain_sense, GPIO.IN, pull_up_down=GPIO.PUD_UP)

    Added to in the #### Class Definitions #### section – I’m showing the two previous lines including the one from the CPU temp patch

    homepg += 'n'
    homepg += 'n'
    homepg += 'n'

    Added to …OSPi/static/scripts/java/svc1.8/home.js after the two lines shown, again I’d already added the CPU temp patch.

    w("Device time: "+datestr(devt*1000)+"
    w("CPU Temp: "+(cputemp)+"
    w("Garage Door: "+(gdoor)+"

    Here is a screen shot:

    I want to be able to add a button to the home page that will call my python script to raise and lower the door.

    Can anyone give me a clue to how to do this cleanly? I’m looking at the section of home.js near line 18 where the menu buttons Refresh, Options, Stations, Programs, and Log buttons are defined.

    I’m new to python, any help will be greatly appreciated.



    Very nice! I’m going to start on my garage door project this weekend and your legwork will come in handy. I’ll look at the code later tonight, but the button should be pretty simple. I’m going to do something similar but I’ll include HTTP commands so other programs can view the door status and open/close them.

    Dan’s been working on the program a lot – have you looked into trying to move some of your code out of There is an file for some kinds of external code so you don’t have to manually re-add everything whenever the program is updated.



    Yes, I looked at the external addon capability and didn’t see how it would help much, since there are little chunks of code spread out in several places in

    Glad to hear Dan is working on the program, I hope he adds the rain sensor logic. I’d very much like to see a per-program input from the rain sensor. I need to have my lawn obey the rain sensor, but not the pots on the deck and window boxes.

    When I set up the I/O for the garage door,I arbitrarily picked an input pin for the rain sensor. It could easily be redefined.



    Love this project. Thanks for the detailed info.

    Any impact to the installation code given Dan’s recent software update?




    Hi Kevin,

    I have been manually applying my changes after doing updates. I copy into the new OSPi/data directory the two files that preserve your settings and programs; Dan covers that in his announcement.

    You may want to look at my recent post on how to start/stop from /etc/init.d if you plan on fiddling around with the code. This makes it a lot easier to start/stop the program. Otherwise Dan’s instructions are fine for starting it from /etc/rc.local if you are going to boot it and forget it.



    Dan in CA


    Just a thought, but I wonder if you could use the patch program to add your mods to and .js files.

    Samer has used it to insert some bug fixes into I think that by using the -u switch you could add your changes to an updated program file and it might even be a way to distribute custom additions to the program.

    I am looking into adding the rain sensor logic.

    Also, regarding your suggestion for having the rain sensor control individual zones, that could be done by adding something like a “Sense Rain” column to the stations page next to the “Activate Master” check boxes. That looks doable.




    Hi Dan,

    Great idea, I had been thinking about diff/patch too. Yesterday I made a couple of patch files for and home.js, so applying my local updates will be a lot easier in the future.

    I’m glad to hear that you are looking at the rain sensor logic. I’ve been looking at the code in to try to at least read the input signal and get the state of the rain sensor to show up on the home page. I’m having a hard time figuring out how the code works there.

    Having the rain sensor state be available for control in each program would really be great.



    Dan in CA


    Check the Python GPIO info at:

    Look under the Example Usage section at the bottom of the page to see how to set the pins for input/output.

    To get the Rain sensor state to show on the OpenSprinkler home page you just need to set the var = 1 for rain sensed, = 0 for no rain.
    EDIT: Scratch that. It needs more to make it work. I’ll let you know when I have the correct incantations.
    OK. I added +’,rs=’+str( to one of the script lines (~426) in the class home: definition. Now if you check “Use rain sensor:” on the Options page and have = 1 it will show “rain detected” on the home page and all stations will have a strike through. Bit it does not actually stop the stations from running – yet. I’ll work on adding that to the next update.




    With some great help from Dan, I now have a button on the home page that will open or close the garage door when clicked.

    Dan has also implemented the per station rain sensor option so that is really cool too. If you look at the screen shot below, you can see that the Deck and Window Boxes stations are still active even though the Lawn stations are off because rain has been detected.

    Here’s how to put a button on the home page and get it to do something when clicked.

    First I wanted a button with a suitable icon that would match the good looking ones on the page. I searched for free 32×32 bit png icons and couldn’t find one that looked like a garage door, but I found one that I could edit to serve. has lots of free icons, the one I chose is

    I rotated it 270 degrees and edited the tails off the arrows. I did the editing with Windows Paint, but found that neither the Win7 or WinXP version would save the icon with its transparency. I had to use Gimp to restore the transparency to the white backround so that the icon would look like the others. Gimp calls it the alpha layer. Saved the output as svc_updown.png to comply with the naming convention and uploaded it to ../OSPi/static/images/icons.

    To get the button to show up on the page, add a line to ../OSPi/static/scripts/java/svc1.8.3/home.js. I put it right after the commented out line. I’ll show where the link argument is pointing to in just a minute.


    So now the home page looks like this:


    Now to get the button to do the action, here’s what gets added to ../OSPi/

    Since I have previously added as documented above the pin definition for the door control and had initialized it to be an output, I needed to add a function to raise and lower the output for a short time to activate the relay that simulates a button push for the door. Added to the Functions section:

    # Simulate garage door button push by turning relay on for a short time
    def ToggleDoor():
    GPIO.output(pin_door_control, GPIO.HIGH)
    GPIO.output(pin_door_control, GPIO.LOW)

    Now to integrate the action into the web page add a url to the bottom of the urls section:

    '/rev', 'show_revision',
    '/tgd', 'toggle_garageDoor', <---- added

    Notice that ‘tgd’ is the parameter used in home.js where the button is placed on the page and the onclick link defined.

    The last step is to define a class to service the url, added to the bottom of the Classes section:

    class toggle_garageDoor:
    """Open and close garage door"""
    def GET(self):
    time.sleep(12) # Give the door time to open or close before refreshing the home page
    raise web.seeother('/') # This line refreshes the home page

    Notice that the class name is the same as the one defined in the urls section. The time.sleep(12) is there so that the new state of the door is displayed when the home page is refreshed. When opening, the door clears the “closed” sensor very quickly, but when the door is closing, it takes about 11 seconds to trip the “closed” switch.

    That’s it. Click the button on the home page, the door goes up, click it again, the door goes down. What fun!

    Hopefully you can use this as-is or as a template to get OSPi and Interval to do all kinds of cool things.

    Thanks to Dan for all his help in getting this going! Having him explain the magic of the url and class was the key!



    hello, fantastic this project, but i don’t understand a thing.
    I need to put only the garage state on the homepage and I did it, but the garage door indicator is always open, what’s wrong?
    If I launch the python script “” correctly indicates the status open or closed.
    I’m a newbie in python, someone can help me please?
    Thanks in advance!!




    Hi Pierantonio,

    If the script correctly reports the state of the door, that verifes that the hardware is hooked up correctly, good!

    After this article was written Dan changed the setmode for the GPIO


    This has the effect of changing the pin number you need to define in

    Pin 24 in GPIO.setmode(GPIO.BCM) becomes
    Pin 18 in GPIO.setmode(GPIO.BOARD)

    The script explicity defines Pin 24 for doorsense in GPIO.setmode(GPIO.BCM), so if that’s the pin you used, it will report correctly.

    Check the setmode command used in the version of that you are running. If it is GPIO.BOARD, change the pin definition from 24 to 18.

    Sounds like you are very close. I hope this helps…




    Thank u very much Denny!!
    Now with pin 18 in GPIO.setmode work perfectly!!!

    All the best!

    Bye, Pierantonio.



    Great! Glad that did the trick!



    Just a quite note that the latest OSPi v1.3 has a built-in mini-relay (rated at 120V/2A) which can be used for garage door control. The three relay pins are mapped out on the PCB (NO–normally open, CO–change over, NC–normally closed). A related thread is here:



    Hi Dan, thanks for your explanation, fantastic!!
    I’m waiting for my arrival, in theory next week.



    Hi all.
    Now with the new version of the interval program 2.0 is possibile to add again this function on the home page?
    Thanks to anyone who help me.

    Bye, Pierantonio.

Viewing 16 posts - 1 through 16 (of 16 total)
  • You must be logged in to reply to this topic.

OpenSprinkler Forums Hardware Questions OpenSprinkler Pi (OSPi) OSPi – Interval – Garage Door Sensing and Control – Success