-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathRFID_Logger.ino
464 lines (389 loc) · 12.1 KB
/
RFID_Logger.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
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
/*
MIT License
Copyright(c) 2020 gojimmypi
Permission is hereby granted, free of charge, to any person obtaining a copy
of this softwareand associated documentation files(the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and /or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions :
The above copyright noticeand this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
*/
//
// see: https://github.com/miguelbalboa/rfid
//
#include "GlobalDefine.h"
#include <vector>
#include <stdlib.h>
#include <SPI.h>
#include <MFRC522.h>
#ifdef ARDUINO_ARCH_ESP8266
#include <WiFiClientSecure.h>
#endif
#ifdef ARDUINO_ARCH_ESP32
#include <WiFiClientSecure.h>
#endif
#ifdef ARDUINO_SAMD_MKRWIFI1010
#include <WiFiNINA.h>
#endif
#include "WiFiHelper.h"
#include "sslHelper.h"
#include "htmlHelper.h"
// Define our SPI connection and parameters for the RFID reader (VSPI)
#if defined(ARDUINO_ARCH_ESP32) || defined(ARDUINO_ARCH_ESP8266)
#define SS_PIN 21
#define RST_PIN 22
#endif
#if defined(ARDUINO_SAMD_MKRWIFI1010)
#define SS_PIN 7
#define RST_PIN 6
#endif
#define SIZE_BUFFER 18
#define MAX_SIZE_BLOCK 16
// Initialize the Wi-Fi client library
// WiFiClient client; // this is the non-TLS/SSL one!
// Use WiFiClientSecure class to create TLS connection
WIFI_CLIENT_CLASS client;
// Initialize our FRID reader with a single instance:
MFRC522 rfid(SS_PIN, RST_PIN);
// Initialize our RFID key (new CARDSN/UID values found here)
MFRC522::MIFARE_Key key;
// Init array that will store new NUID
byte nuidPICC[4];
void WebServerConnect(int retry) {
while (!client.connect(SECRET_APP_HOST, APP_HTTPS_PORT)) {
if (retry == 0) {
Serial.println(F("client.connect failed; check firewall on receiver. giving up..."));
break;
}
// TODO what if the web server is down? we shouldn't wait here forever...
Serial.println(F("client.connect failed; check firewall on receiver"));
Serial.print(F("IP address="));
Serial.println(WiFi.localIP());
Serial.print(F("MAC address="));
Serial.println(wifiMacAddress());
for (int i = retry; i > 0; i--)
{
Serial.print(F("."));
Serial.print(i);
delay(1000);
}
Serial.println();
Serial.println(F("Trying again!"));
}
}
bool WiFiConnected() {
return (WiFi.status() == WL_CONNECTED);
}
typedef struct ItemUID {
String UID = "";
String MSG = "";
unsigned long timestamp;
bool sent = false;
};
std::vector<ItemUID> QueueOfUID;
ulong SendAttempts = 0;
int SendQueue() {
// WiFiStart occurs in SaveUID()
if (QueueOfUID.empty()) {
Serial.println(F("QueueOfUID is empty!"));
return 0;
}
SendAttempts++;
if (WiFiConnected()) {
WebServerConnect(0); // zero retries, we check again on the next loop
if (!client.connected()) {
Serial.println(F("SaveUID when wifi client.connected is false; check firewall on receiver"));
Serial.print(F("IP address="));
Serial.println(WiFi.localIP());
return 2;
}
Serial.println(F(""));
Serial.print("Items to check: ");
Serial.println(QueueOfUID.size());
bool HasUnsentItems = false;
bool SentAnItem = false; // sincce web communications takes some time, we'll only send one at a time
int thisResult = -1;
for (uint i = 0; i < QueueOfUID.size(); i++)
{
if (QueueOfUID[i].sent) {
Serial.print(F("Item already sent: "));
Serial.print(i);
Serial.print(F("; timestamp: "));
Serial.println(QueueOfUID[i].timestamp);
}
else {
if (SentAnItem) {
// we already sent an item, but another was found. if it hasn't been sent, we'll do it later
if (QueueOfUID[i].sent == false) {
Serial.println("Skipping another unsent item...");
HasUnsentItems = true;
break;
}
}
else {
Serial.print(F("Sending UID Item "));
Serial.print(i);
Serial.print(F("; timestamp: "));
Serial.println(QueueOfUID[i].timestamp);
String url = String(SECRET_APP_PATH)
+ F("?UID=") + QueueOfUID[i].UID
+ F("&MAC=") + wifiMacAddress()
+ F("&IP=") + WiFi.localIP()
+ F("&MSG=") + QueueOfUID[i].MSG; // reminder that IIS will return a 302 (moved) for default.aspx that points to default :/
String thisRequest = HTML_RequestText(url);
String thisMovedRequestURL = "";
thisResult = HTML_SendRequestFollowMove(&client, thisRequest, thisMovedRequestURL);
if (thisResult == 0) {
// TODO check if successful, for now, assume it was
// HasUnsentItems = true // if it fails we have an unsent item
Serial.println(F("Item successfully sent!"));
QueueOfUID[i].sent = true;
SentAnItem = true;
}
else {
Serial.println(F("Item NOT sent!"));
HasUnsentItems = true;
}
client.flush();
delay(100);
client.stop();
}
} // else not sent
if (HasUnsentItems) {
// if we can't send one, we probably can't send any others, so give up
Serial.println(F("Giving up..."));
break;
}
} // for
if (HasUnsentItems) {
Serial.println(F("There are still unsent items!"));
}
else {
Serial.println(F("Clearing unsent item list..."));
QueueOfUID.clear();
Serial.println(F("Cleared unsent item list!"));
SendAttempts = 0;
}
}
else {
Serial.print(F("Exiting, WiFi not connected; item not saved; "));
Serial.print(String(QueueOfUID.size()));
Serial.println(F(" Items queued"));
return 3;
}
return 0;
}
int SaveUID(String thisUID, String thisMessage) {
if (thisUID) {
ItemUID newItem;
newItem.UID = thisUID;
newItem.MSG = thisMessage;
newItem.timestamp = millis();
newItem.sent = false;
QueueOfUID.push_back(newItem);
Serial.println(F(""));
Serial.print(F("Added item "));
Serial.print(newItem.timestamp);
Serial.print(F("; New Queue Size : "));
Serial.println(QueueOfUID.size());
// we'll only try to turn on WiFi when we know we have an item to send
if (!WiFiConnected()) {
WiFiStart(IS_EAP);
}
// it's unlikely that we'll be able to connect yet, but let's try:
SendQueue();
return 0;
}
else {
return 1;
}
}
#ifdef _MSC_VER
#pragma region helpers
#endif
String UID_Hex(byte* buffer, byte bufferSize) {
String res = "";
for (byte i = 0; i < bufferSize; i++) {
res += HEX_CHARS[(buffer[i] >> 4) & 0xF];
res += HEX_CHARS[buffer[i] & 0xF];
}
return res;
}
/**
* Helper routine to dump a byte array as hex values to Serial.
*/
void printHex(byte* buffer, byte bufferSize) {
for (byte i = 0; i < bufferSize; i++) {
Serial.print(buffer[i] < 0x10 ? " 0" : " ");
Serial.print(buffer[i], HEX);
}
}
/**
* Helper routine to dump a byte array as dec values to Serial.
*/
void printDec(byte* buffer, byte bufferSize) {
for (byte i = 0; i < bufferSize; i++) {
Serial.print(buffer[i] < 0x10 ? " 0" : " ");
Serial.print(buffer[i], DEC);
}
}
#ifdef _MSC_VER
#pragma endregion helpers
#endif
void setup() {
Serial.begin(115200);
while (!Serial) {
delay(10);
}
Serial.println(F("Hello RFID_Logger!"));
SPI.begin(); // Init SPI bus
rfid.PCD_Init(); // Init MFRC522
for (byte i = 0; i < 6; i++) {
key.keyByte[i] = 0xFF;
}
Serial.println(F("This code scans the MIFARE Classsic NUID."));
Serial.print(F("Using the following key:"));
printHex(key.keyByte, MFRC522::MF_KEY_SIZE);
SaveUID("00000000", "Startup"); // save a marker at startup time
// some reconnection tests...
//delay(1000);
//SaveUID("00000001", "Startup"); // save a marker at startup time
//delay(5000);
//SaveUID("00000005", "Startup"); // save a marker at startup time
//delay(20000);
//SaveUID("00000020", "Startup"); // save a marker at startup time
//delay(60000);
//SaveUID("00000060", "Startup"); // save a marker at startup time
//delay(120000);
//SaveUID("00000120", "Startup"); // save a marker at startup time
//delay(240000);
//SaveUID("00000240", "Startup"); // save a marker at startup time
//delay(1000000);
//SaveUID("00001000", "Startup"); // save a marker at startup time
Serial.println(F("Setup complete: awaiting card..."));
}
bool IsCardReady() {
if (!rfid.PICC_IsNewCardPresent()) {
return false;
}
if (!rfid.PICC_ReadCardSerial()) {
RFID_DEBUG_PRINT(F("PICC_IsNewCardPresent but not PICC_ReadCardSerial."));
return false;
}
MFRC522::PICC_Type piccType = rfid.PICC_GetType(rfid.uid.sak);
if (piccType != MFRC522::PICC_TYPE_MIFARE_MINI &&
piccType != MFRC522::PICC_TYPE_MIFARE_1K &&
piccType != MFRC522::PICC_TYPE_MIFARE_4K) {
RFID_DEBUG_PRINT(F("Card type not valid."));
return false;
}
return true;
}
// these counters are only general approximations, not accurate time.
int minutes_uptime = 0;
int hours_uptime = 0;
int days_uptime = 0;
int this_ms_counter = 0;
int save_result = 0;
// wait [ms_count] milliseconds; attempt to send data every minute
void wait_ms(int ms_count) {
this_ms_counter++;
unsigned long CodeStart = millis();
bool SentQueue = false; // we'll keep track of whether we attempted a slow operation, like talking to the web server
if (this_ms_counter >= (60000 / ms_count) ) {
minutes_uptime++;
this_ms_counter = 0;
TIMER_DEBUG_PRINTLN("Uptime: " + String(days_uptime) + " days; "
+ String(hours_uptime) + " hours; "
+ String(minutes_uptime) + " minutes. "
+ "Queue Size: " + String(QueueOfUID.size()));
// code that runs every minute
// once per minute, we'll need to try to connect again if the queue has items to be sent
if (QueueOfUID.empty()) {
}
else {
// TODO for low power operations, we probably don't want to try this every minute
Serial.print(F("Send attempt: "));
Serial.println(SendAttempts);
SendQueue();
SentQueue = true;
}
}
// keep track of hours
if (minutes_uptime >= 60) {
hours_uptime++;
minutes_uptime = 0;
// any hourly code would go here
}
// keep track of days
if (hours_uptime >= 24) {
days_uptime++;
hours_uptime = 0;
// any daily code would go here
String dailyCheckin = "Check-in #" + String(days_uptime);
SaveUID("00000000", dailyCheckin); // save a marker
}
// CodeDuration is typically 0, as the above will take less than a millisecond, unless data sent to web server
unsigned long CodeDuration = millis() - CodeStart;
if (SentQueue) {
TIMER_DEBUG_PRINT(F("Code Duration: "));
TIMER_DEBUG_PRINTLN(CodeDuration);
}
if ((CodeDuration >= 0) && (CodeDuration < ms_count)) {
int thisDelay = ms_count - CodeDuration;
if (SentQueue || thisDelay > -1) {
TIMER_DEBUG_PRINT(F("waiting: "));
TIMER_DEBUG_PRINTLN(thisDelay);
}
delay(thisDelay);
}
else {
// no delay, we already spend more time in code than the delay!
TIMER_DEBUG_PRINT(F("No delay! Duration="));
TIMER_DEBUG_PRINTLN(CodeDuration);
this_ms_counter = this_ms_counter + CodeDuration;
}
}
void loop() {
// check to see if we have a card
if (IsCardReady()) {
Serial.print(F("Detected UID: "));
printHex(rfid.uid.uidByte, rfid.uid.size);
Serial.println();
Serial.print(F("aka: "));
String detectedUID = UID_Hex(rfid.uid.uidByte, rfid.uid.size);
Serial.println(detectedUID);
save_result = SaveUID(detectedUID, F("Detected"));
if (save_result == 0) {
Serial.println(F("Data queued. Awaiting next card..."));
}
else {
Serial.println(F("Error! Data NOT queued. Awaiting next card..."));
}
// Halt PICC
rfid.PICC_HaltA();
// Stop encryption on PCD
rfid.PCD_StopCrypto1();
}
else {
wait_ms(100); // data sent while waiting, too
}
// check to see of we have any items still queued to send
if (QueueOfUID.empty()) {
if (WiFiConnected()) {
Serial.println(F("WiFi.disconnect"));
WiFi.disconnect();
}
}
else {
}
}