def return_state(): global g_pingStarted, g_pingFailCount, g_buttonsConnected, g_gpioHandler fields = 0; if relayExp.readChannel(g_relayAddress, 0) == 1: fields = 1 << 0 if relayExp.readChannel(g_relayAddress, 1) == 1: fields = 1 << 1 if g_gpioHandler.getValue() == 1: fields = 1 << 2 relaystate = str(fields).encode('ascii') print("Sending state of " + relaystate) client.publish("aquarium/state", relaystate, 1, True) g_pingStarted = True g_buttonsConnected = True
def turn_pump_on(): print("Turning pump on") global g_relayAddress if relayExp.readChannel(g_relayAddress, 0) == 0: if relayExp.setChannel(g_relayAddress, 0, 1) == 0: client.publish("aquarium/pump/on") print_status()
def print_relay2_status(): global g_relayAddress oledExp.setCursor(5, 0) if relayExp.readChannel(g_relayAddress, 1) == 0: oledExp.write("Valve : closed") else: oledExp.write("Valve : open ")
def print_relay1_status(): global g_relayAddress oledExp.setCursor(4, 0) if relayExp.readChannel(g_relayAddress, 0) == 0: oledExp.write("Pump : off") else: oledExp.write("Pump : on ")
def close_valve(): print ("Closing Valve") global g_relayAddress if relayExp.readChannel(g_relayAddress, 0) == 1: turn_pump_off() if relayExp.setChannel(g_relayAddress, 1, 1) == 0: client.publish("aquarium/valve/close") print_status()
def initial_setup(): status_oled = oledExp.driverInit() status_relay = relayExp.driverInit(7) # 7 is the address of the Relay Expansion; 7 is when all relay switches are switched OFF check = relayExp.readChannel(7, 0) # (7, 0) - again 7 is the address and 0 is the relay channel if check == 1: close_lock() with open('data.json') as json_file: # Your UIDs could be different, make changes to the data.json file according to your settings data = json.load(json_file) return data['accepted'] return None
def checkStatus(light): led = int2Addr(light) status = relayExp.readChannel(led[0], led[1]) if status == 1: print 'Relay ON' elif status == 0: print 'Relay OFF ' else: print 'Error: Bad status.' return status
# check initialization # should return 1 since the Expansion was initialized above ret = relayExp.checkInit(addr) print "checking if initialized: ", ret time.sleep(1) # set channel 0 to on ret = relayExp.setChannel(addr, 0, 1) print "Result from relaySetChannel: ", ret if (ret != 0): exit() time.sleep(2) # read channel 0 value value = relayExp.readChannel(addr, 0) print "Channel 0 value: ", value time.sleep(2) # set both channels to on ret = relayExp.setAllChannels(addr, 1) print "Result from relaySetAllChannels: ", ret if (ret != 0): exit() time.sleep(2) # set channel 0 to off ret = relayExp.setChannel(addr, 0, 0) print "Result from relaySetChannel: ", ret if (ret != 0): exit()
The first iteration will be just to run 14h per day, 0500-1900 Intended implementation on omega2 with relay expansion """ import time from OmegaExpansion import relayExp led1 = 7 init = 0 #intiialize relay status = relayExp.driverInit(led1) print status led1Init = relayExp.checkInit(1) #check initialization if led1Init == 0: print 'Initialization unsuccessful' led1Init = relayExp.driverInit(led1) else: print 'Initialization successful' while 1: #read current relay state state1 = relayExp.readChannel(led1, 1) if state1 == 1: output1 = 0 print 'Relay found ON' else: output1 = 1 print 'Relay found OFF' status1 = relayExp.setChannel(led1, 1, output1) time.sleep(2)
def read(self, channel): return relayExp.readChannel(self.defaultAddress, channel)