/
ntag2xx_updatendef.ino
212 lines (180 loc) · 7.29 KB
/
ntag2xx_updatendef.ino
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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
/**************************************************************************/
/*!
@file ntag2xx_updatendef.pde
@author KTOWN (Adafruit Industries)
@license BSD (see license.txt)
This example will wait for any NTAG203 or NTAG213 card or tag,
and will attempt to add or update an NDEF URI at the start of the
tag's memory.
This is an example sketch for the Adafruit PN532 NFC/RFID breakout boards
This library works with the Adafruit NFC breakout
----> https://www.adafruit.com/products/364
Check out the links above for our tutorials and wiring diagrams
These chips use SPI or I2C to communicate.
Adafruit invests time and resources providing this open source code,
please support Adafruit and open-source hardware by purchasing
products from Adafruit!
*/
/**************************************************************************/
#include <Wire.h>
#include <SPI.h>
#include <Adafruit_PN532.h>
// If using the breakout with SPI, define the pins for SPI communication.
#define PN532_SCK (2)
#define PN532_MOSI (3)
#define PN532_SS (4)
#define PN532_MISO (5)
// If using the breakout or shield with I2C, define just the pins connected
// to the IRQ and reset lines. Use the values below (2, 3) for the shield!
#define PN532_IRQ (2)
#define PN532_RESET (3) // Not connected by default on the NFC Shield
// Uncomment just _one_ line below depending on how your breakout or shield
// is connected to the Arduino:
// Use this line for a breakout with a software SPI connection (recommended):
Adafruit_PN532 nfc(PN532_SCK, PN532_MISO, PN532_MOSI, PN532_SS);
// Use this line for a breakout with a hardware SPI connection. Note that
// the PN532 SCK, MOSI, and MISO pins need to be connected to the Arduino's
// hardware SPI SCK, MOSI, and MISO pins. On an Arduino Uno these are
// SCK = 13, MOSI = 11, MISO = 12. The SS line can be any digital IO pin.
//Adafruit_PN532 nfc(PN532_SS);
// Or use this line for a breakout or shield with an I2C connection:
//Adafruit_PN532 nfc(PN532_IRQ, PN532_RESET);
/*
We can encode many different kinds of pointers to the card,
from a URL, to an Email address, to a phone number, and many more
check the library header .h file to see the large # of supported
prefixes!
*/
// For a http://www. url:
char * url = "adafruit.com/blog/";
uint8_t ndefprefix = NDEF_URIPREFIX_HTTP_WWWDOT;
// for an email address
//char * url = "mail@example.com";
//uint8_t ndefprefix = NDEF_URIPREFIX_MAILTO;
// for a phone number
//char * url = "+1 212 555 1212";
//uint8_t ndefprefix = NDEF_URIPREFIX_TEL;
void setup(void) {
Serial.begin(115200);
while (!Serial) delay(10); // for Leonardo/Micro/Zero
Serial.println("Hello!");
nfc.begin();
uint32_t versiondata = nfc.getFirmwareVersion();
if (! versiondata) {
Serial.print("Didn't find PN53x board");
while (1); // halt
}
// Got ok data, print it out!
Serial.print("Found chip PN5"); Serial.println((versiondata>>24) & 0xFF, HEX);
Serial.print("Firmware ver. "); Serial.print((versiondata>>16) & 0xFF, DEC);
Serial.print('.'); Serial.println((versiondata>>8) & 0xFF, DEC);
}
void loop(void)
{
uint8_t success;
uint8_t uid[] = { 0, 0, 0, 0, 0, 0, 0 }; // Buffer to store the returned UID
uint8_t uidLength; // Length of the UID (4 or 7 bytes depending on ISO14443A card type)
uint8_t dataLength;
// Require some user feedback before running this example!
Serial.println("\r\nPlace your NDEF formatted NTAG2xx tag on the reader to update the");
Serial.println("NDEF record and press any key to continue ...\r\n");
// Wait for user input before proceeding
while (!Serial.available());
// a key was pressed1
while (Serial.available()) Serial.read();
// 1.) Wait for an NTAG203 card. When one is found 'uid' will be populated with
// the UID, and uidLength will indicate the size of the UID (normally 7)
success = nfc.readPassiveTargetID(PN532_MIFARE_ISO14443A, uid, &uidLength);
// It seems we found a valid ISO14443A Tag!
if (success)
{
// 2.) Display some basic information about the card
Serial.println("Found an ISO14443A card");
Serial.print(" UID Length: ");Serial.print(uidLength, DEC);Serial.println(" bytes");
Serial.print(" UID Value: ");
nfc.PrintHex(uid, uidLength);
Serial.println("");
if (uidLength != 7)
{
Serial.println("This doesn't seem to be an NTAG203 tag (UUID length != 7 bytes)!");
}
else
{
uint8_t data[32];
// We probably have an NTAG2xx card (though it could be Ultralight as well)
Serial.println("Seems to be an NTAG2xx tag (7 byte UID)");
// NTAG2x3 cards have 39*4 bytes of user pages (156 user bytes),
// starting at page 4 ... larger cards just add pages to the end of
// this range:
// See: http://www.nxp.com/documents/short_data_sheet/NTAG203_SDS.pdf
// TAG Type PAGES USER START USER STOP
// -------- ----- ---------- ---------
// NTAG 203 42 4 39
// NTAG 213 45 4 39
// NTAG 215 135 4 129
// NTAG 216 231 4 225
// 3.) Check if the NDEF Capability Container (CC) bits are already set
// in OTP memory (page 3)
memset(data, 0, 4);
success = nfc.ntag2xx_ReadPage(3, data);
if (!success)
{
Serial.println("Unable to read the Capability Container (page 3)");
return;
}
else
{
// If the tag has already been formatted as NDEF, byte 0 should be:
// Byte 0 = Magic Number (0xE1)
// Byte 1 = NDEF Version (Should be 0x10)
// Byte 2 = Data Area Size (value * 8 bytes)
// Byte 3 = Read/Write Access (0x00 for full read and write)
if (!((data[0] == 0xE1) && (data[1] == 0x10)))
{
Serial.println("This doesn't seem to be an NDEF formatted tag.");
Serial.println("Page 3 should start with 0xE1 0x10.");
}
else
{
// 4.) Determine and display the data area size
dataLength = data[2]*8;
Serial.print("Tag is NDEF formatted. Data area size = ");
Serial.print(dataLength);
Serial.println(" bytes");
// 5.) Erase the old data area
Serial.print("Erasing previous data area ");
for (uint8_t i = 4; i < (dataLength/4)+4; i++)
{
memset(data, 0, 4);
success = nfc.ntag2xx_WritePage(i, data);
Serial.print(".");
if (!success)
{
Serial.println(" ERROR!");
return;
}
}
Serial.println(" DONE!");
// 6.) Try to add a new NDEF URI record
Serial.print("Writing URI as NDEF Record ... ");
success = nfc.ntag2xx_WriteNDEFURI(ndefprefix, url, dataLength);
if (success)
{
Serial.println("DONE!");
}
else
{
Serial.println("ERROR! (URI length?)");
}
} // CC contents NDEF record check
} // CC page read check
} // UUID length check
// Wait a bit before trying again
Serial.flush();
while (!Serial.available());
while (Serial.available()) {
Serial.read();
}
Serial.flush();
} // Start waiting for a new ISO14443A tag
}