-
Notifications
You must be signed in to change notification settings - Fork 4
/
NTag213ReadWrite.ino
92 lines (65 loc) · 2.59 KB
/
NTag213ReadWrite.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
/* This program writes data to a read/write protected NTag213
*
*/
#include <SPI.h>
#include <MFRC522.h>
#define RST_PIN 9 // Configurable, see typical pin layout above
#define SS_PIN 10 // Configurable, see typical pin layout above
MFRC522 mfrc522(SS_PIN, RST_PIN); // Create MFRC522 instance
MFRC522::StatusCode status; //variable to get card status
byte buffer[18]; //data transfer buffer (16+2 bytes data+CRC)
byte size = sizeof(buffer);
uint8_t pageAddr = 0x06; //In this example we will write/read 16 bytes (page 6,7,8 and 9).
//Ultraligth mem = 16 pages. 4 bytes per page.
//Pages 0 to 4 are for special functions.
void setup() {
Serial.begin(9600); // Initialize serial communications with the PC
SPI.begin(); // Init SPI bus
mfrc522.PCD_Init(); // Init MFRC522 card
Serial.println(F("Sketch has been started!"));
memcpy(buffer,"HELLO WORLD! <3",16);
}
void loop() {
// Look for new cards
if ( ! mfrc522.PICC_IsNewCardPresent())
return;
// Select one of the cards
if ( ! mfrc522.PICC_ReadCardSerial())
return;
byte PSWBuff[] = {0xFF, 0xFF, 0xEE, 0xEE}; //32 bit password. Set using NTag213Protection program
byte pACK[] = {0xFF, 0xFF}; //16 bit PassWord ACK returned by the NFCtag
status = mfrc522.PCD_NTAG216_AUTH(&PSWBuff[0], pACK);
if(MFRC522::STATUS_OK !=status){
Serial.print(F("Authentication failed: "));
return;
}
Serial.println(F("Authentication OK "));
// Write data ***********************************************
for (int i=0; i < 4; i++) {
//data is writen in blocks of 4 bytes (4 bytes per page)
status = (MFRC522::StatusCode) mfrc522.MIFARE_Ultralight_Write(pageAddr+i, &buffer[i*4], 4);
if (status != MFRC522::STATUS_OK) {
Serial.print(F("MIFARE_Ultralight_Write() failed: "));
Serial.println(mfrc522.GetStatusCodeName(status));
return;
}
}
Serial.println(F("MIFARE_Ultralight_Write() OK "));
Serial.println();
// Read data ***************************************************
Serial.println(F("Reading data ... "));
//data in 4 block is readed at once.
status = (MFRC522::StatusCode) mfrc522.MIFARE_Read(pageAddr, buffer, &size);
if (status != MFRC522::STATUS_OK) {
Serial.print(F("MIFARE_Read() failed: "));
Serial.println(mfrc522.GetStatusCodeName(status));
return;
}
Serial.print(F("Readed data: "));
//Dump a byte array to Serial
for (byte i = 0; i < 16; i++) {
Serial.write(buffer[i]);
}
Serial.println();
mfrc522.PICC_HaltA();
}