100 lines
3.1 KiB
Python
100 lines
3.1 KiB
Python
import time
|
|
import datetime as dt
|
|
import logging
|
|
|
|
import RPi.GPIO as GPIO # Import Raspberry Pi GPIO library
|
|
|
|
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
|
|
pin2relay[13] = 1
|
|
pin2relay[16] = 2
|
|
pin2relay[18] = 3
|
|
|
|
|
|
# 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)
|
|
GPIO.setup(16, GPIO.IN, pull_up_down=GPIO.PUD_UP) # Set pin 16 to be an input pin and set initial value to be pulled low (off)
|
|
GPIO.setup(18, GPIO.IN, pull_up_down=GPIO.PUD_UP) # Set pin 18 to be an input pin and set initial value to be pulled low (off)
|
|
|
|
def turn_relay_on(relay_id):
|
|
print ("turning on ", relay_id)
|
|
|
|
def turn_relay_off(relay_id):
|
|
print ("turning off ", relay_id)
|
|
|
|
if __name__ == "__main__":
|
|
|
|
nc_dict = dict()
|
|
nc_dict["ipaddr"] = "192.168.1.2"
|
|
nc_dict["port"] = 502
|
|
nc_dict["uid"] = 240
|
|
|
|
modbus = WebRelay()
|
|
modbus.apply_network_configs(nc_dict)
|
|
modbus.connect_slave()
|
|
'''
|
|
'''
|
|
|
|
pub.subscribe(modbus.turn_relay_off, "turn_relay_off")
|
|
pub.subscribe(modbus.turn_relay_on, "turn_relay_on")
|
|
#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:
|
|
|
|
# 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:
|
|
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)
|
|
|
|
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()
|
|
logging.info("Driver terminated")
|