- Joined
- Sep 16, 2016
- Messages
- 399
- Reaction score
- 215
Productive 30 mins this afternoon
import time,json,argparse
from MCP23017_I2C import *
#######################################
# Setup Arguments and Parse
#######################################
parser = argparse.ArgumentParser()
parser.add_argument("--R0", help="Set state of Relay 0",choices=['on', 'off'])
parser.add_argument("--R1", help="Set state of Relay 1",choices=['on', 'off'])
parser.add_argument("--R2", help="Set state of Relay 2",choices=['on', 'off'])
parser.add_argument("--R3", help="Set state of Relay 3",choices=['on', 'off'])
args = parser.parse_args()
if args.R0:
print("Set Relay 0 to {}".format(args.R0))
if args.R1:
print("Set Relay 1 to {}".format(args.R1))
if args.R2:
print("Set Relay 2 to {}".format(args.R2))
if args.R3:
print("Set Relay 3 to {}".format(args.R3))
#######################################
# Read JSON File Containing States
#######################################
with open('relay.states','r') as infile:
D = json.load(infile)
globals().update(D)
#######################################
# Set Relay States From Args
#######################################
if args.R0 == 'on':
relay_0_state = 1
if args.R1 == 'on':
relay_1_state = 1
if args.R2 == 'on':
relay_2_state = 1
if args.R3 == 'on':
relay_3_state = 1
if args.R0 == 'off':
relay_0_state = 0
if args.R1 == 'off':
relay_1_state = 0
if args.R2 == 'off':
relay_2_state = 0
if args.R3 == 'off':
relay_3_state = 0
#######################################
# Write JSON File Containing States
#######################################
vars={} # the dictionary we will save.
vars['relay_0_state'] = relay_0_state
vars['relay_1_state'] = relay_1_state
vars['relay_2_state'] = relay_2_state
vars['relay_3_state'] = relay_3_state
with open('relay.states','w') as myfile:
json.dump(vars,myfile,indent=2)
#######################################
# Set Relay States
#######################################
GPIO_CHIP_1 = GPIO_CHIP(0x20, 1) # define chip MCP23017_I2C
GPIO_CHIP_1.output(0, relay_0_state, 'A')
GPIO_CHIP_1.output(1, relay_1_state, 'A')
GPIO_CHIP_1.output(2, relay_2_state, 'A')
GPIO_CHIP_1.output(3, relay_3_state, 'A')
from MCP23017_I2C import *
#######################################
# Setup Arguments and Parse
#######################################
parser = argparse.ArgumentParser()
parser.add_argument("--R0", help="Set state of Relay 0",choices=['on', 'off'])
parser.add_argument("--R1", help="Set state of Relay 1",choices=['on', 'off'])
parser.add_argument("--R2", help="Set state of Relay 2",choices=['on', 'off'])
parser.add_argument("--R3", help="Set state of Relay 3",choices=['on', 'off'])
args = parser.parse_args()
if args.R0:
print("Set Relay 0 to {}".format(args.R0))
if args.R1:
print("Set Relay 1 to {}".format(args.R1))
if args.R2:
print("Set Relay 2 to {}".format(args.R2))
if args.R3:
print("Set Relay 3 to {}".format(args.R3))
#######################################
# Read JSON File Containing States
#######################################
with open('relay.states','r') as infile:
D = json.load(infile)
globals().update(D)
#######################################
# Set Relay States From Args
#######################################
if args.R0 == 'on':
relay_0_state = 1
if args.R1 == 'on':
relay_1_state = 1
if args.R2 == 'on':
relay_2_state = 1
if args.R3 == 'on':
relay_3_state = 1
if args.R0 == 'off':
relay_0_state = 0
if args.R1 == 'off':
relay_1_state = 0
if args.R2 == 'off':
relay_2_state = 0
if args.R3 == 'off':
relay_3_state = 0
#######################################
# Write JSON File Containing States
#######################################
vars={} # the dictionary we will save.
vars['relay_0_state'] = relay_0_state
vars['relay_1_state'] = relay_1_state
vars['relay_2_state'] = relay_2_state
vars['relay_3_state'] = relay_3_state
with open('relay.states','w') as myfile:
json.dump(vars,myfile,indent=2)
#######################################
# Set Relay States
#######################################
GPIO_CHIP_1 = GPIO_CHIP(0x20, 1) # define chip MCP23017_I2C
GPIO_CHIP_1.output(0, relay_0_state, 'A')
GPIO_CHIP_1.output(1, relay_1_state, 'A')
GPIO_CHIP_1.output(2, relay_2_state, 'A')
GPIO_CHIP_1.output(3, relay_3_state, 'A')