def on_disconnect(c, ud, rc): global g_relayAddress, g_buttonsConnected relayExp.setChannel(g_relayAddress, 0, 0) relayExp.setChannel(g_relayAddress, 1, 0) g_buttonsConnected = False print ("Disconnected") print_status()
def access_lock(uid): #oledExp.write("Your UID is: " + uid) relayExp.setChannel(7, 0, 1) time.sleep(5) relayExp.setChannel(7, 0, 0) #oledExp.clear() return
def turnON(): relayAddr = 7 initRelay = relayExp.driverInit(relayAddr) if relayExp.checkInit(relayAddr): logData('relay is init') else: logData('ERR relay not init') exit() relayExp.setChannel(relayAddr, 0, 1)
def root1(pinStates=pinStates): global loggedIn print(loggedIn) global pinStates2 if (loggedIn == False): password = request.forms.get('password') print('below is return for password') print(password) if (password != '1234'): return template('login.tpl') else: loggedIn = True pinStates = pinStates2 buttonNum = (str(request.forms.get('btnId'))).splitlines() buttonNum = buttonNum[0] print (buttonNum) if (buttonNum == 'None'): return template('gpio.tpl', pinStates = pinStates) buttonNum = int(buttonNum) pinStates = pinStates2.split(',') if (pinStates[buttonNum] == '0on'): pinStates[buttonNum] = '0off' omega_gpio.setoutput(dictPins[buttonNum], 1) elif (pinStates[buttonNum] == '0off'): pinStates[buttonNum] = '0on' omega_gpio.setoutput(dictPins[buttonNum], 0) elif (pinStates[buttonNum] == '1on'): pinStates[buttonNum] = '1off' omega_gpio.setoutput(dictPins[buttonNum], 1) elif (pinStates[buttonNum] == '1off'): pinStates[buttonNum] = '1on' omega_gpio.setoutput(dictPins[buttonNum], 0) elif (pinStates[buttonNum] == '2on'): pinStates[buttonNum] = '2off' omega_gpio.setoutput(dictPins[buttonNum], 1) elif (pinStates[buttonNum] == '2off'): pinStates[buttonNum] = '2on' omega_gpio.setoutput(dictPins[buttonNum], 0) elif (pinStates[buttonNum] == '3on'): pinStates[buttonNum] = '3off' relayExp.setChannel(7,0,0) elif (pinStates[buttonNum] == '3off'): pinStates[buttonNum] = '3on' relayExp.setChannel(7,0,1) else: print ('Unable to decode the pinStates string') print (pinStates) pinStates2 = ",".join(pinStates) print (pinStates2) return template('gpio.tpl', pinStates = pinStates2)
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 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 lightControl(light, desiredState): #set relay i2c address led = int2Addr(light) ledAddr = led[0] ledChannel = led[1] #set output to desiredState input status1 = relayExp.setChannel(ledAddr, ledChannel, desiredState) return 1
def main(argv): global g_relayAddress, g_pingStarted, g_pingSuccess, g_pingFailCount, g_buttonsConnected g_relayAddress = int(argv[1]) get_ip_address() print("g_relayAddress is " + str(g_relayAddress)) oledExp.driverInit() client.on_message = on_message client.on_connect = on_connect client.on_disconnect = on_disconnect client.connect(broker) oledExp.setDisplayPower(1) relayExp.driverInit(g_relayAddress) g_gpioHandler.setOutputDirection(0) try: client.subscribe("control/#") client.loop_start() while True: time.sleep(1); if g_pingStarted: if g_pingSuccess: g_pingSuccess = False elif g_pingSuccess == False: g_pingFailCount += 1 if g_pingFailCount > 5: turn_pump_off() g_buttonsConnected = False print_status() g_pingStarted = False g_pingFailCount = 0 except KeyboardInterrupt: print("exiting on ctrl-c") client.disconnect() #disconnect client.loop_stop() #stop loop g_buttonsConnected = False relayExp.setChannel(g_relayAddress, 0, 0) relayExp.setChannel(g_relayAddress, 1, 0) time.sleep(2) sys.exit(0)
def setRelayforDoor(desired_door, new_state): desired_relay = relay_right if desired_door == door_name_left: desired_relay = relay_left if new_state == 0 or new_state == 1: status = relayExp.setChannel(relay_sw_addr, desired_relay, new_state) errorValue = True if status == 0: errorValue = False print_line('* relay [{}] set to=[{}], status=[{}]'.format( desired_relay, new_state, status), debug=True, error=errorValue)
# initialize the relay-exp ret = relayExp.driverInit(addr) print "Result from relayDriverInit: ", ret if ret != 0: exit() # 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) # 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
def setRelay(num, state): relayExp.setChannel(defaultChannel, num, state)
def open_lock(): relayExp.setChannel( 7, 0, 1) # # Setting Relay with address 7, channel 0 and state is 1 (ON) return
print "checking if initialized: ", ret # initialize the relay-exp ret = relayExp.driverInit(addr) print "Result from relayDriverInit: ", ret if (ret != 0): exit() # 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()
def close_lock(): relayExp.setChannel( 7, 0, 0) # Setting Relay with address 7, channel 0 and state is 0 (OFF) return
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 open_valve(): print ("Opening valve") global g_relayAddress if relayExp.setChannel(g_relayAddress, 1, 1) == 0: client.publish("aquarium/valve/open") print_status()
for x in range(0, pins): omega_gpio.initpin(dictPins[x],'out') # Initialize the relay. If more than one expansion then this will need to use # a for loop to do multiple units. relayStatus = relayExp.driverInit(7) if (relayStatus == 0): print('Relay is initialized') else: print('Relay is NOT initialized') # This sets all pins to off and all relays to open for x in range(0, pins): omega_gpio.setoutput(dictPins[x], 1) for x in range(0, relays): relayExp.setChannel(7,0,x) # This allows use of files in the static directory @route('/static/<filename>') def server_static(filename): return static_file(filename, root = './static/') @route('/') @post('/<pinStates>') def root1(pinStates=pinStates): global loggedIn print(loggedIn) global pinStates2 if (loggedIn == False): password = request.forms.get('password')
def turnLeft90(): relayExp.setChannel(addr, 0, 1) time.sleep(0.25) relayExp.setChannel(addr, 0, 0) return
def turnRight90(): relayExp.setChannel(addr, 1, 1) time.sleep(0.25) relayExp.setChannel(addr, 1, 0) return
def EEG_MrESP1_ACQ(self): STATE_IDLE = 0 STATE_FIRST = 1 STATE_SECOND = 2 STATE_LEN = 3 STATE_DATA = 4 state = 0 print("EEG_MrESP1_ACQ") dispstate = 0 dataindex = 0 # code:time x = time.localtime() flocaltime = time.strftime('BLueBCI_MrESP1_BP_%Y-%m-%d_%H-%M-%S', x) print(flocaltime) f = open("/root/DATA/" + flocaltime + ".hs2f", "wb") f.write("####### EEG_BlueBCI_MrESPV1 \r\n") f.write("####### Made By Daniel Hwang \r\n") f.close() f = open("/root/DATA/" + flocaltime + ".hs2f", "a") #self.l_serial.write(self.connect_senddata) receive_stack = [] while True: # use file to COM_OBplot, (fake data) while True: ans = self.l_serial.read(self.connect_receive_len) receive_stack += ans #print(len(ans)) while (ans): ans = self.l_serial.read(self.continue_receive_len) #print(ans.encode('hex')) receive_stack += ans while len(receive_stack) != 0: tmp = receive_stack[0] receive_stack = receive_stack[1:] #print (hex(ord(tmp))) if state == STATE_IDLE: if ord(tmp) == 170: #0xAA state = STATE_FIRST #print "first" elif state == STATE_FIRST: if ord(tmp) == 170: #0xAA state = STATE_SECOND lencnt = 0 lenstr = "" #print "second" elif state == STATE_SECOND: #record 4 indax if ord(tmp) == 32: #0x20 state = STATE_SECOND lencnt = 0 lenstr = "" #print "second" state = STATE_LEN datapt = 0 datastr = "" elif state == STATE_LEN: if len(datastr) < 32: # datastr += tmp datapt += 1 if len(datastr ) == 30: #the 29 for the focus value #since the datastr++, the 30 for that if ord( datastr[28] ) == 4: #0x04 the Attention protocal print(hex(ord(datastr[28]))) print(ord(datastr[29])) if ord(datastr[29]) < 50: # set channel 1 to off ret = relayExp.setChannel( addr, 1, 0) print "Result from relaySetChannel: ", ret if (ret != 0): exit() else: # set channel 1 to on ret = relayExp.setChannel( addr, 1, 1) print "Result from relaySetChannel: ", ret if (ret != 0): exit() else: state = STATE_IDLE elif len(datastr) == 32: dataindex += 1 print(datastr.encode('hex')) if ord(datastr[30] ) == 5: #the 30 is fixed 5 print(hex(ord(datastr[30]))) print(ord(datastr[31])) self.s2f_switch = 1 if self.s2f_switch == 1: global s2f_buffer s2f_buffer.append(str(dataindex)) s2f_buffer.append(', ') s2f_buffer.append( str(datastr.encode('hex'))) s2f_buffer.append('\r') print(len(s2f_buffer)) if len( s2f_buffer ) >= 20: #4*5 #record every 5s data #print(len(s2f_buffer)) #f = open("/root/DATA/"+flocaltime+".hs2f", "a") for i in range( len(s2f_buffer)): f.write(str(s2f_buffer[i])) #f.close(); s2f_buffer = [] #20171120 why need??? #datastr=""; print("s2f successful!!! \r\n") else: state = STATE_IDLE elif len(datastr) >= 32: #print (datastr.encode('hex')) state = STATE_IDLE
def set(self, relay, state): relayExp.setChannel(self.defaultAddress, relay, state)