-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrfid-v-1_0.ino
230 lines (188 loc) · 6.11 KB
/
rfid-v-1_0.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
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
#include <SPI.h>
#include <MFRC522.h>
#include <LiquidCrystal.h>
#define RST_PIN 9
#define SS_PIN 10
MFRC522 mfrc522(SS_PIN, RST_PIN);
LiquidCrystal lcd(7, 6, 5, 4, 3, 2);
MFRC522::MIFARE_Key key;
MFRC522::PICC_Type piccType;
const byte sector = 1;
const byte blockAddr = 4;
const byte trailerBlock = 7;
#define STATE_READ_NEXT 1
#define STATE_DISPLAY_COUNTER 2
#define STATE_RESET_COUNTER 3
#define STATE_DISPLAY_ERROR 4
const int stateDelayInMillis = 1500;
int currentState = STATE_READ_NEXT;
unsigned long currentStateSinceMillis = 0;
const char * errorInfo;
byte dataBuffer[18];
byte dataBufferSize;
const byte resetReadCounterThreshold = 3;
void setup() {
Serial.begin(9600);
// init SPI and MFRC522 library
SPI.begin(); // Init SPI bus
mfrc522.PCD_Init(); // Init MFRC522
// Prepare the key (used both as key A and as key B)
// using FFFFFFFFFFFFh which is the default at chip delivery from the factory
for (byte i = 0; i < 6; i++) {
key.keyByte[i] = 0xff;
}
// Prepare data buffer we use for read/write operations
dataBufferSize = sizeof(dataBuffer) / sizeof(dataBuffer[1]);
for(byte i = 0; i < dataBufferSize; i++) {
dataBuffer[i] = 0;
}
lcd.begin(16, 2);
}
void loop() {
//dump_picc_to_serial();
//return;
unsigned long currentMillis = millis();
switch(currentState) {
case STATE_READ_NEXT:
lcd.setCursor(0,0);
lcd.write("No RFID tag ");
lcd.setCursor(0,1);
lcd.write("detected.");
mfrc522_updateCounter();
break;
case STATE_DISPLAY_COUNTER:
if((currentMillis - currentStateSinceMillis) < stateDelayInMillis) {
lcd.setCursor(0, 0);
lcd.write("RFID scanned ");
lcd.setCursor(13, 0);
String scanCount = String(dataBuffer[0]);
lcd.write(scanCount.c_str());
lcd.setCursor(0, 1);
lcd.write("times.");
} else {
currentState = STATE_READ_NEXT;
currentStateSinceMillis = millis();
lcd.clear(); // prepare for new message
}
break;
case STATE_RESET_COUNTER:
if((currentMillis - currentStateSinceMillis) < stateDelayInMillis) {
lcd.setCursor(0, 0);
lcd.write("Reset scan");
lcd.setCursor(0, 1);
lcd.write("counter.");
} else {
currentState = STATE_READ_NEXT;
currentStateSinceMillis = millis();
lcd.clear(); // prepare for new message
}
break;
case STATE_DISPLAY_ERROR:
if((currentMillis - currentStateSinceMillis) < stateDelayInMillis) {
lcd.setCursor(0, 0);
lcd.write(errorInfo);
} else {
currentState = STATE_READ_NEXT;
currentStateSinceMillis = millis();
errorInfo = ""; // clear error msg
lcd.clear(); // prepare for new message
}
break;
default:
break; // NoP
}
}
void mfrc522_updateCounter() {
if (!mfrc522.PICC_IsNewCardPresent()) {
return;
}
if (!mfrc522.PICC_ReadCardSerial()) {
return;
}
piccType = mfrc522.PICC_GetType(mfrc522.uid.sak);
// Check for compatibility
if ( piccType != MFRC522::PICC_TYPE_MIFARE_MINI
&& piccType != MFRC522::PICC_TYPE_MIFARE_1K
&& piccType != MFRC522::PICC_TYPE_MIFARE_4K) {
currentState = STATE_DISPLAY_ERROR;
currentStateSinceMillis = millis();
errorInfo = "Invalid RFID tag";
lcd.clear(); // prepare for new message
return;
}
if(!mfrc522_auth()) {
return;
}
MFRC522::StatusCode status;
//Read data from the block
status = (MFRC522::StatusCode) mfrc522.MIFARE_Read(blockAddr, dataBuffer, &dataBufferSize);
if (status != MFRC522::STATUS_OK) {
currentState = STATE_DISPLAY_ERROR;
currentStateSinceMillis = millis();
errorInfo = "RFID read fail";
lcd.clear(); // prepare for new message
halt_picc_communication();
return;
}
dataBuffer[0]++;
boolean isReset = false;
if(dataBuffer[0] > resetReadCounterThreshold) {
dataBuffer[0] = 0;
isReset = true;
}
if(!mfrc522_auth()) {
return;
}
status = (MFRC522::StatusCode) mfrc522.MIFARE_Write(blockAddr, dataBuffer, 16);
if (status != MFRC522::STATUS_OK) {
currentState = STATE_DISPLAY_ERROR;
currentStateSinceMillis = millis();
errorInfo = "RFID write fail";
lcd.clear(); // prepare for new message
halt_picc_communication();
return;
}
if(isReset) {
currentState = STATE_RESET_COUNTER;
currentStateSinceMillis = millis();
lcd.clear(); // prepare for new message
halt_picc_communication();
return;
}
currentState = STATE_DISPLAY_COUNTER;
currentStateSinceMillis = millis();
lcd.clear(); // prepare for new message
halt_picc_communication();
}
boolean mfrc522_auth() {
// Authenticate using key A
MFRC522::StatusCode status = (MFRC522::StatusCode) mfrc522.PCD_Authenticate(MFRC522::PICC_CMD_MF_AUTH_KEY_A, trailerBlock, &key, &(mfrc522.uid));
if (status != MFRC522::STATUS_OK) {
currentState = STATE_DISPLAY_ERROR;
currentStateSinceMillis = millis();
errorInfo = "RFID auth fail";
lcd.clear(); // prepare for new message
return false;
}
return true;
}
/**
* Dump EEPROM to serial.
**/
void dump_picc_to_serial() {
if ( ! mfrc522.PICC_IsNewCardPresent()) {
return;
}
if ( ! mfrc522.PICC_ReadCardSerial()) {
return;
}
mfrc522.PICC_DumpToSerial(&(mfrc522.uid));
}
/**
* After this was executed you are required to remove the PICC from PCD before
* next read can happen.
**/
void halt_picc_communication() {
mfrc522.PICC_HaltA();
mfrc522.PCD_StopCrypto1();
}