0

Using openhab2 on machine A. Machine B is a RPi which controls a relay. Using pigpio and gpiozero from machine a to control machine b gpio pins.

Using below script for testing. How can I rewrite this so that the on/off function in openhab will work? as of now it just loops between on and off. help a noob please

#!/usr/bin/python
# https://gpiozero.readthedocs.io/en/stable/
# https://gpiozero.readthedocs.io/en/stable/api_output.html#outputdevice

import sys
import time
import gpiozero

relay = gpiozero.OutputDevice(18, active_high=False, initial_value=False)

def set_relay(status):
    if status:
        print("Setting relay: ON")
        relay.on()
    else:
        print("Setting relay: OFF")
        relay.off()

def toggle_relay():
    print("toggling relay")
    relay.toggle()

def main_loop():
    while 1:
        # then toggle the relay every second until the app closes
        toggle_relay()
        # wait a second
        time.sleep(1)

if __name__ == "__main__":
    try:
        main_loop()
    except KeyboardInterrupt:
        # turn the relay off
        set_relay(False)
    
    print("\nExiting application\n")
    # exit the application
    sys.exit(0)
Jason Aller
  • 3,541
  • 28
  • 38
  • 38
  • I've edited your code to make sure the indentation is as intended – ben_nuttall Jan 24 '19 at 16:34
  • It's not clear what you want to do. There's a lot of lines of code that do very little. You define `set_relay` and never call it. `toggle_relay` just calls `relay.toggle()` and nothing else, and you do it every second in a loop. There's no mention of which part of the system is remote (pigpio) and which is local. What is the problem you have? – ben_nuttall Jan 24 '19 at 16:37
  • Sorry about that, quite new here. The relay itself is remote. This is run like this; PIGPIO_ADDR=192.168.10.169 python /etc/openhab2/scripts/on.py. But what i really want to achieve is having openhab send an on/off thru the exec binding. Instead of the stupid looping going on here. And i did not write this, this was just something i managed to find to test it all with. –  Jan 24 '19 at 19:28

0 Answers0