Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/usr/bin/env python
- # -*- coding: utf8 -*-
- import RPi.GPIO as GPIO
- import MFRC522
- import signal
- import mysql.connector
- from mysql.connector import Error
- #import threading
- #import thread
- #class read(threading.Thread):
- class read():
- continue_reading = True
- pins = []
- def __init__(self):
- #threading.Thread.__init__(self)
- signal.signal(signal.SIGINT, self.end_read)
- self.getCodes()
- self.read()
- def end_read(self,signal,frame):
- self.continue_reading
- print "Ctrl+C captured, ending read."
- self.continue_reading = False
- GPIO.cleanup()
- #def run(self):
- def read(self):
- MIFAREReader = MFRC522.MFRC522()
- while self.continue_reading:
- # Scan for cards
- (status,TagType) = MIFAREReader.MFRC522_Request(MIFAREReader.PICC_REQIDL)
- # If a card is found
- if status == MIFAREReader.MI_OK:
- print "Card detected"
- # Get the UID of the card
- (status,uid) = MIFAREReader.MFRC522_Anticoll()
- # If we have the UID, continue
- if status == MIFAREReader.MI_OK:
- # Print UID
- print "Card read UID: "+str(uid[0])+","+str(uid[1])+","+str(uid[2])+","+str(uid[3])
- # This is the default key for authentication
- key = [0xFF,0xFF,0xFF,0xFF,0xFF,0xFF]
- # Select the scanned tag
- MIFAREReader.MFRC522_SelectTag(uid)
- # Authenticate
- status = MIFAREReader.MFRC522_Auth(MIFAREReader.PICC_AUTHENT1A, 8, key, uid)
- # Check if authenticated
- if status == MIFAREReader.MI_OK:
- pin = str(uid[0])
- if pin == self.pins[0] or pin == self.pins[1]:
- print("Access granted")
- else:
- print("Access denied")
- MIFAREReader.MFRC522_StopCrypto1()
- else:
- print "Authentication error"
- def getCodes(self):
- try:
- connection = mysql.connector.connect(host='localhost', database='zugang',user='root', password='abcD123')
- query = 'SELECT pin FROM rolle'
- cursor = connection.cursor()
- cursor.execute(query)
- records = cursor.fetchall()
- for row in records:
- self.pins.append(row[0])
- cursor.close()
- except Error as e:
- print("Error", e)
- finally:
- if(connection.is_connected()):
- connection.close()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement