Krolyxon
Commit 3120783

Branches

Tags

Refs →

orion-rf.piolibdepsesp32-s3-n16r8Adafruit PN532examplesreadMifareClassicreadMifareClassic.ino

119 lines · 4.2 KB

1/**************************************************************************/
2/*!
3 @file readMifareClassic.pde
4 @author Adafruit Industries
5 @license BSD (see license.txt)
6
7 This example will wait for any ISO14443A card or tag, and
8 depending on the size of the UID will attempt to read from it.
9
10 If the card has a 4-byte UID it is probably a Mifare
11 Classic card, and the following steps are taken:
12
13 Reads the 4 byte (32 bit) ID of a MiFare Classic card.
14 Since the classic cards have only 32 bit identifiers you can stick
15 them in a single variable and use that to compare card ID's as a
16 number. This doesn't work for ultralight cards that have longer 7
17 byte IDs!
18
19 Note that you need the baud rate to be 115200 because we need to
20 print out the data and read from the card at the same time!
21
22This is an example sketch for the Adafruit PN532 NFC/RFID breakout boards
23This library works with the Adafruit NFC breakout
24 ----> https://www.adafruit.com/products/364
25
26Check out the links above for our tutorials and wiring diagrams
27These chips use SPI to communicate, 4 required to interface
28
29Adafruit invests time and resources providing this open source code,
30please support Adafruit and open-source hardware by purchasing
31products from Adafruit!
32*/
33/**************************************************************************/
34#include <Wire.h>
35#include <SPI.h>
36#include <Adafruit_PN532.h>
37
38// If using the breakout with SPI, define the pins for SPI communication.
39#define PN532_SCK (2)
40#define PN532_MOSI (3)
41#define PN532_SS (4)
42#define PN532_MISO (5)
43
44// If using the breakout or shield with I2C, define just the pins connected
45// to the IRQ and reset lines. Use the values below (2, 3) for the shield!
46#define PN532_IRQ (2)
47#define PN532_RESET (3) // Not connected by default on the NFC Shield
48
49// Uncomment just _one_ line below depending on how your breakout or shield
50// is connected to the Arduino:
51
52// Use this line for a breakout with a SPI connection:
53Adafruit_PN532 nfc(PN532_SCK, PN532_MISO, PN532_MOSI, PN532_SS);
54
55// Use this line for a breakout with a hardware SPI connection. Note that
56// the PN532 SCK, MOSI, and MISO pins need to be connected to the Arduino's
57// hardware SPI SCK, MOSI, and MISO pins. On an Arduino Uno these are
58// SCK = 13, MOSI = 11, MISO = 12. The SS line can be any digital IO pin.
59//Adafruit_PN532 nfc(PN532_SS);
60
61// Or use this line for a breakout or shield with an I2C connection:
62//Adafruit_PN532 nfc(PN532_IRQ, PN532_RESET);
63
64void setup(void) {
65 Serial.begin(115200);
66 while (!Serial) delay(10); // for Leonardo/Micro/Zero
67
68 Serial.println("Hello!");
69
70 nfc.begin();
71
72 uint32_t versiondata = nfc.getFirmwareVersion();
73 if (! versiondata) {
74 Serial.print("Didn't find PN53x board");
75 while (1); // halt
76 }
77 // Got ok data, print it out!
78 Serial.print("Found chip PN5"); Serial.println((versiondata>>24) & 0xFF, HEX);
79 Serial.print("Firmware ver. "); Serial.print((versiondata>>16) & 0xFF, DEC);
80 Serial.print('.'); Serial.println((versiondata>>8) & 0xFF, DEC);
81
82 Serial.println("Waiting for an ISO14443A Card ...");
83}
84
85
86void loop(void) {
87 uint8_t success;
88 uint8_t uid[] = { 0, 0, 0, 0, 0, 0, 0 }; // Buffer to store the returned UID
89 uint8_t uidLength; // Length of the UID (4 or 7 bytes depending on ISO14443A card type)
90
91 // Wait for an ISO14443A type cards (Mifare, etc.). When one is found
92 // 'uid' will be populated with the UID, and uidLength will indicate
93 // if the uid is 4 bytes (Mifare Classic) or 7 bytes (Mifare Ultralight)
94 success = nfc.readPassiveTargetID(PN532_MIFARE_ISO14443A, uid, &uidLength);
95
96 if (success) {
97 // Display some basic information about the card
98 Serial.println("Found an ISO14443A card");
99 Serial.print(" UID Length: ");Serial.print(uidLength, DEC);Serial.println(" bytes");
100 Serial.print(" UID Value: ");
101 nfc.PrintHex(uid, uidLength);
102
103 if (uidLength == 4)
104 {
105 // We probably have a Mifare Classic card ...
106 uint32_t cardid = uid[0];
107 cardid <<= 8;
108 cardid |= uid[1];
109 cardid <<= 8;
110 cardid |= uid[2];
111 cardid <<= 8;
112 cardid |= uid[3];
113 Serial.print("Seems to be a Mifare Classic card #");
114 Serial.println(cardid);
115 }
116 Serial.println("");
117 }
118}
119