Working automatic mode.No DB stuff yet.
This commit is contained in:
@@ -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
|
||||
|
||||
20
automatic/output.py
Normal file
20
automatic/output.py
Normal file
@@ -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'])
|
||||
@@ -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()
|
||||
|
||||
|
||||
68
automatic/state_trans.py
Normal file
68
automatic/state_trans.py
Normal file
@@ -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
|
||||
Reference in New Issue
Block a user