forked from johnwargo/raspberry-pi-relay-timer
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrelay-test.py
38 lines (30 loc) · 1.08 KB
/
relay-test.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
#!/usr/bin/python
# A simple Python application for controlling a relay board from a Raspberry Pi
# The application uses the GPIO Zero library (https://gpiozero.readthedocs.io/en/stable/)
# The relay is connected to one of the Pi's GPIO ports, then is defined as an Output device
# in GPIO Zero: https://gpiozero.readthedocs.io/en/stable/api_output.html#outputdevice
import sys
import time
import gpiozero
import relay
# set this variable to the GPIO pin the relay is connected to
RELAY_PIN = 18
def main_loop():
# initialize the relay, nothing will work until you do
relay.init(RELAY_PIN)
# Turn the relay off, just to make sure it starts off
relay.set_status(False)
while 1:
# then toggle the relay every second until the app closes
relay.toggle()
# wait a second
time.sleep(1)
if __name__ == "__main__":
try:
main_loop()
except KeyboardInterrupt:
# turn the relay off, just in case its on
relay.set_status(False)
print("\nExiting application\n")
# exit the application
sys.exit(0)