From 266204d9ce27498efd1f86c1734e848630ad1867 Mon Sep 17 00:00:00 2001 From: Shaun Setlock Date: Fri, 12 Nov 2021 13:55:45 -0500 Subject: [PATCH] Pushing some work on automatic mode. --- automatic/input.py | 43 +++++++++++++++++++++++++++++++++++++++++++ automatic/routine.py | 35 +++++++++++++++++++++++++++++++++++ manual/manual.py | 3 +++ relay/relay.py | 2 +- 4 files changed, 82 insertions(+), 1 deletion(-) create mode 100644 automatic/input.py create mode 100644 automatic/routine.py diff --git a/automatic/input.py b/automatic/input.py new file mode 100644 index 0000000..02626e2 --- /dev/null +++ b/automatic/input.py @@ -0,0 +1,43 @@ +#! env/bin/python3 + +import RPi.GPIO as GPIO +import time +from datetime import datetime + +def get_pin_input(): + + # Pin Assignments + PB1_BCM = 4 # Green + PB2_BCM = 27 # Yellow + PB3_BCM = 13 # Blue + RELAY1_PIN = 26 # Water Pump + RELAY2_PIN = 20 # Unassigned + RELAY3_PIN = 21 # Lamp + + # Setup the Inputs to use the internal pull-down. + GPIO.setup(PB1_BCM, GPIO.IN, pull_up_down=GPIO.PUD_DOWN) + GPIO.setup(PB2_BCM, GPIO.IN, pull_up_down=GPIO.PUD_DOWN) + GPIO.setup(PB3_BCM, GPIO.IN, pull_up_down=GPIO.PUD_DOWN) + + # Setup the Outputs. + GPIO.setup(RELAY1_PIN, GPIO.OUT) + GPIO.setup(RELAY2_PIN, GPIO.OUT) + GPIO.setup(RELAY3_PIN, GPIO.OUT) + + # Get current time. + now = datetime.now() + hour = now.hour + minute = now.minute + + # Define alias variables for input devices. + inputs = { + 'green_button' : GPIO.input(PB1_BCM), + 'yellow_button' : GPIO.input(PB2_BCM), + 'blue_button' : GPIO.input(PB3_BCM), + 'pump' : GPIO.input(RELAY1_PIN), + 'lamp' : GPIO.input(RELAY3_PIN), + 'hour' : hour, + 'minute' : minute, + } + + return inputs \ No newline at end of file diff --git a/automatic/routine.py b/automatic/routine.py new file mode 100644 index 0000000..f5c63d6 --- /dev/null +++ b/automatic/routine.py @@ -0,0 +1,35 @@ +#! env/bin/python3 + +import RPi.GPIO as GPIO +import time +from input import get_pin_input + +if __name__ == "__main__": + + GPIO.setmode(GPIO.BCM) + GPIO.setwarnings(False) + + # If any error, Exit cleanly. + try: + + while True: + + inputs = get_pin_input() + + if 6 < inputs['hour'] < 22: + GPIO.output(21,True) + else: + GPIO.output(21,False) + + if inputs['minute']-30 < 0: + GPIO.output(26,True) + else: + GPIO.output(26,False) + + # Cleanup on Exit. + finally: + + GPIO.output(21,False) + GPIO.output(26,False) + GPIO.cleanup() + diff --git a/manual/manual.py b/manual/manual.py index 1e64946..e672729 100644 --- a/manual/manual.py +++ b/manual/manual.py @@ -70,5 +70,8 @@ if __name__ == "__main__": GPIO.output(RELAY1_PIN, pump_state) GPIO.output(RELAY3_PIN, lamp_state) + # Avoid running a single thread maxxed out. + time.sleep(0.1) + finally: GPIO.cleanup() diff --git a/relay/relay.py b/relay/relay.py index 99c3de3..c5d484a 100644 --- a/relay/relay.py +++ b/relay/relay.py @@ -34,4 +34,4 @@ if __name__ == "__main__": time.sleep(1.0) finally: - GPIO.cleanup() \ No newline at end of file + GPIO.cleanup()