forked from dirkx/makerspaceleiden-payment-node-7segments
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRFID.cpp
More file actions
78 lines (66 loc) · 2.02 KB
/
RFID.cpp
File metadata and controls
78 lines (66 loc) · 2.02 KB
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
#include <SPI.h>
#include <MFRC522.h>
#include "global.h"
#include "log.h"
SPIClass * spi;
MFRC522_SPI * spiDevice;
MFRC522 * mfrc522;
char tag[128];
unsigned int rfid_scans = 0, rfid_miss = 0;
void setupRFID()
{
unsigned char mac[6];
const unsigned char RFID_SCLK = 18;
const unsigned char RFID_MISO = 19;
const unsigned char RFID_MOSI = 23;
unsigned char RFID_CS = 5; // default VSPI wiring
unsigned char RFID_RESET = 21; // these two pins swapped on the older beer-node.
// unsigned char RFID_IRQ = 22;
// 3C:71:BF:43:0F:E4 - oldest beer node with funny wiring.
// Later units are as per above.
WiFi.macAddress(mac);
if (mac[3] == 0x43 && mac[4] == 0x0F && mac[5] == 0xE4) {
RFID_RESET = 22;
// RFID_IRQ = 21;
RFID_CS = 15;
};
spi = new SPIClass(VSPI);
spi->begin(RFID_SCLK, RFID_MISO, RFID_MOSI, RFID_CS);
spiDevice = new MFRC522_SPI(RFID_CS, RFID_RESET, spi);
mfrc522 = new MFRC522(spiDevice);
mfrc522->PCD_Init();
Serial.print("RFID Scanner init.");
mfrc522->PCD_DumpVersionToSerial(); // Show details of PCD - MFRC522 Card Reader details
}
int loopRFID() {
if ( ! mfrc522->PICC_IsNewCardPresent()) {
return 0;
}
if ( ! mfrc522->PICC_ReadCardSerial()) {
Log.println("Bad read (was card removed too quickly ? )");
rfid_miss++;
return 0;
}
if (mfrc522->uid.size < 3) {
rfid_miss++;
Log.println("Bad card (size tool small)");
return 0;
};
// We're somewhat strict on the parsing/what we accept; as we use it unadultared in the URL.
//
if (mfrc522->uid.size > sizeof(mfrc522->uid.uidByte)) {
rfid_miss++;
Log.println("Too large a card id size. Ignoring.)");
return 0;
}
memset(tag, 0, sizeof(tag));
for (int i = 0; i < mfrc522->uid.size; i++) {
char buff[5]; // 3 digits, dash and \0.
snprintf(buff, sizeof(buff), "%s%d", i ? "-" : "", mfrc522->uid.uidByte[i]);
strncat(tag, buff, sizeof(tag) - 1);
};
Log.println("Good scan");
rfid_scans++;
mfrc522->PICC_HaltA();
return 1;
}