1 | /*
|
2 | * ----------------------------------------------------------------------
|
3 | * Example program showing how to read new NUID from a PICC to serial.
|
4 | * ----------------------------------------------------------------------
|
5 | * https://circuits4you.com
|
6 | *
|
7 | * RC522 Interfacing with NodeMCU
|
8 | *
|
9 | Typical pin layout used:
|
10 | * ---------------------------------------------------------------------
|
11 | * MFRC522 Arduino Arduino Arduino Arduino Arduino
|
12 | * Reader/PCD Uno/101 Mega Nano v3 Leonardo/Micro Pro Micro
|
13 | * Signal Pin Pin Pin Pin Pin Pin
|
14 | * ----------------------------------------------------------------------
|
15 | * RST/Reset RST 9 5 D9 RESET/ICSP-5 RST
|
16 | * SPI SS SDA(SS) 10 53 D10 10 10
|
17 | * SPI MOSI MOSI 11 / ICSP-4 51 D11 ICSP-4 16
|
18 | * SPI MISO MISO 12 / ICSP-1 50 D12 ICSP-1 14
|
19 | * SPI SCK SCK 13 / ICSP-3 52 D13 ICSP-3 15
|
20 | */
|
21 |
|
22 | #include <SPI.h>
|
23 | #include <MFRC522.h>
|
24 |
|
25 | constexpr uint8_t RST_PIN = 9; // Configurable, see typical pin layout above
|
26 | constexpr uint8_t SS_PIN = 10; // Configurable, see typical pin layout above
|
27 |
|
28 | MFRC522 rfid(SS_PIN, RST_PIN); // Instance of the class
|
29 |
|
30 | MFRC522::MIFARE_Key key;
|
31 |
|
32 | // Init array that will store new NUID
|
33 | byte nuidPICC[4];
|
34 |
|
35 | void setup() {
|
36 | Serial.begin(9600);
|
37 | delay(1000);
|
38 | SPI.begin(); // Init SPI bus
|
39 | rfid.PCD_Init(); // Init MFRC522
|
40 |
|
41 | for (byte i = 0; i < 6; i++) {
|
42 | key.keyByte[i] = 0xFF;
|
43 | }
|
44 |
|
45 | Serial.println(F("This code scan the MIFARE Classsic NUID."));
|
46 | Serial.print(F("Using the following key:"));
|
47 | printHex(key.keyByte, MFRC522::MF_KEY_SIZE);
|
48 | }
|
49 |
|
50 | void loop() {
|
51 |
|
52 | // Look for new cards
|
53 | if ( ! rfid.PICC_IsNewCardPresent())
|
54 | return;
|
55 |
|
56 | // Verify if the NUID has been readed
|
57 | if ( ! rfid.PICC_ReadCardSerial())
|
58 | return;
|
59 |
|
60 | Serial.print(F("PICC type: "));
|
61 | MFRC522::PICC_Type piccType = rfid.PICC_GetType(rfid.uid.sak);
|
62 | Serial.println(rfid.PICC_GetTypeName(piccType));
|
63 |
|
64 | // Check is the PICC of Classic MIFARE type
|
65 | if (piccType != MFRC522::PICC_TYPE_MIFARE_MINI &&
|
66 | piccType != MFRC522::PICC_TYPE_MIFARE_1K &&
|
67 | piccType != MFRC522::PICC_TYPE_MIFARE_4K) {
|
68 | Serial.println(F("Your tag is not of type MIFARE Classic."));
|
69 | return;
|
70 | }
|
71 |
|
72 | if (rfid.uid.uidByte[0] != nuidPICC[0] ||
|
73 | rfid.uid.uidByte[1] != nuidPICC[1] ||
|
74 | rfid.uid.uidByte[2] != nuidPICC[2] ||
|
75 | rfid.uid.uidByte[3] != nuidPICC[3] ) {
|
76 | Serial.println(F("A new card has been detected."));
|
77 |
|
78 | // Store NUID into nuidPICC array
|
79 | for (byte i = 0; i < 4; i++) {
|
80 | nuidPICC[i] = rfid.uid.uidByte[i];
|
81 | }
|
82 |
|
83 | /*Serial.println(F("The NUID tag is:"));
|
84 | Serial.print(F("In hex: "));
|
85 | printHex(rfid.uid.uidByte, rfid.uid.size);
|
86 | Serial.println();
|
87 | Serial.print(F("In dec: "));
|
88 | printDec(rfid.uid.uidByte, rfid.uid.size);
|
89 | Serial.println();
|
90 | }
|
91 | */
|
92 | Serial.println(F(""));
|
93 | //Serial.print(F(""));
|
94 | printHex(rfid.uid.uidByte, rfid.uid.size);
|
95 | Serial.println();
|
96 |
|
97 | }
|
98 | else Serial.println(F("Card read previously."));
|
99 |
|
100 | // Halt PICC
|
101 | rfid.PICC_HaltA();
|
102 |
|
103 | // Stop encryption on PCD
|
104 | rfid.PCD_StopCrypto1();
|
105 | }
|
106 |
|
107 |
|
108 | /**
|
109 | * Helper routine to dump a byte array as hex values to Serial.
|
110 | */
|
111 | void printHex(byte *buffer, byte bufferSize) {
|
112 | for (byte i = 0; i < bufferSize; i++) {
|
113 | Serial.print(buffer[i] < 0x10 ? " 0" : " ");
|
114 | Serial.print(buffer[i], HEX);
|
115 |
|
116 | }
|
117 | }
|
118 |
|
119 | /**
|
120 | * Helper routine to dump a byte array as dec values to Serial.
|
121 | */
|
122 | void printDec(byte *buffer, byte bufferSize) {
|
123 | for (byte i = 0; i < bufferSize; i++) {
|
124 | Serial.print(buffer[i] < 0x10 ? " 0" : " ");
|
125 | Serial.print(buffer[i], DEC);
|
126 |
|
127 | }
|
128 | }
|