Commit 25fb98d37bff22c10b43daa1b63152a141c5b000

Authored by henyxia
1 parent 3c63af70

Rev 1

Showing 1 changed file with 101 additions and 87 deletions   Show diff stats
tweekd_nfc.ino
... ... @@ -2,128 +2,142 @@
2 2 #include <PN532_SPI.h>
3 3 #include "PN532.h"
4 4  
  5 +#define MODEL_QUERY 0x80
  6 +#define SERIAL_ERROR 0x55
  7 +#define NFC_TAGQUERY 0x82
  8 +#define NFC_TAGQUERY_UID 0x84
  9 +#define NFC_ARDUINO 0x02
  10 +#define NFC_NOTAG 0x81
  11 +
  12 +#define NFC_TYPE_PROFESSOR 0x00
  13 +#define NFC_TYPE_STUDENT 0x01
  14 +
  15 +boolean tagDetected;
  16 +boolean tagType;
  17 +
5 18 PN532_SPI pn532spi(SPI, 10);
6 19 PN532 nfc(pn532spi);
7 20  
8   -void setup(void) {
  21 +void setup(void)
  22 +{
  23 + int ser;
  24 +
9 25 Serial.begin(115200);
10   - Serial.println("Hello!");
11 26  
12 27 nfc.begin();
13 28  
14 29 uint32_t versiondata = nfc.getFirmwareVersion();
15   - if (! versiondata)
  30 + if(!versiondata)
16 31 {
17   - Serial.print("Didn't find PN53x board");
  32 + Serial.print(SERIAL_ERROR);
18 33 while (1);
19   - }
20   - //Serial.print("Found chip PN5"); Serial.println((versiondata>>24) & 0xFF, HEX);
21   - Serial.print("Firmware ver. "); Serial.print((versiondata>>16) & 0xFF, DEC);
22   - Serial.print('.'); Serial.println((versiondata>>8) & 0xFF, DEC);
23   -
24   - // configure board to read RFID tags
  34 + }
  35 +
25 36 nfc.SAMConfig();
26 37  
27   - Serial.println("Waiting for an ISO14443A Card ...");
  38 + tagDetected = false;
  39 +
  40 + do
  41 + {
  42 + while(!(Serial.available() > 0));
  43 + ser = Serial.read();
  44 + if(ser == MODEL_QUERY)
  45 + Serial.print(NFC_ARDUINO);
  46 + else
  47 + Serial.print(SERIAL_ERROR);
  48 + }while(ser != MODEL_QUERY);
28 49 }
29 50  
30 51  
31 52 void loop(void)
32 53 {
33 54 uint8_t success;
34   - uint8_t uid[] = { 0, 0, 0, 0, 0, 0, 0 }; // Buffer to store the returned UID
35   - uint8_t uidLength; // Length of the UID (4 or 7 bytes depending on ISO14443A card type)
  55 + uint8_t uid[] = { 0, 0, 0, 0, 0, 0, 0 };
  56 + uint8_t uidLength;
  57 + uint8_t data1[16];
  58 + uint8_t data2[16];
  59 + uint8_t dataf[8];
  60 + int ser;
36 61  
37   - // Wait for an ISO14443A type cards (Mifare, etc.). When one is found
38   - // 'uid' will be populated with the UID, and uidLength will indicate
39   - // if the uid is 4 bytes (Mifare Classic) or 7 bytes (Mifare Ultralight)
40 62 success = nfc.readPassiveTargetID(PN532_MIFARE_ISO14443A, uid, &uidLength);
41   -
42   - if(success)
  63 +
  64 + if(success && !tagDetected)
43 65 {
44   - // Display some basic information about the card
45   - Serial.println("Found an ISO14443A card");
46   - Serial.print(" UID Length: ");Serial.print(uidLength, DEC);Serial.println(" bytes");
47   - Serial.print(" UID Value: ");
48   - nfc.PrintHex(uid, uidLength);
49   - Serial.println("");
50   -
51 66 if(uidLength == 4)
52 67 {
53   - // We probably have a Mifare Classic card ...
54   - Serial.println("Seems to be a Mifare Classic card (4 byte UID)");
55   -
56   - // Now we need to try to authenticate it for read/write access
57   - // Try with the factory default KeyA: 0xFF 0xFF 0xFF 0xFF 0xFF 0xFF
58   - Serial.println("Trying to authenticate block 4 with default KEYA value");
59 68 uint8_t keya[6] = { 0xA0, 0xA1, 0xA2, 0xA3, 0xA4, 0xA5 };
60   -
61   - // Start with block 4 (the first block of sector 1) since sector 0
62   - // contains the manufacturer data and it's probably better just
63   - // to leave it alone unless you know what you're doing
64   - success = nfc.mifareclassic_AuthenticateBlock(uid, uidLength, 44, 0, keya);
65   -
  69 + success = nfc.mifareclassic_AuthenticateBlock(uid, uidLength, 44, 0, keya);
66 70 if(success)
67 71 {
68   - Serial.println("Sector 1 (Blocks 4..7) has been authenticated");
69   - uint8_t data[16];
70   -
71   - // If you want to write something to block 4 to test with, uncomment
72   - // the following line and this text should be read back in a minute
73   - // data = { 'a', 'd', 'a', 'f', 'r', 'u', 'i', 't', '.', 'c', 'o', 'm', 0, 0, 0, 0};
74   - // success = nfc.mifareclassic_WriteDataBlock (4, data);
75   -
76   - // Try to read the contents of block 4
77   - success = nfc.mifareclassic_ReadDataBlock(44, data);
  72 + success = nfc.mifareclassic_ReadDataBlock(44, data1);
78 73  
79 74 if(success)
80 75 {
81   - // Data seems to have been read ... spit it out
82   - Serial.println("Reading Block 4:");
83   - nfc.PrintHexChar(data, 16);
84   - nfc.mifareclassic_ReadDataBlock(45, data);
85   - nfc.PrintHexChar(data, 16);
86   - nfc.mifareclassic_ReadDataBlock(46, data);
87   - nfc.PrintHexChar(data, 16);
88   - Serial.println("");
89   -
90   - // Wait a bit before reading the card again
91   - delay(1000);
92   - }
93   - else
94   - {
95   - Serial.println("Ooops ... unable to read the requested block. Try another key?");
  76 + //nfc.PrintHexChar(data1, 16);
  77 + success = nfc.mifareclassic_ReadDataBlock(45, data2);
  78 +
  79 + if(success)
  80 + {
  81 + tagDetected = true;
  82 + if(data1[4] == data1[15] &&
  83 + data1[5] == data2[0] &&
  84 + data1[6] == data2[1] &&
  85 + data1[7] == data2[2] &&
  86 + data1[8] == data2[3] &&
  87 + data1[9] == data2[4])
  88 + {
  89 + tagType = NFC_TYPE_PROFESSOR;
  90 + dataf[0] = data1[4];
  91 + dataf[1] = data1[5];
  92 + dataf[2] = data1[6];
  93 + dataf[3] = data1[7];
  94 + dataf[4] = data1[8];
  95 + dataf[5] = data1[9];
  96 + }
  97 + else
  98 + {
  99 + tagType = NFC_TYPE_STUDENT;
  100 + dataf[0] = data1[15];
  101 + dataf[1] = data2[0];
  102 + dataf[2] = data2[1];
  103 + dataf[3] = data2[2];
  104 + dataf[4] = data2[3];
  105 + dataf[5] = data2[4];
  106 + dataf[6] = data2[5];
  107 + dataf[7] = data2[6];
  108 + }
  109 + }
96 110 }
97 111 }
98   - else
99   - {
100   - Serial.println("Ooops ... authentication failed: Try another key?");
101   - }
102 112 }
103   -
104   - if(uidLength == 7)
  113 + }
  114 + else if(tagDetected)
  115 + {
  116 + while(!(Serial.available() > 0));
  117 + ser = Serial.read();
  118 + if(ser == NFC_TAGQUERY)
  119 + Serial.print(tagType);
  120 + else if(ser == NFC_TAGQUERY_UID)
105 121 {
106   - // We probably have a Mifare Ultralight card ...
107   - Serial.println("Seems to be a Mifare Ultralight tag (7 byte UID)");
108   -
109   - // Try to read the first general-purpose user page (#4)
110   - Serial.println("Reading page 4");
111   - uint8_t data[32];
112   - success = nfc.mifareultralight_ReadPage (4, data);
113   - if(success)
114   - {
115   - // Data seems to have been read ... spit it out
116   - nfc.PrintHexChar(data, 4);
117   - Serial.println("");
118   -
119   - // Wait a bit before reading the card again
120   - delay(1000);
121   - }
122   - else
  122 + Serial.print((char) dataf[0]);
  123 + Serial.print((char) dataf[1]);
  124 + Serial.print((char) dataf[2]);
  125 + Serial.print((char) dataf[3]);
  126 + Serial.print((char) dataf[4]);
  127 + Serial.print((char) dataf[5]);
  128 + if(tagType == NFC_TYPE_STUDENT)
123 129 {
124   - Serial.println("Ooops ... unable to read the requested page!?");
  130 + Serial.print((char) dataf[6]);
  131 + Serial.print((char) dataf[7]);
125 132 }
126 133 }
  134 + else
  135 + Serial.print(SERIAL_ERROR);
  136 + }
  137 + else
  138 + {
  139 + if(Serial.available() > 0)
  140 + if(Serial.read() == NFC_TAGQUERY)
  141 + Serial.print(NFC_NOTAG);
127 142 }
128 143 }
129   -
... ...