| 1 | /**************************************************************************/ |
| 2 | /*! |
| 3 | @file readMifareClassicIrq.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 | |
| 22 | This is an example sketch for the Adafruit PN532 NFC/RFID breakout boards |
| 23 | This library works with the Adafruit NFC breakout |
| 24 | ----> https://www.adafruit.com/products/364 |
| 25 | |
| 26 | Check out the links above for our tutorials and wiring diagrams |
| 27 | |
| 28 | This example is for communicating with the PN532 chip using I2C. Wiring |
| 29 | should be as follows: |
| 30 | PN532 SDA -> SDA pin |
| 31 | PN532 SCL -> SCL pin |
| 32 | PN532 IRQ -> D2 |
| 33 | PN532 SDA -> 3.3v (with 2k resistor) |
| 34 | PN532 SCL -> 3.3v (with 2k resistor) |
| 35 | PN532 3.3v -> 3.3v |
| 36 | PN532 GND -> GND |
| 37 | |
| 38 | Adafruit invests time and resources providing this open source code, |
| 39 | please support Adafruit and open-source hardware by purchasing |
| 40 | products from Adafruit! |
| 41 | */ |
| 42 | /**************************************************************************/ |
| 43 | #include <Wire.h> |
| 44 | #include <SPI.h> |
| 45 | #include <Adafruit_PN532.h> |
| 46 | |
| 47 | // If using the breakout with SPI, define the pins for SPI communication. |
| 48 | #define PN532_SCK (2) |
| 49 | #define PN532_MOSI (3) |
| 50 | #define PN532_SS (4) |
| 51 | #define PN532_MISO (5) |
| 52 | |
| 53 | // If using the breakout or shield with I2C, define just the pins connected |
| 54 | // to the IRQ and reset lines. Use the values below (2, 3) for the shield! |
| 55 | #define PN532_IRQ (2) |
| 56 | #define PN532_RESET (3) // Not connected by default on the NFC Shield |
| 57 | |
| 58 | const int DELAY_BETWEEN_CARDS = 500; |
| 59 | long timeLastCardRead = 0; |
| 60 | boolean readerDisabled = false; |
| 61 | int irqCurr; |
| 62 | int irqPrev; |
| 63 | |
| 64 | // This example uses the IRQ line, which is available when in I2C mode. |
| 65 | Adafruit_PN532 nfc(PN532_IRQ, PN532_RESET); |
| 66 | |
| 67 | void setup(void) { |
| 68 | Serial.begin(115200); |
| 69 | while (!Serial) delay(10); // for Leonardo/Micro/Zero |
| 70 | |
| 71 | Serial.println("Hello!"); |
| 72 | |
| 73 | nfc.begin(); |
| 74 | |
| 75 | uint32_t versiondata = nfc.getFirmwareVersion(); |
| 76 | if (! versiondata) { |
| 77 | Serial.print("Didn't find PN53x board"); |
| 78 | while (1); // halt |
| 79 | } |
| 80 | // Got ok data, print it out! |
| 81 | Serial.print("Found chip PN5"); Serial.println((versiondata>>24) & 0xFF, HEX); |
| 82 | Serial.print("Firmware ver. "); Serial.print((versiondata>>16) & 0xFF, DEC); |
| 83 | Serial.print('.'); Serial.println((versiondata>>8) & 0xFF, DEC); |
| 84 | |
| 85 | startListeningToNFC(); |
| 86 | } |
| 87 | |
| 88 | void loop(void) { |
| 89 | if (readerDisabled) { |
| 90 | if (millis() - timeLastCardRead > DELAY_BETWEEN_CARDS) { |
| 91 | readerDisabled = false; |
| 92 | startListeningToNFC(); |
| 93 | } |
| 94 | } else { |
| 95 | irqCurr = digitalRead(PN532_IRQ); |
| 96 | |
| 97 | // When the IRQ is pulled low - the reader has got something for us. |
| 98 | if (irqCurr == LOW && irqPrev == HIGH) { |
| 99 | Serial.println("Got NFC IRQ"); |
| 100 | handleCardDetected(); |
| 101 | } |
| 102 | |
| 103 | irqPrev = irqCurr; |
| 104 | } |
| 105 | } |
| 106 | |
| 107 | void startListeningToNFC() { |
| 108 | // Reset our IRQ indicators |
| 109 | irqPrev = irqCurr = HIGH; |
| 110 | |
| 111 | Serial.println("Starting passive read for an ISO14443A Card ..."); |
| 112 | if (!nfc.startPassiveTargetIDDetection(PN532_MIFARE_ISO14443A)) { |
| 113 | Serial.println("No card found. Waiting..."); |
| 114 | } else { |
| 115 | Serial.println("Card already present."); |
| 116 | handleCardDetected(); |
| 117 | } |
| 118 | } |
| 119 | |
| 120 | void handleCardDetected() { |
| 121 | uint8_t success = false; |
| 122 | uint8_t uid[] = { 0, 0, 0, 0, 0, 0, 0 }; // Buffer to store the returned UID |
| 123 | uint8_t uidLength; // Length of the UID (4 or 7 bytes depending on ISO14443A card type) |
| 124 | |
| 125 | // read the NFC tag's info |
| 126 | success = nfc.readDetectedPassiveTargetID(uid, &uidLength); |
| 127 | Serial.println(success ? "Read successful" : "Read failed (not a card?)"); |
| 128 | |
| 129 | if (success) { |
| 130 | // Display some basic information about the card |
| 131 | Serial.println("Found an ISO14443A card"); |
| 132 | Serial.print(" UID Length: ");Serial.print(uidLength, DEC);Serial.println(" bytes"); |
| 133 | Serial.print(" UID Value: "); |
| 134 | nfc.PrintHex(uid, uidLength); |
| 135 | |
| 136 | if (uidLength == 4) |
| 137 | { |
| 138 | // We probably have a Mifare Classic card ... |
| 139 | uint32_t cardid = uid[0]; |
| 140 | cardid <<= 8; |
| 141 | cardid |= uid[1]; |
| 142 | cardid <<= 8; |
| 143 | cardid |= uid[2]; |
| 144 | cardid <<= 8; |
| 145 | cardid |= uid[3]; |
| 146 | Serial.print("Seems to be a Mifare Classic card #"); |
| 147 | Serial.println(cardid); |
| 148 | } |
| 149 | Serial.println(""); |
| 150 | |
| 151 | timeLastCardRead = millis(); |
| 152 | } |
| 153 | |
| 154 | // The reader will be enabled again after DELAY_BETWEEN_CARDS ms will pass. |
| 155 | readerDisabled = true; |
| 156 | } |