diff --git a/gpio_test2.py b/gpio_test2.py index 3d24415..d56b8de 100644 --- a/gpio_test2.py +++ b/gpio_test2.py @@ -1,4 +1,6 @@ import time +import datetime as dt +import logging import RPi.GPIO as GPIO # Import Raspberry Pi GPIO library @@ -6,6 +8,19 @@ from pubsub import pub from WebRelay import WebRelay +tnow = dt.datetime.now() + +logging.basicConfig( + handlers = [ + logging.FileHandler("log." + tnow.strftime("%Y%m%d-%H%M%S")), + logging.StreamHandler(), + ], + format='%(asctime)s %(message)s', + datefmt='%m/%d/%Y %H:%M:%S', + level=logging.INFO + ) + +logging.info("Driver started") pin2relay = {} pin2relay[11] = 0 @@ -14,7 +29,7 @@ pin2relay[16] = 2 pin2relay[18] = 3 -GPIO.setwarnings(False) # Ignore warning for now +# GPIO.setwarnings(False) # Ignore warning for now GPIO.setmode(GPIO.BOARD) # Use physical pin numbering GPIO.setup(11, GPIO.IN, pull_up_down=GPIO.PUD_UP) # Set pin 11 to be an input pin and set initial value to be pulled low (off) GPIO.setup(13, GPIO.IN, pull_up_down=GPIO.PUD_UP) # Set pin 13 to be an input pin and set initial value to be pulled low (off) @@ -45,44 +60,41 @@ if __name__ == "__main__": #pub.subscribe(turn_relay_off, "turn_relay_off") #pub.subscribe(turn_relay_on, "turn_relay_on") + valve1_old = False + valve2_old = False + try: while True: - # Container 1,2 and 3 Hot - if GPIO.input(18) == 0: - print("Hot") - - # Container 1 Full - if GPIO.input(11) == 0: - print("Fill Container ", 1) - pub.sendMessage("turn_relay_on", relay_id=0) - else: - print("Stop Container ", 1) - pub.sendMessage("turn_relay_off", relay_id=0) - - # Container 2 Full - if GPIO.input(13) == 0: - print("Fill Container ", 2) - pub.sendMessage("turn_relay_on", relay_id=1) - else: - print("Stop Container ", 2) - pub.sendMessage("turn_relay_off", relay_id=1) - - # Container 3 Full - if GPIO.input(16) == 0: - print("Fill Container ", 3) - pub.sendMessage("turn_relay_on", relay_id=2) - else: - print("Stop Container ", 3) - pub.sendMessage("turn_relay_off", relay_id=2) + # Valve 1 Switch + if GPIO.input(11) == 0: + if not valve1_old: + logging.info("Open Valve 1") + valve1_old = not valve1_old + pub.sendMessage("turn_relay_on", relay_id=0) else: - print("Cold") + if valve1_old: + logging.info("Close Valve 1") + valve1_old = not valve1_old pub.sendMessage("turn_relay_off", relay_id=0) + + # Container 2 Full + if GPIO.input(13) == 0: + if not valve2_old: + logging.info("Open Valve 2") + valve2_old = not valve2_old + pub.sendMessage("turn_relay_on", relay_id=1) + else: + if valve2_old: + logging.info("Close Valve 2") + valve2_old = not valve2_old pub.sendMessage("turn_relay_off", relay_id=1) - pub.sendMessage("turn_relay_off", relay_id=2) time.sleep(0.1) + except KeyboardInterrupt: GPIO.cleanup() # Clean up + pub.sendMessage("turn_relay_off", relay_id=0) + pub.sendMessage("turn_relay_off", relay_id=1) modbus.disconnect_slave() - print("Program Terminated.") + logging.info("Driver terminated")