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
    Posts
  • #22550

    dennyfmn
    Participant

    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.

    [attachment=2:svv412y1]DoorSwitch.jpg[/attachment:svv412y1]

    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.

    [attachment=1:svv412y1]OSPiDoorInterface.png[/attachment:svv412y1]


    Edit 2/22/2014: The schematic and all the example code refer to pin numbers defined with GPIO.setmode(GPIO.BCM). Dan subsequently changed ospi.py to use GPIO.setmode(GPIO.BOARD). If the version of ospi.py 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/readpins.py

    #! /usr/bin/python

    import time
    import sys

    import RPi.GPIO as GPIO
    GPIO.setwarnings(False)

    # using BCM GPIO 00..nn numbers
    GPIO.setmode(GPIO.BCM)

    # 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.')
    else:
    print('Garage Door is Closed.')

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

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

    cat /usr/local/bin/toggledoor.py

    #! /usr/bin/python

    import time
    import sys

    import RPi.GPIO as GPIO
    GPIO.setwarnings(False)

    # using BCM GPIO 00..nn numbers
    GPIO.setmode(GPIO.BCM)

    # 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)
    time.sleep(1)
    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 ospi.py at the bottom of the section #### pin defines ####


    pin_door_control = 23
    pin_door_sense = 24
    pin_rain_sense = 25

    Added to ospi.py under #### Function Definitions ####


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

    Added to ospi.py 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 ospi.py 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:
    [attachment=0:svv412y1]DoorWorking.JPG[/attachment:svv412y1]

    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.

    #24909

    virtus
    Participant

    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 ospi.py? There is an ospi_addon.py file for some kinds of external code so you don’t have to manually re-add everything whenever the program is updated.

    #24910

    dennyfmn
    Participant

    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 ospi.py.

    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.

    #24911

    kkpwrh2o
    Member

    Love this project. Thanks for the detailed info.

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

    Thanks,
    Kevin

    #24912

    dennyfmn
    Participant

    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 opsi.py 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.

    Denny

    #24913

    Dan in CA
    Participant

    Denny,

    Just a thought, but I wonder if you could use the patch program to add your mods to ospi.py and .js files.
    https://en.wikipedia.org/wiki/Patch_%28Unix%29

    Samer has used it to insert some bug fixes into ospi.py. 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.

    Dan

    #24914

    dennyfmn
    Participant

    Hi Dan,

    Great idea, I had been thinking about diff/patch too. Yesterday I made a couple of patch files for opsi.py 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 ospi.py 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.

    Denny

    #24915

    Dan in CA
    Participant

    Denny,

    Check the Python GPIO info at:
    http://www.raspberrypi-spy.co.uk/2012/05/install-rpi-gpio-python-library/

    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 gv.sd = 1 for rain sensed, gv.sd = 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(gv.sd) 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 gv.sd = 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.

    Dan

    #24916

    dennyfmn
    Participant

    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. http://www.zoobapps.com has lots of free icons, the one I chose is http://www.zoobapps.com/free_icons/index.php?icon_id=25220&cat_id=

    [attachment=2:2dczcqz9]reloadall-5777.png[/attachment:2dczcqz9]
    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.

    [attachment=1:2dczcqz9]svc_updown.png[/attachment:2dczcqz9]
    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 http://igoogle.wunderground.com line. I’ll show where the link argument is pointing to in just a minute.


    w("");

    So now the home page looks like this:

    [attachment=0:2dczcqz9]ToggleDoorButtonAdded.JPG[/attachment:2dczcqz9]

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

    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)
    time.sleep(1)
    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):
    ToggleDoor()
    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!

    #24918

    pierantonio
    Participant

    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 “readpins.py” correctly indicates the status open or closed.
    I’m a newbie in python, someone can help me please?
    Thanks in advance!!

    Pierantonio

    #24917

    dennyfmn
    Participant

    Hi Pierantonio,

    If the readpins.py 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

    From:
    GPIO.setmode(GPIO.BCM)
    To:
    GPIO.setmode(GPIO.BOARD)

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

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

    The readpins.py 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 ospi.py 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…

    Denny

    #24919

    pierantonio
    Participant

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

    All the best!

    Bye, Pierantonio.

    #24920

    dennyfmn
    Participant

    Great! Glad that did the trick!

    #24921

    Ray
    Keymaster

    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:
    viewtopic.php?f=28&t=490

    #24922

    pierantonio
    Participant

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

    #24923

    pierantonio
    Participant

    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