-
Notifications
You must be signed in to change notification settings - Fork 0
/
RFID_Project.ino
156 lines (119 loc) · 3 KB
/
RFID_Project.ino
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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
#include <MFRC522.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>
#include <SPI.h>
#define OLED_RESET 4
Adafruit_SSD1306 display(OLED_RESET);
#define SS_PIN 10
#define RST_PIN 9
MFRC522 rfid(SS_PIN, RST_PIN);
MFRC522::MIFARE_Key key;
int code[] = {153,186,223,176};
int codeRead = 0;
String uidString;
void setup() {
Serial.begin(9600);
SPI.begin();
rfid.PCD_Init();
display.begin(SSD1306_SWITCHCAPVCC, 0x3C);
display.clearDisplay();
display.display();
display.setTextColor(WHITE);
display.setTextSize(2);
display.setCursor(10,0);
display.print("RFID Lock");
display.display();
}
void loop() {
if( rfid.PICC_IsNewCardPresent())
{
readRFID();
}
delay(100);
}
void readRFID()
{
rfid.PICC_ReadCardSerial();
Serial.print(F("\nPICC type: "));
MFRC522::PICC_Type piccType = rfid.PICC_GetType(rfid.uid.sak);
Serial.println(rfid.PICC_GetTypeName(piccType));
if (piccType != MFRC522::PICC_TYPE_MIFARE_MINI &&
piccType != MFRC522::PICC_TYPE_MIFARE_1K &&
piccType != MFRC522::PICC_TYPE_MIFARE_4K) {
Serial.println(F("Your tag is not of type MIFARE Classic."));
return;
}
clearUID();
Serial.println("Scanned PICC's UID:");
printDec(rfid.uid.uidByte, rfid.uid.size);
uidString = String(rfid.uid.uidByte[0])+" "+String(rfid.uid.uidByte[1])+" "+String(rfid.uid.uidByte[2])+ " "+String(rfid.uid.uidByte[3]);
printUID();
int i = 0;
boolean match = true;
while(i<rfid.uid.size)
{
if(!(rfid.uid.uidByte[i] == code[i]))
{
match = false;
}
i++;
}
if(match)
{
Serial.println("\nI know this card!");
printUnlockMessage();
}else
{
Serial.println("\nUnknown Card");
}
rfid.PICC_HaltA();
rfid.PCD_StopCrypto1();
}
void printDec(byte *buffer, byte bufferSize) {
for (byte i = 0; i < bufferSize; i++) {
Serial.print(buffer[i] < 0x10 ? " 0" : " ");
Serial.print(buffer[i], DEC);
}
}
void clearUID()
{
display.setTextColor(BLACK);
display.setTextSize(1);
display.setCursor(30,20);
display.print(uidString);
display.display();
}
void printUID()
{
display.setTextColor(WHITE);
display.setTextSize(1);
display.setCursor(0,20);
display.print("UID: ");
display.setCursor(30,20);
display.print(uidString);
display.display();
}
void printUnlockMessage()
{
display.display();
display.setTextColor(BLACK);
display.setTextSize(2);
display.setCursor(10,0);
display.print("RFID Lock");
display.display();
display.setTextColor(WHITE);
display.setTextSize(2);
display.setCursor(10,0);
display.print("Unlocked");
display.display();
delay(2000);
display.setTextColor(BLACK);
display.setTextSize(2);
display.setCursor(10,0);
display.print("Unlocked");
display.setTextColor(WHITE);
display.setTextSize(2);
display.setCursor(10,0);
display.print("RFID Lock");
display.display();
}