def __init__(self): logger.info('Create instances of all components') self.io_expander_1_i2c = mcp23017.mcp23017(MCP23017_ADDR_0) self.io_expander_2_i2c = mcp23017.mcp23017(MCP23017_ADDR_1) self.adc1 = ad4112.ad4112() self.config_locked_by_system = False
def __init__(self): self.io_expander_1_i2c = mcp23017.mcp23017(MCP23017_ADDR_0) self.io_expander_2_i2c = mcp23017.mcp23017(MCP23017_ADDR_1) self.io_expander_spi = mcp23s17.mcp23s17() self.dac = dac61408.dac61408() self.adc1 = ad4112.ad4112() # cs 1 self.adc2 = ad4112.ad4112() # cs 2
def read_Input(s_addr,s_port): seade = MCP_seade[s_addr] port = MCP_port[s_port] mpin = 0 mymcp = mcp23017() # print (seade, port) seis = inpin[mpin][s_port] while (mpin < pin_count): # print ("Sisend:", port, "-", mpin, "value-", inpin[mpin][s_port]) mypin = pin(mymcp, str(port), mpin) seis = mypin.value() aeg = str(currentDT.strftime('%Y/%m/%d %H:%M:%S')) if seis != inpin[mpin][s_port]: print (aeg, "ALARM: Sisend:", port, "-", mpin, "value -", inpin[mpin][s_port], "muudetud =", seis) # Kirjuta alarm logisse f = open(logFile,'a') f.write(aeg + ': ALARM! Sisend: ' + str(port) + ' pin-' + str(mpin + 1) + ' seis = ' + str(seis) + '\n') f.close() # MQTT_sens client = mqtt.Client() client.username_pw_set(MQTT_user, password=MQTT_password) client.on_connect = on_connect client.connect(MQTT_HOST, MQTT_port, MQTT_KEEPALIVE_INTERVAL) client.on_log = on_log client.publish_mqtt = publish_mqtt(MQTT_pub_topic + MQTT_seade + str(seade) + '_' + str(port) + '_' + str(mpin + 1), statusMap[seis]) client.disconnect() # Salvesta viimane sisendi väärtus inpin[mpin][s_port] = seis # else: # print ("Sisend:", port, "-", mpin, "value", inpin[mpin][s_port], "muutmata") mpin = mpin + 1
def __init__(self, addr, port): self.addr = addr self.port = port self.mc23017 = mcp23017(self.addr,port) self.init_mcp23017() self.lcd1602 = lcd1602("LCD1602",0); self.lcd1602.on_update_io = self.on_update_1602IO self.lcd1602.init_4bitmode();
import smbus # Get I2C bus, this is I2C Bus 1 bus = smbus.SMBus(1) #define which GPIOs are to be used as outputs. By default all GPIOs are defined as inputs. #because MCP23017 chips have two banks of 8 GPIOs we can optionally pass two sets to the object constructor #the first set in the array will be for bank 0 and the second will be for bank 1. #even if only bank 0 has input it still must be passed in an array i.e. [{0,1,2,3}] #in each set the numbers passed will be the number of the GPIO that should be set as outputs i.e. 0 is the first GPIO 1 is the second gpio_output_map = [{0,1,2,3},{0,1,2,3}] #kwargs is a Python set that contains the address of your device and the output map to be passed to the object for initialization. kwargs = {'address': 0x20, 'gpio_output_map': gpio_output_map} #create the MCP23017 object from the MCP23017 library and pass it the GPIO output map and address defined above #the object requires that you pass it the bus object so that it can communicate and share the bus with other chips if necessary mcp23017_1 = mcp23017(bus, kwargs) #Now I will define a second MCP23017 chips so that I can send commands to it. #The address of the second MCP23017 will automatically be one above the address configured for the first MCP23017 #This means that if you keep the first MCP23017 at the default address of 0x20 (byte value 32) then the address of the second MCP23017 # will be 0x21 (byte value 33) #This map sets all of bank 1 as outputs and leaves bank 2 in the default input state. gpio_output_map = [{0,1,2,3,4,5,6,7}, {0,1,2,3,4}] kwargs = {'address': 0x21, 'gpio_output_map': gpio_output_map} mcp23017_2 = mcp23017(bus, kwargs)
# Get I2C bus, this is I2C Bus 1 bus = smbus.SMBus(1) #define which GPIOs are to be used as outputs. By default all GPIOs are defined as inputs. #because MCP23017 chips have two banks of 8 GPIOs we can optionally pass two sets to the object constructor #the first set in the array will be for bank 0 and the second will be for bank 1. #even if only bank 0 has input it still must be passed in an array i.e. [{0,1,2,3}] #in each set the numbers passed will be the number of the GPIO that should be set as outputs i.e. 0 is the first GPIO 1 is the second gpio_output_map = [{0, 1, 2, 3}, {0, 1, 2, 3}] #kwargs is a Python set that contains the address of your device and the output map to be passed to the object for initialization. kwargs = {'address': 0x20, 'gpio_output_map': gpio_output_map} #the object requires that you pass it the bus object so that it can communicate and share the bus with other chips if necessary #create the MCP23017 object from the MCP23017 library and pass it the GPIO output map and address defined above #the object requires that you pass it the bus object so that it can communicate and share the bus with other chips if necessary mcp23017 = mcp23017(bus, kwargs) while True: #turn on Relay at GPIO 0 in bank 1 aka the first relay mcp23017.set_gpio_high(1, 0) time.sleep(.2) #turn off Relay at GPIO 0 in bank 1 aka the first relay mcp23017.set_gpio_low(1, 0) time.sleep(.2) #toggle Relay at GPIO 1 in bank 1 aka the second relay mcp23017.toggle_gpio(1, 1) time.sleep(.2) #toggle Relay at GPIO 2 in bank 2 aka the third relay mcp23017.toggle_gpio(2, 2) time.sleep(.2) #toggle Relay at GPIO 2 in bank 2 aka the third relay in bank 2
import signal import sys from time import sleep # ----------------------------------------------- # Sensoren libraries aus CaravanPi einbinden # ----------------------------------------------- sys.path.append('/home/pi/CaravanPi/.lib') from mcp23017 import mcp23017, pin from CaravanPiLedClass import Led # ----------------------------------------------- # globale Variable # ----------------------------------------------- # initiate GPIO port expander mymcp1 = mcp23017(1, 0x20) mymcp2 = mcp23017(1, 0x21) # LED threads LED_HR = [None, None, None] LED_HL = [None, None, None] LED_ZR = [None, None, None] LED_ZL = [None, None, None] LED_VR = [None, None, None] LED_VL = [None, None, None] LED_Vo = [None, None, None] # ------------------------- # adjust Threats for LEDs # -------------------------
# !/usr/bin/python import datetime from mcp23017 import mcp23017, pin from time import sleep sisend = 0 mymcp = mcp23017() mypin = pin(mymcp, "gpioa", sisend) while True: currentDT = datedime.datedime.now() seis = mypin.value() if seis == 0: print(currentDT.strftime("%Y-%m-%d %H:%M:%S"), "Alarm sisendis ->", sisend, seis) if sisend < 7 sisens = sisend + 1 else: sisend = 0 sleep(1)
class Led(object): # initiate GPIO port expander MYMCP1 = mcp23017(1, 0x20) MYMCP2 = mcp23017(1, 0x21) LED_OFF = 0 LED_ON = 1 LED_FLASHING = 2 # the short time sleep to use when the led is on or off to ensure the led responds quickly to changes to blinking FAST_CYCLE = 0.05 def __init__(self, mcp, gpio, led_pin): # create the semaphore used to make thread exit self.pin_stop = threading.Event() # the pin for the LED self.__mcp = led_pin self.__gpio = led_pin self.__led_pin = led_pin # initialise the pin and turn the led off self.__led_address = pin(mcp, gpio, led_pin) ## GPIO.setmode(GPIO.BCM) ## GPIO.setup(self.__led_pin, GPIO.OUT) # the mode for the led - off/on/flashing self.__ledmode = Led.LED_OFF # make sure the LED is off (this also initialises the times for the thread) self.off() # create the thread, keep a reference to it for when we need to exit self.__thread = threading.Thread(name='ledblink', target=self.__blink_pin) # start the thread self.__thread.start() def blink(self, time_on=0.050, time_off=1): # blinking will start at the next first period # because turning the led on now might look funny because we don't know # when the next first period will start - the blink routine does all the # timing so that will 'just work' self.__ledmode = Led.LED_FLASHING self.__time_on = time_on self.__time_off = time_off def off(self): self.__ledmode = self.LED_OFF # set the cycle times short so changes to ledmode are picked up quickly self.__time_on = Led.FAST_CYCLE self.__time_off = Led.FAST_CYCLE # could turn the LED off immediately, might make for a short flicker on if was blinking def on(self): self.__ledmode = self.LED_ON # set the cycle times short so changes to ledmode are picked up quickly self.__time_on = Led.FAST_CYCLE self.__time_off = Led.FAST_CYCLE # could turn the LED on immediately, might make for a short flicker off if was blinking def reset(self): # set the semaphore so the thread will exit after sleep has completed self.pin_stop.set() # wait for the thread to exit self.__thread.join() # now clean up the GPIO GPIO.cleanup() ############################################################################ # below here are private methods def __turnledon(self): self.__led_address.enable() ## GPIO.output(pin, GPIO.LOW) def __turnledoff(self): self.__led_address.disable() ## GPIO.output(pin, GPIO.HIGH) # this does all the work # If blinking, there are two sleeps in each loop # if on or off, there is only one sleep to ensure quick response to blink() def __blink_pin(self): while not self.pin_stop.is_set(): # the first period is when the LED will be on if blinking if self.__ledmode == Led.LED_ON or self.__ledmode == Led.LED_FLASHING: self.__turnledon() else: self.__turnledoff() # this is the first sleep - the 'on' time when blinking time.sleep(self.__time_on) # only if blinking, turn led off and do a second sleep for the off time if self.__ledmode == Led.LED_FLASHING: self.__turnledoff() # do an extra check that the stop semaphore hasn't been set before the off-time sleep if not self.pin_stop.is_set(): # this is the second sleep - off time when blinking time.sleep(self.__time_off)