Advertisement
Guest User

Untitled

a guest
Oct 19th, 2018
106
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.43 KB | None | 0 0
  1. #!/usr/bin/python
  2. # coding: utf-8
  3.  
  4. import RPi.GPIO as gpio
  5. import time
  6. import mysql.connector
  7. from mysql.connector import Error
  8. #import threading
  9. #import thread
  10.  
  11. #class keypad(threading.Thread):
  12. class keypad():
  13.     matrix = [["1","2","3","A"],
  14.           ["4","5","6","B"],
  15.           ["7","8","9","C"],
  16.           ["*","0","#","D"]]
  17.  
  18.     pins = []
  19.     spalte = [32, 36, 38, 40]
  20.     zeile = [12, 16, 18, 22]
  21.     pin = ""
  22.     zaehler = 0
  23.  
  24.     def __init__(self):
  25.         #threading.Thread.__init__(self)
  26.         gpio.setmode(gpio.BOARD)
  27.         gpio.setwarnings(False)
  28.         self.getCodes()
  29.         print(self.pins)
  30.         self.GpioSetup()
  31.    
  32.     def GpioSetup(self):
  33.         for j in range(4):
  34.             gpio.setup(self.spalte[j], gpio.OUT)
  35.             gpio.output(self.spalte[j], 1)
  36.             gpio.setup(self.zeile[j],gpio.IN, pull_up_down=gpio.PUD_UP)
  37.         self.run()
  38.  
  39.     def keypad(self):
  40.       while True:
  41.         for j in range(4):
  42.             gpio.output(self.spalte[j], 0)
  43.             for i in range(4):
  44.               if gpio.input(self.zeile[i]) == 0:
  45.                   benutzerEingabe = self.matrix[i][j]
  46.                   while gpio.input(self.zeile[i]) == 0:
  47.                        pass
  48.                   return benutzerEingabe
  49.             gpio.output(self.spalte[j], 1)
  50.       return False
  51.  
  52.     def run(self):
  53.         try:
  54.             while self.zaehler < 4:
  55.                 self.pin += self.keypad()
  56.                 print(self.pin)
  57.                 self.zaehler += 1
  58.                 time.sleep(0.2)
  59.         except KeyboardInterrupt:
  60.             gpio.cleanup()
  61.  
  62.         if self.zaehler == 4:
  63.             if self.pin == self.pins[0] or self.pin == self.pins[1]:
  64.                 print("Access granted")
  65.             else:
  66.                 print("Access denied")
  67.    
  68.     def getCodes(self):
  69.         try:
  70.             connection = mysql.connector.connect(host='localhost', database='zugang',user='root', password='abcD123')
  71.             query = 'SELECT pin FROM rolle'
  72.             cursor = connection.cursor()
  73.             cursor.execute(query)
  74.             records = cursor.fetchall()
  75.            
  76.             for row in records:
  77.                 self.pins.append(row[0])
  78.            
  79.             cursor.close()
  80.        
  81.         except Error as e:
  82.             print("Error", e)
  83.        
  84.         finally:
  85.             if(connection.is_connected()):
  86.                 connection.close()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement