webrelay-modbus/gpio_test2.py
2020-12-09 06:26:38 -08:00

88 lines
3 KiB
Python

import time
import RPi.GPIO as GPIO # Import Raspberry Pi GPIO library
from pubsub import pub
from WebRelay import WebRelay
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")
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)
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
modbus.disconnect_slave()
print("Program Terminated.")