-
Notifications
You must be signed in to change notification settings - Fork 0
/
RFID-Schrein.ino
153 lines (129 loc) · 3.48 KB
/
RFID-Schrein.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
#include <SPI.h>
#include <MFRC522.h>
#define SS_PIN 10
#define RST_PIN 9
MFRC522 mfrc522(SS_PIN, RST_PIN); // Create MFRC522 instance.
MFRC522::MIFARE_Key key;
MFRC522::StatusCode status;
void setup()
{
Serial.begin(9600); // Initiate a serial communication
SPI.begin(); // Initiate SPI bus
mfrc522.PCD_Init(); // Initiate MFRC522
Serial.println("Approximate your card to the reader...");
Serial.println();
}
void loop()
{
//default key for fresh card
for (byte i = 0; i < 6; i++) key.keyByte[i] = 0xFF;
//desired EE key:
/*key.keyByte[0] = 0x36;
key.keyByte[1] = 0x5A;
key.keyByte[2] = 0xC4;
key.keyByte[3] = 0x22;
key.keyByte[4] = 0xFE;
key.keyByte[5] = 0x35;*/
// Look for new cards
if ( ! mfrc522.PICC_IsNewCardPresent())
{
return;
}
// Select one of the cards
if ( ! mfrc522.PICC_ReadCardSerial())
{
return;
}
String uuid = "";
uuid = readUUID();
if (uuid.substring(1) == "43 0D FE 27") //change here the UID of the card/cards that you want to give access
{
Serial.println("Authorized access - correct card");
}
else {
Serial.println("Access denied - unknown card");
delay(2000);
return;
}
byte buffer[18];
byte block;
byte len;
block = 16;
len = 18;
decryptBlock(block, key);
int counter;
counter = readCounterValue(block, buffer, len);
Serial.print("Counter before:");
Serial.println(counter);
//Reset to start Values
//buffer[0] = 0;
//buffer[1] = 0x02;
buffer[0]++;
writeToBlock(block, buffer);
counter = readCounterValue(block, buffer, len);
Serial.print("Counter after:");
Serial.println(counter);
Serial.println(F("\n**End**\n"));
delay(1000);
mfrc522.PICC_HaltA();
mfrc522.PCD_StopCrypto1();
}
int readCounterValue(byte block, byte *buffer, byte len)
{
status = mfrc522.MIFARE_Read(block, buffer, &len);
if (status != MFRC522::STATUS_OK) {
Serial.print(F("Reading failed: "));
Serial.println(mfrc522.GetStatusCodeName(status));
return;
}
dump_byte_array(buffer, len);
return (int)buffer[0];
}
void decryptBlock(byte block, MFRC522::MIFARE_Key key)
{
status = mfrc522.PCD_Authenticate(MFRC522::PICC_CMD_MF_AUTH_KEY_A, block, &key, &(mfrc522.uid));
if (status != MFRC522::STATUS_OK) {
Serial.print(F("Authentication failed: "));
Serial.println(mfrc522.GetStatusCodeName(status));
return;
}
}
String readUUID()
{
//Show UID on serial monitor
Serial.print("UID tag :");
String content= "";
byte letter;
for (byte i = 0; i < mfrc522.uid.size; i++)
{
Serial.print(mfrc522.uid.uidByte[i] < 0x10 ? " 0" : " ");
Serial.print(mfrc522.uid.uidByte[i], HEX);
content.concat(String(mfrc522.uid.uidByte[i] < 0x10 ? " 0" : " "));
content.concat(String(mfrc522.uid.uidByte[i], HEX));
}
Serial.println();
Serial.print("Message : ");
content.toUpperCase();
return content;
}
void writeToBlock(byte block, byte buff[])
{
// Write block
status = mfrc522.MIFARE_Write(block, buff, 16);
if (status != MFRC522::STATUS_OK) {
Serial.print(F("MIFARE_Write() failed: "));
Serial.println(mfrc522.GetStatusCodeName(status));
return;
}
else Serial.println(F("MIFARE_Write() success!"));
}
/*
* Helper routine to dump a byte array as hex values to Serial.
*/
void dump_byte_array(byte *buffer, byte bufferSize) {
for (byte i = 0; i < bufferSize; i++) {
Serial.print(buffer[i] < 0x10 ? " 0" : " ");
Serial.print(buffer[i], HEX);
}
Serial.println("");
}