relay driver for test on 20.12.14
This commit is contained in:
parent
f97b580b11
commit
f3ea630bbf
1 changed files with 43 additions and 31 deletions
|
|
@ -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
|
||||
# Valve 1 Switch
|
||||
if GPIO.input(11) == 0:
|
||||
print("Fill Container ", 1)
|
||||
if not valve1_old:
|
||||
logging.info("Open Valve 1")
|
||||
valve1_old = not valve1_old
|
||||
pub.sendMessage("turn_relay_on", relay_id=0)
|
||||
else:
|
||||
print("Stop Container ", 1)
|
||||
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:
|
||||
print("Fill Container ", 2)
|
||||
if not valve2_old:
|
||||
logging.info("Open Valve 2")
|
||||
valve2_old = not valve2_old
|
||||
pub.sendMessage("turn_relay_on", relay_id=1)
|
||||
else:
|
||||
print("Stop Container ", 2)
|
||||
if valve2_old:
|
||||
logging.info("Close Valve 2")
|
||||
valve2_old = not valve2_old
|
||||
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)
|
||||
|
||||
else:
|
||||
print("Cold")
|
||||
pub.sendMessage("turn_relay_off", relay_id=0)
|
||||
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")
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue