Skip to content

Commit 4bca03d

Browse files
committed
feat: authentication with rfid
1 parent 6e08356 commit 4bca03d

File tree

6 files changed

+244
-0
lines changed

6 files changed

+244
-0
lines changed
+28
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
#include <SPI.h>
2+
#include <MFRC522.h>
3+
#define RST_PIN 9
4+
#define SS_PIN 10
5+
MFRC522 mfrc522(SS_PIN, RST_PIN);
6+
void setup() {
7+
Serial.begin(9600);
8+
while (!Serial);
9+
SPI.begin();
10+
//Enhance the MFRC522 Receiver Gain to maximum value of some 48 dB
11+
mfrc522.PCD_SetRegisterBitMask(mfrc522.RFCfgReg, (0x07<<4));
12+
mfrc522.PCD_Init();
13+
delay(4);
14+
mfrc522.PCD_DumpVersionToSerial();
15+
Serial.println(F("DISPLAYING UID, SAK, TYPE, AND DATA BLOCKS:"));
16+
}
17+
void loop(){
18+
if(!mfrc522.PICC_IsNewCardPresent()){
19+
return;
20+
}
21+
if(!mfrc522.PICC_ReadCardSerial()){
22+
return;
23+
}
24+
/*
25+
Dump debug info about the card. Worry not. PICC_HaltA() will be automatically called at the end.
26+
*/
27+
mfrc522.PICC_DumpToSerial(&(mfrc522.uid));
28+
}

reading-rfid.ino/reading-rfid.ino

+58
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
#include <SPI.h>
2+
#include <MFRC522.h>
3+
#define RST_PIN 9
4+
#define SS_PIN 10
5+
MFRC522 mfrc522(SS_PIN, RST_PIN);
6+
MFRC522::MIFARE_Key key;
7+
MFRC522::StatusCode card_status;
8+
void setup(){
9+
Serial.begin(9600);
10+
SPI.begin();
11+
mfrc522.PCD_Init();
12+
Serial.println(F("PCD Ready!"));
13+
}
14+
void loop(){
15+
for (byte i = 0; i < 6; i++){
16+
key.keyByte[i] = 0xFF;
17+
}
18+
if(!mfrc522.PICC_IsNewCardPresent()){
19+
return;
20+
}
21+
if(!mfrc522.PICC_ReadCardSerial()){
22+
return;
23+
}
24+
25+
Serial.println(F("\n*** Balance on the PICC ***\n"));
26+
String balance = readBytesFromBlock();
27+
Serial.println(balance);
28+
Serial.println(F("\n***************************\n"));
29+
delay(1000);
30+
31+
mfrc522.PICC_HaltA();
32+
mfrc522.PCD_StopCrypto1();
33+
}
34+
String readBytesFromBlock(){
35+
byte blockNumber = 4;
36+
37+
card_status = mfrc522.PCD_Authenticate(MFRC522::PICC_CMD_MF_AUTH_KEY_A, blockNumber, &key, &(mfrc522.uid));
38+
if(card_status != MFRC522::STATUS_OK){
39+
Serial.print(F("Authentication failed: "));
40+
Serial.println(mfrc522.GetStatusCodeName(card_status));
41+
return;
42+
}
43+
byte arrayAddress[18];
44+
byte buffersize = sizeof(arrayAddress);
45+
card_status = mfrc522.MIFARE_Read(blockNumber, arrayAddress, &buffersize);
46+
if(card_status != MFRC522::STATUS_OK){
47+
Serial.print(F("Reading failed: "));
48+
Serial.println(mfrc522.GetStatusCodeName(card_status));
49+
return;
50+
}
51+
52+
String value = "";
53+
for (uint8_t i = 0; i < 16; i++){
54+
value += (char)arrayAddress[i];
55+
}
56+
value.trim();
57+
return value;
58+
}

secure-rfid.ino/authentication.py

+37
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
#!/usr/bin/env python3
2+
3+
import serial
4+
5+
ser = serial.Serial(
6+
port='COM21',
7+
baudrate=9600,
8+
parity=serial.PARITY_NONE,
9+
stopbits=serial.STOPBITS_ONE,
10+
bytesize=serial.EIGHTBITS,
11+
timeout=1
12+
)
13+
14+
ser.flush()
15+
16+
def readFile(uuid):
17+
print(f"[py:log] Checking for UUID: |{uuid}|")
18+
with open("./tags.txt", "r") as f:
19+
for line in f.readlines():
20+
if line.rstrip().lstrip() == uuid:
21+
return True
22+
23+
return False
24+
25+
26+
27+
28+
if __name__ == '__main__':
29+
while True:
30+
if ser.in_waiting > 0:
31+
line = ser.readline().decode('utf-8').rstrip()
32+
print(line)
33+
if line[0:14] == "[check-access]":
34+
print("Checking Access")
35+
isAllowed = readFile(line[14:].rstrip().lstrip())
36+
response = "granted" if isAllowed else "denied"
37+
ser.write(response.encode())

secure-rfid.ino/readme.md

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
# Secure RFID
2+
3+
Goal of this project is to authenticate a card, when we have tags's uuid in our database (File in version 1) the the card is authorized otherwise it's not.
4+
5+
In v2 we will use ``sqlite`` database and it's comming soon.

secure-rfid.ino/secure-rfid.ino.ino

+114
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,114 @@
1+
#include <SPI.h>
2+
3+
#include <MFRC522.h>
4+
5+
#define SS_PIN 10
6+
#define RST_PIN 9
7+
MFRC522 rfid(SS_PIN, RST_PIN);
8+
MFRC522::MIFARE_Key key;
9+
byte nuidPICC[4];
10+
char ACCESS_GRANTED[] = "granted";
11+
12+
const int SUCCESS_PIN = 7;
13+
const int ERROR_PIN = 8;
14+
const int BUZZER_PIN = 6;
15+
16+
void setup() {
17+
Serial.begin(9600);
18+
SPI.begin();
19+
rfid.PCD_Init();
20+
pinMode(SUCCESS_PIN, OUTPUT);
21+
pinMode(ERROR_PIN, OUTPUT);
22+
pinMode(BUZZER_PIN, OUTPUT);
23+
24+
Serial.println(F("READING THE CARD UNIQUE ID:"));
25+
for (byte i = 0; i < 6; i++) {
26+
key.keyByte[i] = 0xFF;
27+
}
28+
}
29+
void loop() {
30+
if (!rfid.PICC_IsNewCardPresent()) {
31+
return;
32+
}
33+
if (!rfid.PICC_ReadCardSerial()) {
34+
return;
35+
}
36+
if (rfid.uid.uidByte[0] != nuidPICC[0] ||
37+
rfid.uid.uidByte[1] != nuidPICC[1] ||
38+
rfid.uid.uidByte[2] != nuidPICC[2] ||
39+
rfid.uid.uidByte[3] != nuidPICC[3]) {
40+
for (byte i = 0; i < 4; i++) {
41+
nuidPICC[i] = rfid.uid.uidByte[i];
42+
}
43+
Serial.print(F("[check-access]"));
44+
printHex(rfid.uid.uidByte, rfid.uid.size);
45+
Serial.println(F("\n"));
46+
Serial.setTimeout(500);
47+
String res = Serial.readString();
48+
res.trim();
49+
Serial.print("|");
50+
Serial.print(res);
51+
Serial.println("|");
52+
if(res.equals(ACCESS_GRANTED)){
53+
Serial.println("ACCESS GRANTED");
54+
access_granted();
55+
}else {
56+
Serial.println("ACCESS DENIED");
57+
access_denied();
58+
}
59+
} else {
60+
Serial.println(F("This car was lastly detected ."));
61+
buzz("once");
62+
}
63+
/*
64+
* Halt PICC
65+
* Stop encryption on PCD
66+
*/
67+
rfid.PICC_HaltA();
68+
rfid.PCD_StopCrypto1();
69+
}
70+
void printHex(byte * buffer, byte bufferSize) {
71+
for (byte i = 0; i < bufferSize; i++) {
72+
Serial.print(buffer[i] < 0x10 ? " 0" : " ");
73+
Serial.print(buffer[i], HEX);
74+
}
75+
}
76+
77+
void access_granted() {
78+
digitalWrite(SUCCESS_PIN, HIGH);
79+
digitalWrite(ERROR_PIN, LOW);
80+
buzz("twice");
81+
delay(1000);
82+
digitalWrite(SUCCESS_PIN, LOW);
83+
}
84+
85+
void access_denied() {
86+
digitalWrite(ERROR_PIN, HIGH);
87+
digitalWrite(SUCCESS_PIN, LOW);
88+
buzz("long");
89+
digitalWrite(ERROR_PIN, LOW);
90+
}
91+
92+
void buzz(String type){
93+
if(type.equals("once")) {
94+
analogWrite(BUZZER_PIN, 60);
95+
delay(60);
96+
analogWrite(BUZZER_PIN, 0);
97+
}else if(type.equals("twice")){
98+
analogWrite(BUZZER_PIN, 60);
99+
delay(60);
100+
analogWrite(BUZZER_PIN, 0);
101+
delay(60);
102+
analogWrite(BUZZER_PIN, 60);
103+
delay(60);
104+
analogWrite(BUZZER_PIN, 0);
105+
}else if(type.equals("long")){
106+
analogWrite(BUZZER_PIN, 60);
107+
delay(1000);
108+
analogWrite(BUZZER_PIN, 0);
109+
} else {
110+
analogWrite(BUZZER_PIN, 60);
111+
delay(60);
112+
analogWrite(BUZZER_PIN, 0);
113+
}
114+
}

secure-rfid.ino/tags.txt

+2
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
4B 77 D0 14
2+
E6 77 65 AC

0 commit comments

Comments
 (0)