From 7bbdbc8998f2e714a2ed5ea14244bfab7954102f Mon Sep 17 00:00:00 2001 From: Shaun Setlock Date: Fri, 12 Nov 2021 15:23:57 -0500 Subject: [PATCH] Working automatic mode.No DB stuff yet. --- automatic/input.py | 10 ------ automatic/output.py | 20 ++++++++++++ automatic/routine.py | 60 +++++++++++++++++++++++++++-------- automatic/state_trans.py | 68 ++++++++++++++++++++++++++++++++++++++++ 4 files changed, 135 insertions(+), 23 deletions(-) create mode 100644 automatic/output.py create mode 100644 automatic/state_trans.py diff --git a/automatic/input.py b/automatic/input.py index 02626e2..ccacbfa 100644 --- a/automatic/input.py +++ b/automatic/input.py @@ -14,16 +14,6 @@ def get_pin_input(): 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 diff --git a/automatic/output.py b/automatic/output.py new file mode 100644 index 0000000..e66a290 --- /dev/null +++ b/automatic/output.py @@ -0,0 +1,20 @@ +#! env/bin/python3 + +import RPi.GPIO as GPIO +import time +from datetime import datetime + +def write_pin_output(outputs): + + # Setup for Hardware + RELAY1_PIN = 26 # Water Pump + RELAY2_PIN = 20 # Unassigned + RELAY3_PIN = 21 # Lamp + + # Write outputs. + if outputs['detect']: + time.sleep(1.0) + + GPIO.output(RELAY1_PIN, outputs['pump']) + GPIO.output(RELAY2_PIN, False) + GPIO.output(RELAY3_PIN, outputs['lamp']) \ No newline at end of file diff --git a/automatic/routine.py b/automatic/routine.py index f5c63d6..fd71721 100644 --- a/automatic/routine.py +++ b/automatic/routine.py @@ -2,34 +2,68 @@ import RPi.GPIO as GPIO import time + from input import get_pin_input +from state_trans import check_state +from output import write_pin_output if __name__ == "__main__": GPIO.setmode(GPIO.BCM) GPIO.setwarnings(False) - # If any error, Exit cleanly. + # 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) + try: + auto_mode = False + while True: - + + # Detect user input. inputs = get_pin_input() - if 6 < inputs['hour'] < 22: - GPIO.output(21,True) - else: - GPIO.output(21,False) + # Activate auto mode if the Green PB is pressed. + if auto_mode or inputs['green_button'] == True: - if inputs['minute']-30 < 0: - GPIO.output(26,True) - else: - GPIO.output(26,False) + inputs['mode'] = "AUTO" + auto_mode = True + + # Activate manual mode if the Yellow or Blue PB's are pressed. + if not auto_mode or inputs['yellow_button'] == True or inputs['blue_button'] == True: + + inputs['mode'] = "MANUAL" + auto_mode = False + + # Make any transitions based on mode and inputs. + outputs = check_state(inputs) + + # Write the outputs which correspond to the desired state. + write_pin_output(outputs) + + # Avoid maxxing out a single thread. + time.sleep(0.1) # Cleanup on Exit. finally: - - GPIO.output(21,False) - GPIO.output(26,False) + GPIO.output(RELAY1_PIN, False) + GPIO.output(RELAY2_PIN, False) + GPIO.output(RELAY3_PIN, False) GPIO.cleanup() diff --git a/automatic/state_trans.py b/automatic/state_trans.py new file mode 100644 index 0000000..61519b6 --- /dev/null +++ b/automatic/state_trans.py @@ -0,0 +1,68 @@ +#! env/bin/python3 + +import RPi.GPIO as GPIO +import time +from datetime import datetime + +def check_state(inputs): + + # Define alias variables for input devices. + green_button = inputs['green_button'] + yellow_button = inputs['yellow_button'] + blue_button = inputs['blue_button'] + + # Define state variables for each of the output devices. + pump_state = inputs['pump'] + lamp_state = inputs['lamp'] + + # Get mode and schedule information. + mode = inputs['mode'] + hour = inputs['hour'] + minute = inputs['minute'] + + # Determine state. + + # Assume no change in state. + pump_out = pump_state + lamp_out = lamp_state + + # Automatic mode. + if mode == "AUTO": + + detect = False + + if 6 < hour < 22: + lamp_out = True + pump_out = True + else: + lamp_out = False + pump_out = False + + # Manual mode. + if mode == "MANUAL": + + detect = False + + # Pump Flip-Flop + if not detect and blue_button and pump_state: + pump_out = False + detect = True + if not detect and blue_button and not pump_state: + pump_out = True + detect = True + # Lamp Flip-Flop + if not detect and yellow_button and lamp_state: + lamp_out = False + detect = True + if not detect and yellow_button and not lamp_state: + lamp_out = True + detect = True + + # Define alias variables for input devices. + outputs = { + 'detect' : detect, + 'pump' : pump_out, + 'lamp' : lamp_out, + } + + return outputs \ No newline at end of file