-
Notifications
You must be signed in to change notification settings - Fork 0
/
Rfid2Base.py
95 lines (73 loc) · 2.44 KB
/
Rfid2Base.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
#!/usr/bin/env python
# -*- coding: utf8 -*-
# Version modifiee de la librairie https://github.com/mxgxw/MFRC522-python
import RPi.GPIO as GPIO
import MFRC522
import signal
import mysql.connector
from mysql.connector import Error
from time import *
import time
db = mysql.connector.connect(
host="ipduserver",
user="nomduuser",
password="motdepasse",
database="nomdelabase")
GPIO.setmode(GPIO.BOARD)
GPIO.setwarnings(False)
GPIO.setup(18,GPIO.OUT)
continue_reading = True
# Fonction qui arrete la lecture proprement
def end_read(signal,frame):
global continue_reading
print ("Lecture terminée")
continue_reading = False
GPIO.cleanup()
# Hook the SIGINT
signal.signal(signal.SIGINT, end_read)
# Create an object of the class MFRC522
MIFAREReader = MFRC522.MFRC522()
# Welcome message
print "Welcome to the Raspberry Pi RFID Login System"
print "Passer le tag RFID a lire"
print "Presser Ctrl-C pour quitter."
# This loop keeps checking for chips. If one is near it will get the UID and authenticate
while continue_reading:
# Detecter les tags
(status,TagType) = MIFAREReader.MFRC522_Request(MIFAREReader.PICC_REQIDL)
# If we have the UID, continue
if status == MIFAREReader.MI_OK:
(status,uid) = MIFAREReader.MFRC522_Anticoll()
uid = str(uid[0])+"."+str(uid[1])+"."+str(uid[2])+"."+str(uid[3])
print uid
# Light a LED for 1 second
GPIO.output(18,GPIO.HIGH)
sleep(1)
GPIO.output(18,GPIO.LOW)
sleep(1)
cur = db.cursor()
# Read time and date
currentTime = strftime("%Y-%m-%d %H:%M:%S", localtime())
# Insert every login into database
try:
try:
cur.execute("""INSERT INTO log_uid (id, uid, date) VALUES (%s, %s, %s)""",('NULL', uid, currentTime))
db.commit()
except Error as e :
print(e)
finally:
print "Succes!"
cur.close()
# 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:
MIFAREReader.MFRC522_Read(8)
MIFAREReader.MFRC522_StopCrypto1()
else:
pass
db.close()