Guest User

Untitled

a guest
Nov 11th, 2019
148
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.11 KB | None | 0 0
  1. #!/usr/bin/env python
  2. # -*- coding: utf8 -*-
  3.  
  4. import RPi.GPIO as GPIO
  5. import MFRC522
  6. import signal
  7. import MySQLdb
  8. # Verbindung zum MySQL Server herstellen
  9. db = MySQLdb.connect(host="localhost",  # your host, usually localhost
  10.                      user="root",  # your username
  11.                      passwd="abcD123",  # your password
  12.                      db="lf6")  # name of the data base
  13. cur = db.cursor()
  14.  
  15. continue_reading = True
  16.  
  17. # Capture SIGINT for cleanup when the script is aborted
  18. def end_read(signal,frame):
  19.     global continue_reading
  20.     print "Ctrl+C captured, ending read."
  21.     continue_reading = False
  22.     GPIO.cleanup()
  23.  
  24. # Hook the SIGINT
  25. signal.signal(signal.SIGINT, end_read)
  26.  
  27. # Create an object of the class MFRC522
  28. MIFAREReader = MFRC522.MFRC522()
  29.  
  30. # Welcome message
  31. print "Welcome to the MFRC522 data read example"
  32. print "Press Ctrl-C to stop."
  33.  
  34. # This loop keeps checking for chips. If one is near it will get the UID and authenticate
  35. while continue_reading:
  36.    
  37.     # Scan for cards    
  38.     (status,TagType) = MIFAREReader.MFRC522_Request(MIFAREReader.PICC_REQIDL)
  39.  
  40.     # If a card is found
  41.     if status == MIFAREReader.MI_OK:
  42.         print "Card detected"
  43.    
  44.     # Get the UID of the card
  45.     (status,uid) = MIFAREReader.MFRC522_Anticoll()
  46.  
  47.     # If we have the UID, continue
  48.     if status == MIFAREReader.MI_OK:
  49.        
  50.         cur.execute("SELECT * FROM rfid WHERE rfid = '" + str(uid[0]) +  str(uid[1]) +  str(uid[2]) +  str(uid[3]) + "'")
  51.         if(cur.rowcount == 0):
  52.             print "not welcome"
  53.         else:
  54.             print "welcome"
  55.         # This is the default key for authentication
  56.         key = [0xFF,0xFF,0xFF,0xFF,0xFF,0xFF]
  57.        
  58.         # Select the scanned tag
  59.         MIFAREReader.MFRC522_SelectTag(uid)
  60.  
  61.         # Authenticate
  62.         status = MIFAREReader.MFRC522_Auth(MIFAREReader.PICC_AUTHENT1A, 8, key, uid)
  63.  
  64.         # Check if authenticated
  65.         if status == MIFAREReader.MI_OK:
  66.             MIFAREReader.MFRC522_Read(8)
  67.             MIFAREReader.MFRC522_StopCrypto1()
  68.         else:
  69.             print "Authentication error"
Advertisement
Add Comment
Please, Sign In to add comment