diff --git a/test.py b/test.py index 3c4b1d4..5a66b91 100644 --- a/test.py +++ b/test.py @@ -1,41 +1,43 @@ -import gpiozero import time +import os from datetime import datetime -import RPi.GPIO as GPIO -GPIO.setmode(GPIO.BCM) +def debug_print(message: str): + now = datetime.now() + current_time = now.strftime("%H:%M:%S") + print('[DEBUG]['+current_time+'] '+message) -GPIO.setup(19, GPIO.OUT) -GPIO.output(19, GPIO.LOW) -time.sleep(1) -GPIO.output(19, GPIO.HIGH) +def error_print(message: str): + now = datetime.now() + current_time = now.strftime("%H:%M:%S") + print('[ERROR]['+current_time+'] '+message) + + +relay_channels = ["19", "16", "26", "20"] +for channel in relay_channels: + os.system('echo "'+channel+'" /sys/class/gpio/export') + os.system('echo "out" > /sys/class/gpio/gpio'+channel+'/direction') + + +def relay_controller(pin: int, state: str): + if state == "True": + os.system('echo "1" > /sys/class/gpio/gpio'+str(pin)+'/value') + debug_print("Set relay-pin "+str(pin)+" to state True") + elif state == "False": + os.system('echo "0" > /sys/class/gpio/gpio'+str(pin)+'/value') + debug_print("Set relay-pin "+str(pin)+" to state False") + else: + error_print("Unknown state ("+state+") for relay channel "+str(pin)) + +channel_1 = 19 +channel_2 = 16 +channel_3 = 26 +channel_4 = 20 + +relay_controller(channel_1, "True") +time.sleep(2) +relay_controller(channel_1, "False") time.sleep(5) -GPIO.cleanup() -# def debug_print(message: str): -# now = datetime.now() -# current_time = now.strftime("%H:%M:%S") -# print('[DEBUG]['+current_time+'] '+message) - -# def error_print(message: str): -# now = datetime.now() -# current_time = now.strftime("%H:%M:%S") -# print('[ERROR]['+current_time+'] '+message) - -# def relay_controller(pin: int, state: str): -# if state == "True": -# relay = gpiozero.OutputDevice(pin, active_high=False, initial_value=False) -# relay.on() -# debug_print("Set relay-pin "+str(pin)+" to state True") -# elif state == "False": -# relay = gpiozero.OutputDevice(pin, active_high=False, initial_value=False) -# relay.off() -# debug_print("Set relay-pin "+str(pin)+" to state False") -# else: -# error_print("Unknown state for relay ("+state+")") - -# while True: -# relay_controller(19, "True") -# time.sleep(2) -# relay_controller(19, "False") -# time.sleep(2) \ No newline at end of file +for channel in relay_channels: + os.system('echo "'+channel+'" > /sys/class/gpio/unexport') \ No newline at end of file