#!/usr/bin/python # Author: Dipl.-Inform. (FH) Andreas Link - www.AndreasLink.de # Website: http://raspberrypi.link-tech.de/ import RPi.GPIO as GPIO import sys import smbus import time import MySQLdb # Init GPIO GPIO.setmode(GPIO.BCM) GPIO.setup(25, GPIO.IN, pull_up_down=GPIO.PUD_UP) # Init I2C-bus bus = smbus.SMBus(1) # 0 = /dev/i2c-0 (port I2C0), 1 = /dev/i2c-1 (port I2C1) DEVICE_ADDRESS = 0x20 # MCP23017 is without extra Jumper on 0x20 entprellVar = 0 # Used for "entprellen" aka bouncing db = 0 # Database connect curs = 0 # Cursor pointing on MySQL-DB to execute commands def initMySql(): global db, curs print "Init MySQLDB: host 'localhost', user 'dbusername', DB 'homebus'" db = MySQLdb.connect("localhost", "dbusername", "dbpassword", "homebus") curs=db.cursor() print "MySQL-DB init done" def logIOevent(a0, a1, a2, a3, a4, a5, a6, a7, b0, b1, b2, b3, b4, b5, b6, b7): global db, curs try: curs.execute("INSERT INTO `homebus`.`ioevents` (`entry`,`timestamp`,`A0`,`A1`,`A2`,`A3`,`A4`,`A5`,`A6`,`A7`,`B0`,`B1`,`B2`,`B3`,`B4`,`B5`,`B6`,`B7`) VALUES (NULL,CURRENT_TIMESTAMP,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s);", (str(a0),str(a1),str(a2),str(a3),str(a4),str(a5),str(a6),str(a7),str(b0),str(b1),str(b2),str(b3),str(b4),str(b5),str(b6),str(b7))) db.commit() except: # If conction fails, try to reconnect the DB print "Connection to DB lost, trying to reconnect..." initMySql() def initMCP23017(): print "Init MCP23017 (", hex(DEVICE_ADDRESS), ")" bus.write_byte_data(DEVICE_ADDRESS, 0x00, 0xFF) bus.write_byte_data(DEVICE_ADDRESS, 0x01, 0xFF) bus.write_byte_data(DEVICE_ADDRESS, 0x0c, 0xFF) bus.write_byte_data(DEVICE_ADDRESS, 0x0d, 0xFF) bus.write_byte_data(DEVICE_ADDRESS, 0x04, 0xFF) bus.write_byte_data(DEVICE_ADDRESS, 0x05, 0xFF) bus.write_byte_data(DEVICE_ADDRESS, 0x0a, 0x40) bus.write_byte_data(DEVICE_ADDRESS, 0x0b, 0x40) bus.read_byte_data(DEVICE_ADDRESS, 0x12) bus.read_byte_data(DEVICE_ADDRESS, 0x13) print "MCP23017 init done!" # Init MCP23017 all as IN and ALL with Interrupt initMCP23017() # MySSQL-DB init initMySql() # Interrupt Routine, beeing called, when interrupt happens def interrupt_routine(callback): global entprellVar if entprellVar == 0: entprellVar = 1 print time.strftime("%d.%m.%Y %H:%M:%S"), "Interrupt triggered... reading I2C-bus:" time.sleep(0.8) # Relax briefly before reading bus portA = bus.read_byte_data(DEVICE_ADDRESS, 0x12) portB = bus.read_byte_data(DEVICE_ADDRESS, 0x13) # Print raw-data print "PortA:", bin(portA) print "PortB:", bin(portB) # Try to determine which bit is set: print "" print "Port A:" ###### Port A - Pin 0 ##### hexMask = 0x01 #Pin0 0000 0001 hexAND0A = hexMask & portA #AND if int(hexAND0A) == 0: print "A-Pin 0 = true" dbA0 = 1 else: print "A-Pin 0 = false" dbA0 = 0 ##### ----- ##### ###### Port A - Pin 1 ##### hexMask = 0x02 #Pin1 0000 0010 hexAND1A = hexMask & portA #AND if int(hexAND1A) == 0: print "A-Pin 1 = true" dbA1 = 1 else: print "A-Pin 1 = false" dbA1 = 0 ##### ----- ##### ###### Port A - Pin 2 ##### hexMask = 0x04 #Pin2 0000 0100 hexAND2A = hexMask & portA #AND if int(hexAND2A) == 0: print "A-Pin 2 = true" dbA2 = 1 else: print "A-Pin 2 = false" dbA2 = 0 ##### ----- ##### ###### Port A - Pin 3 ##### hexMask = 0x08 #Pin3 0000 1000 hexAND3A = hexMask & portA #AND if int(hexAND3A) == 0: print "A-Pin 3 = true" dbA3 = 1 else: print "A-Pin 3 = false" dbA3 = 0 ##### ----- ##### ###### Port A - Pin 4 ##### hexMask = 0x10 #Pin4 0001 0000 hexAND4A = hexMask & portA #AND if int(hexAND4A) == 0: print "A-Pin 4 = true" dbA4 = 1 else: print "A-Pin 4 = false" dbA4 = 0 ##### ----- ##### ###### Port A - Pin 5 ##### hexMask = 0x20 #Pin5 0010 0000 hexAND5A = hexMask & portA #AND if int(hexAND5A) == 0: print "A-Pin 5 = true" dbA5 = 1 else: print "A-Pin 5 = false" dbA5 = 0 ##### ----- ##### ###### Port A - Pin 6 ##### hexMask = 0x40 #Pin6 0100 0000 hexAND6A = hexMask & portA #AND if int(hexAND6A) == 0: print "A-Pin 6 = true" dbA6 = 1 else: print "A-Pin 6 = false" dbA6 = 0 ##### ----- ##### ###### Port A - Pin 7 ##### hexMask = 0x80 #Pin7 1000 0000 hexAND7A = hexMask & portA #AND if int(hexAND7A) == 0: print "A-Pin 7 = true" dbA7 = 1 else: print "A-Pin 7 = false" dbA7 = 0 ##### ----- ##### print "" print "Port B:" ###### Port B - Pin 0 ##### hexMask = 0x01 #Pin0 0000 0001 hexAND0B = hexMask & portB #AND if int(hexAND0B) == 0: print "B-Pin 0 = true" dbB0 = 1 else: print "B-Pin 0 = false" dbB0 = 0 ##### ----- ##### ###### Port B - Pin 1 ##### hexMask = 0x02 #Pin1 0000 0010 hexAND1B = hexMask & portB #AND if int(hexAND1B) == 0: print "B-Pin 1 = true" dbB1 = 1 else: print "B-Pin 1 = false" dbB1 = 0 ##### ----- ##### ###### Port B - Pin 2 ##### hexMask = 0x04 #Pin2 0000 0100 hexAND2B = hexMask & portB #AND if int(hexAND2B) == 0: print "B-Pin 2 = true" dbB2 = 1 else: print "B-Pin 2 = false" dbB2 = 0 ##### ----- ##### ###### Port B - Pin 3 ##### hexMask = 0x08 #Pin3 0000 1000 hexAND3B = hexMask & portB #AND if int(hexAND3B) == 0: print "B-Pin 3 = true" dbB3 = 1 else: print "B-Pin 3 = false" dbB3 = 0 ##### ----- ##### ###### Port B - Pin 4 ##### hexMask = 0x10 #Pin4 0001 0000 hexAND4B = hexMask & portB #AND if int(hexAND4B) == 0: print "B-Pin 4 = true" dbB4 = 1 else: print "B-Pin 4 = false" dbB4 = 0 ##### ----- ##### ###### Port B - Pin 5 ##### hexMask = 0x20 #Pin5 0010 0000 hexAND5B = hexMask & portB #AND if int(hexAND5B) == 0: print "B-Pin 5 = true" dbB5 = 1 else: print "B-Pin 5 = false" dbB5 = 0 ##### ----- ##### ###### Port B - Pin 6 ##### hexMask = 0x40 #Pin6 0100 0000 hexAND6B = hexMask & portB #AND if int(hexAND6B) == 0: print "B-Pin 6 = true" dbB6 = 1 else: print "B-Pin 6 = false" dbB6 = 0 ##### ----- ##### ###### Port B - Pin 7 ##### hexMask = 0x80 #Pin7 1000 0000 hexAND7B = hexMask & portB #AND if int(hexAND7B) == 0: print "B-Pin 7 = true" dbB7 = 1 else: print "B-Pin 7 = false" dbB7 = 0 ##### ----- ##### print "" # Log Event in DB logIOevent(dbA0, dbA1, dbA2, dbA3, dbA4, dbA5, dbA6, dbA7, dbB0, dbB1, dbB2, dbB3, dbB4, dbB5, dbB6, dbB7) time.sleep(1) entprellVar = 0 else: print "Ignoring double interrupt" # Init Interrupt-Callback-Function GPIO.add_event_detect(25, GPIO.FALLING, callback=interrupt_routine) while True: try: print time.strftime("%d.%m.%Y %H:%M:%S"), "Watchdog activ -> Waiting for a new interrupt..." curs.execute("UPDATE `homebus`.`status` SET `watchdog`=NOW() WHERE `status`.`entry`=0;") db.commit() except: # If conction fails, try to reconnect the DB print "Lost DB-connection, trying to reconnect..." initMySql() entprellVar = 0 time.sleep(60) db.close() GPIO.cleanup()