Krolyxon
Commit 3120783

Branches

Tags

Refs →

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

177 lines · 6.3 KB

1/**************************************************************************/
2/*!
3 @file mifareclassic_updatendef.pde
4 @author Adafruit Industries
5 @license BSD (see license.txt)
6
7 Updates a sector that is already formatted for NDEF (using
8 mifareclassic_formatndef.pde for example), inserting a new url
9
10 This is an example sketch for the Adafruit PN532 NFC/RFID breakout boards
11 This library works with the Adafruit NFC Shield
12 ----> https://www.adafruit.com/products/789
13
14 Check out the links above for our tutorials and wiring diagrams
15 These chips use SPI or I2C to communicate
16
17 Adafruit invests time and resources providing this open source code,
18 please support Adafruit and open-source hardware by purchasing
19 products from Adafruit!
20
21*/
22/**************************************************************************/
23
24#include <Wire.h>
25#include <SPI.h>
26#include <Adafruit_PN532.h>
27
28// If using the breakout with SPI, define the pins for SPI communication.
29#define PN532_SCK (2)
30#define PN532_MOSI (3)
31#define PN532_SS (4)
32#define PN532_MISO (5)
33
34// If using the breakout or shield with I2C, define just the pins connected
35// to the IRQ and reset lines. Use the values below (2, 3) for the shield!
36#define PN532_IRQ (2)
37#define PN532_RESET (3) // Not connected by default on the NFC Shield
38
39// Uncomment just _one_ line below depending on how your breakout or shield
40// is connected to the Arduino:
41
42// Use this line for a breakout with a SPI connection:
43Adafruit_PN532 nfc(PN532_SCK, PN532_MISO, PN532_MOSI, PN532_SS);
44
45// Use this line for a breakout with a hardware SPI connection. Note that
46// the PN532 SCK, MOSI, and MISO pins need to be connected to the Arduino's
47// hardware SPI SCK, MOSI, and MISO pins. On an Arduino Uno these are
48// SCK = 13, MOSI = 11, MISO = 12. The SS line can be any digital IO pin.
49//Adafruit_PN532 nfc(PN532_SS);
50
51// Or use this line for a breakout or shield with an I2C connection:
52//Adafruit_PN532 nfc(PN532_IRQ, PN532_RESET);
53
54/*
55 We can encode many different kinds of pointers to the card,
56 from a URL, to an Email address, to a phone number, and many more
57 check the library header .h file to see the large # of supported
58 prefixes!
59*/
60// For a http://www. url:
61const char * url = "adafruit.com/blog/";
62uint8_t ndefprefix = NDEF_URIPREFIX_HTTP_WWWDOT;
63
64// for an email address
65//const char * url = "mail@example.com";
66//uint8_t ndefprefix = NDEF_URIPREFIX_MAILTO;
67
68// for a phone number
69//const char * url = "+1 212 555 1212";
70//uint8_t ndefprefix = NDEF_URIPREFIX_TEL;
71
72
73void setup(void) {
74 Serial.begin(115200);
75 while (!Serial) delay(10); // for Leonardo/Micro/Zero
76
77 Serial.println("Looking for PN532...");
78
79 nfc.begin();
80
81 uint32_t versiondata = nfc.getFirmwareVersion();
82 if (! versiondata) {
83 Serial.print("Didn't find PN53x board");
84 while (1); // halt
85 }
86
87 // Got ok data, print it out!
88 Serial.print("Found chip PN5"); Serial.println((versiondata>>24) & 0xFF, HEX);
89 Serial.print("Firmware ver. "); Serial.print((versiondata>>16) & 0xFF, DEC);
90 Serial.print('.'); Serial.println((versiondata>>8) & 0xFF, DEC);
91}
92
93void loop(void) {
94 uint8_t success; // Flag to check if there was an error with the PN532
95 uint8_t uid[] = { 0, 0, 0, 0, 0, 0, 0 }; // Buffer to store the returned UID
96 uint8_t uidLength; // Length of the UID (4 or 7 bytes depending on ISO14443A card type)
97
98 // Use the default NDEF keys (these would have have set by mifareclassic_formatndef.pde!)
99 uint8_t keyb[6] = { 0xD3, 0xF7, 0xD3, 0xF7, 0xD3, 0xF7 };
100
101 Serial.println("Place your NDEF formatted Mifare Classic card on the reader to update the");
102 Serial.println("NDEF record and press any key to continue ...");
103 // Wait for user input before proceeding
104 while (!Serial.available());
105 // a key was pressed1
106 while (Serial.available()) Serial.read();
107
108 // Wait for an ISO14443A type card (Mifare, etc.). When one is found
109 // 'uid' will be populated with the UID, and uidLength will indicate
110 // if the uid is 4 bytes (Mifare Classic) or 7 bytes (Mifare Ultralight)
111 success = nfc.readPassiveTargetID(PN532_MIFARE_ISO14443A, uid, &uidLength);
112
113 if (success)
114 {
115 // Display some basic information about the card
116 Serial.println("Found an ISO14443A card");
117 Serial.print(" UID Length: ");Serial.print(uidLength, DEC);Serial.println(" bytes");
118 Serial.print(" UID Value: ");
119 nfc.PrintHex(uid, uidLength);
120 Serial.println("");
121
122 // Make sure this is a Mifare Classic card
123 if (uidLength != 4)
124 {
125 Serial.println("Ooops ... this doesn't seem to be a Mifare Classic card!");
126 return;
127 }
128
129 // We probably have a Mifare Classic card ...
130 Serial.println("Seems to be a Mifare Classic card (4 byte UID)");
131
132 // Check if this is an NDEF card (using first block of sector 1 from mifareclassic_formatndef.pde)
133 // Must authenticate on the first key using 0xD3 0xF7 0xD3 0xF7 0xD3 0xF7
134 success = nfc.mifareclassic_AuthenticateBlock (uid, uidLength, 4, 0, keyb);
135 if (!success)
136 {
137 Serial.println("Unable to authenticate block 4 ... is this card NDEF formatted?");
138 return;
139 }
140
141 Serial.println("Authentication succeeded (seems to be an NDEF/NFC Forum tag) ...");
142
143 // Authenticated seems to have worked
144 // Try to write an NDEF record to sector 1
145 // Use 0x01 for the URI Identifier Code to prepend "http://www."
146 // to the url (and save some space). For information on URI ID Codes
147 // see http://www.ladyada.net/wiki/private/articlestaging/nfc/ndef
148 if (strlen(url) > 38)
149 {
150 // The length is also checked in the WriteNDEFURI function, but lets
151 // warn users here just in case they change the value and it's bigger
152 // than it should be
153 Serial.println("URI is too long ... must be less than 38 characters!");
154 return;
155 }
156
157 Serial.println("Updating sector 1 with URI as NDEF Message");
158
159 // URI is within size limits ... write it to the card and report success/failure
160 success = nfc.mifareclassic_WriteNDEFURI(1, ndefprefix, url);
161 if (success)
162 {
163 Serial.println("NDEF URI Record written to sector 1");
164 Serial.println("");
165 }
166 else
167 {
168 Serial.println("NDEF Record creation failed! :(");
169 }
170 }
171
172 // Wait a bit before trying again
173 Serial.println("\n\nDone!");
174 delay(1000);
175 Serial.flush();
176 while(Serial.available()) Serial.read();
177}