From ae30c39954d776ef7c24f8f3c5c2885e784c4ec0 Mon Sep 17 00:00:00 2001 From: Tanner Collin Date: Sun, 1 May 2022 01:49:53 +0100 Subject: [PATCH] Add relay drivers --- rpiserver/relays.py | 41 +++++++++++++++++++++++++++++++++++++++++ 1 file changed, 41 insertions(+) create mode 100644 rpiserver/relays.py diff --git a/rpiserver/relays.py b/rpiserver/relays.py new file mode 100644 index 0000000..45c9f46 --- /dev/null +++ b/rpiserver/relays.py @@ -0,0 +1,41 @@ +import asyncio +import RPi.GPIO as GPIO + +RELAY1 = 4 +RELAY2 = 22 +RELAY3 = 6 +RELAY4 = 26 + +RELAY_ON = True +RELAY_OFF = False + +RELAYS = [RELAY1, RELAY2, RELAY3, RELAY4] + +def set_relay(r, state): + GPIO.output(r, state) + +def all_off(): + for r in RELAYS: + set_relay(r, RELAY_OFF) + +def init_relays(): + GPIO.setmode(GPIO.BCM) + GPIO.setwarnings(False) + + for r in RELAYS: + GPIO.setup(r, GPIO.OUT) + + all_off() + +async def test(): + for r in RELAYS: + set_relay(r, RELAY_ON) + await asyncio.sleep(0.5) + set_relay(r, RELAY_OFF) + +if __name__ == '__main__': + init_relays() + + loop = asyncio.get_event_loop() + loop.run_until_complete(test()) + loop.close()