Soft wiring digital input to a relay



  • What is the best way to "soft wire" a digital input to a relay?

    I want to monitor a digital input port and replicate that state to a relay (if input is On, turn relay On. If input if Off, turn relay Off).

    Right now I have the following code, but I'm not sure that's the most optimal solution:

    #!/usr/bin/python
    
    import RPi.GPIO as GPIO
    from time import sleep
    
    from jsonrpclib import Server
    # connect to unipy
    s=Server("http://localhost/rpc")
    
    # Setup GPIO
    GPIO.setmode(GPIO.BCM)
    port= 27
    GPIO.setup(port, GPIO.IN, pull_up_down=GPIO.PUD_UP)
    
    print "Test port",port
    
    prevstate=False
    try:
            while True:
                    currstate=GPIO.input(port)
                    if currstate <> prevstate:
                            if currstate:
                                    print "Port",port,"is 0"
                                    s.relay_set(1,0)
                            else:
                                    print "Port",port,"is 1"
                                    s.relay_set(1,1)
                                    sleep(0.5)
                    prevstate=currstate
                    sleep(0.1)      
    finally:
            GPIO.cleanup()
    
    print "done"
    
    


  • What is the best way to "soft wire" a digital input to a relay?

    I want to monitor a digital input port and replicate that state to a relay (if input is On, turn relay On. If input if Off, turn relay Off).

    Right now I have the following code, but I'm not sure that's the most optimal solution:

    #!/usr/bin/python
    
    import RPi.GPIO as GPIO
    from time import sleep
    
    from jsonrpclib import Server
    # connect to unipy
    s=Server("http://localhost/rpc")
    
    # Setup GPIO
    GPIO.setmode(GPIO.BCM)
    port= 27
    GPIO.setup(port, GPIO.IN, pull_up_down=GPIO.PUD_UP)
    
    print "Test port",port
    
    prevstate=False
    try:
            while True:
                    currstate=GPIO.input(port)
                    if currstate <> prevstate:
                            if currstate:
                                    print "Port",port,"is 0"
                                    s.relay_set(1,0)
                            else:
                                    print "Port",port,"is 1"
                                    s.relay_set(1,1)
                                    sleep(0.5)
                    prevstate=currstate
                    sleep(0.1)      
    finally:
            GPIO.cleanup()
    
    print "done"
    
    


  • Hello,

    if it is everything you need, we have built in a simple but not easy to understand programming language. See the extcontrols.py file starting at line 270 and evok.py function at line 323. In the extcontrols you can map events to actions and add them to the compute map (so it is done in the next loop).

    However I still do not have time to put it together on wiki with some examples and there are still some todos…

    So for now it is ok, but instead of using the RPi.GPIO library, you could use websocket library and simply react on the messages from the server, like we do in javascript in our webface.