Daemonized valve driver

This commit is contained in:
Yeongdo Park 2020-12-10 05:09:41 -08:00
parent f3ea630bbf
commit 91670794a4

View file

@ -1,16 +1,98 @@
import sys, os
def dummy_handler(signum, frame):
print("received ", signum)
return
def handler(signum, frame):
raise KeyboardInterrupt()
return
def main():
""" An example daemon main routine; writes a datestamp to file
/tmp/daemon-log every 10 seconds.
""" Daemon main routine; update relay state according to
GPIO buttons every 0.1 seconds.
"""
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("/tmp/valve-test2-log"),
],
format='%(asctime)s %(message)s',
datefmt='%m/%d/%Y %H:%M:%S',
level=logging.INFO
)
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)
GPIO.setup(13, GPIO.IN, pull_up_down=GPIO.PUD_UP)
GPIO.setup(16, GPIO.IN, pull_up_down=GPIO.PUD_UP)
GPIO.setup(18, GPIO.IN, pull_up_down=GPIO.PUD_UP)
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()
logging.info("Driver started")
pub.subscribe(modbus.turn_relay_off, "turn_relay_off")
pub.subscribe(modbus.turn_relay_on, "turn_relay_on")
valve1_old = False
valve2_old = False
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)
f = open("/tmp/daemon-log", "w")
while 1:
f.write('%s\n' % time.ctime(time.time()))
f.flush()
time.sleep(10)
if __name__ == "__main__":
# Do the Unix double-fork magic; see Stevens's book "Advanced
@ -41,4 +123,4 @@ if __name__ == "__main__":
sys.exit(1)
# Start the daemon main loop
main( )
main()