-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTagwrite_code.ino
More file actions
158 lines (153 loc) · 6.36 KB
/
Copy pathTagwrite_code.ino
File metadata and controls
158 lines (153 loc) · 6.36 KB
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
//Store name into RFID Tag
#include <SPI.h>
#include <MFRC522.h>
//-----------------------------------------
constexpr uint8_t RST_PIN = D3;
constexpr uint8_t SS_PIN = D4;
//-----------------------------------------
MFRC522 mfrc522(SS_PIN, RST_PIN);
MFRC522::MIFARE_Key key;
//-----------------------------------------
/* Set the block to write data */
int blockNum = 2;
/* This is the actual data which is
going to be written into the card */
byte blockData [16] = {"Rewant"}; // Change the name you want to store in RFID Tag
//-----------------------------------------
/* Create array to read data from Block */
/* Length of buffer should be 2 Bytes
more than the size of Block (16 Bytes) */
byte bufferLen = 18;
byte readBlockData[18];
//-----------------------------------------
MFRC522::StatusCode status;
//-----------------------------------------
void setup()
{
//-----------------------------------------
//Initialize serial communications with PC
Serial.begin(9600);
//-----------------------------------------
//Initialize SPI bus
SPI.begin();
//-----------------------------------------
//Initialize MFRC522 Module
mfrc522.PCD_Init();
Serial.println("Scan a RFID Tag to write data...");
//-----------------------------------------
}
/****************************************************************************************************
* Writ() function
****************************************************************************************************/
void loop()
{
//------------------------------------------------------------------------------
/* Prepare the ksy for authentication */
/* All keys are set to FFFFFFFFFFFFh at chip delivery from the factory */
for (byte i = 0; i < 6; i++){
key.keyByte[i] = 0xFF;
}
//------------------------------------------------------------------------------
/* Look for new cards */
/* Reset the loop if no new card is present on RC522 Reader */
if ( ! mfrc522.PICC_IsNewCardPresent()){return;}
//------------------------------------------------------------------------------
/* Select one of the cards */
if ( ! mfrc522.PICC_ReadCardSerial()) {return;}
//------------------------------------------------------------------------------
Serial.print("\n");
Serial.println("**Card Detected**");
/* Print UID of the Card */
Serial.print(F("Card UID:"));
for (byte i = 0; i < mfrc522.uid.size; i++){
Serial.print(mfrc522.uid.uidByte[i] < 0x10 ? " 0" : " ");
Serial.print(mfrc522.uid.uidByte[i], HEX);
}
Serial.print("\n");
/* Print type of card (for example, MIFARE 1K) */
Serial.print(F("PICC type: "));
MFRC522::PICC_Type piccType = mfrc522.PICC_GetType(mfrc522.uid.sak);
Serial.println(mfrc522.PICC_GetTypeName(piccType));
//------------------------------------------------------------------------------
/* Call 'WriteDataToBlock' function, which will write data to the block */
Serial.print("\n");
Serial.println("Writing to Data Block...");
WriteDataToBlock(blockNum, blockData);
//------------------------------------------------------------------------------
/* Read data from the same block */
Serial.print("\n");
Serial.println("Reading from Data Block...");
ReadDataFromBlock(blockNum, readBlockData);
/* If you want to print the full memory dump, uncomment the next line */
//mfrc522.PICC_DumpToSerial(&(mfrc522.uid));
//------------------------------------------------------------------------------
/* Print the data read from block */
Serial.print("\n");
Serial.print("Data in Block:");
Serial.print(blockNum);
Serial.print(" --> ");
for (int j=0 ; j<16 ; j++){
Serial.write(readBlockData[j]);
}
Serial.print("\n");
//------------------------------------------------------------------------------
}
/****************************************************************************************************
* Writ() function
****************************************************************************************************/
void WriteDataToBlock(int blockNum, byte blockData[])
{
//------------------------------------------------------------------------------
/* Authenticating the desired data block for write access using Key A */
status = mfrc522.PCD_Authenticate(MFRC522::PICC_CMD_MF_AUTH_KEY_A, blockNum, &key, &(mfrc522.uid));
if (status != MFRC522::STATUS_OK){
Serial.print("Authentication failed for Write: ");
Serial.println(mfrc522.GetStatusCodeName(status));
return;
}
//------------------------------------------------------------------------------
else {
Serial.println("Authentication success");
}
//------------------------------------------------------------------------------
/* Write data to the block */
status = mfrc522.MIFARE_Write(blockNum, blockData, 16);
if (status != MFRC522::STATUS_OK) {
Serial.print("Writing to Block failed: ");
Serial.println(mfrc522.GetStatusCodeName(status));
return;
}
else
{Serial.println("Data was written into Block successfully");}
//------------------------------------------------------------------------------
}
/****************************************************************************************************
* ReadDataFromBlock() function
****************************************************************************************************/
void ReadDataFromBlock(int blockNum, byte readBlockData[])
{
//------------------------------------------------------------------------------
/* Authenticating the desired data block for Read access using Key A */
status = mfrc522.PCD_Authenticate(MFRC522::PICC_CMD_MF_AUTH_KEY_A, blockNum, &key, &(mfrc522.uid));
//------------------------------------------------------------------------------
if (status != MFRC522::STATUS_OK){
Serial.print("Authentication failed for Read: ");
Serial.println(mfrc522.GetStatusCodeName(status));
return;
}
else {
Serial.println("Authentication success");
}
//------------------------------------------------------------------------------
/* Reading data from the Block */
status = mfrc522.MIFARE_Read(blockNum, readBlockData, &bufferLen);
if (status != MFRC522::STATUS_OK){
Serial.print("Reading failed: ");
Serial.println(mfrc522.GetStatusCodeName(status));
return;
}
else {
Serial.println("Block was read successfully");
}
//------------------------------------------------------------------------------
}