|
| 1 | +#include <SPI.h> //include the SPI library |
| 2 | +#include <MFRC522.h> //include the MFRC522 RFID reader library |
| 3 | + |
| 4 | +#define RST_PIN 9 //reset pin, which can be changed to another digital pin if needed. |
| 5 | +#define SS_PIN 10 //SS or the slave select pin, which can be changed to another digital pin if needed. |
| 6 | + |
| 7 | +MFRC522 mfrc522(SS_PIN, RST_PIN); // create a MFRC522 instant. |
| 8 | +MFRC522::MIFARE_Key key; //create a MIFARE_Key struct named 'key' to hold the card information |
| 9 | + |
| 10 | +byte data1[14] = {"Circuit-Digest"}; //The first data that needs to be written to the tag. |
| 11 | +byte data2[12] = {"Jobit-Joseph"}; //The second data that needs to be written to the tag. |
| 12 | +byte readbackblock[18]; //Array for reading out a block. |
| 13 | + |
| 14 | +void setup() |
| 15 | +{ |
| 16 | + Serial.begin(115200); // Initialize serial communications with the PC |
| 17 | + SPI.begin(); // Init SPI bus |
| 18 | + mfrc522.PCD_Init(); // Init MFRC522 card (in case you wonder what PCD means: proximity coupling device) |
| 19 | + Serial.println("Scan a MIFARE Classic card"); |
| 20 | + |
| 21 | + for (byte i = 0; i < 6; i++) |
| 22 | + { |
| 23 | + key.keyByte[i] = 0xFF; // Prepare the security key for the read and write operations. |
| 24 | + } |
| 25 | +} |
| 26 | + |
| 27 | +void loop() |
| 28 | +{ |
| 29 | + // Look for new cards if not found rerun the loop function |
| 30 | + if ( ! mfrc522.PICC_IsNewCardPresent()) { |
| 31 | + return; |
| 32 | + } |
| 33 | + |
| 34 | + // read from the card if not found rerun the loop function |
| 35 | + if ( ! mfrc522.PICC_ReadCardSerial()) |
| 36 | + { |
| 37 | + return; |
| 38 | + } |
| 39 | + Serial.println("card detected. Writing data"); |
| 40 | + writeBlock(1, data1); //write data1 to the block 1 of the tag |
| 41 | + writeBlock(2, data2); //write data2 to the block 2 of the tag |
| 42 | + |
| 43 | + Serial.println("reading data from the tag"); |
| 44 | + readBlock(1, readbackblock); //read block 1 |
| 45 | + //print data |
| 46 | + Serial.print("read block 1: "); |
| 47 | + for (int j = 0 ; j < 14 ; j++) |
| 48 | + { |
| 49 | + Serial.write (readbackblock[j]); |
| 50 | + } |
| 51 | + Serial.println(""); |
| 52 | + readBlock(2, readbackblock); //read block 2 |
| 53 | + //print data |
| 54 | + Serial.print("read block 2: "); |
| 55 | + for (int j = 0 ; j < 12 ; j++) |
| 56 | + { |
| 57 | + Serial.write (readbackblock[j]); |
| 58 | + } |
| 59 | + Serial.println(""); |
| 60 | + |
| 61 | + //mfrc522.PICC_DumpToSerial(&(mfrc522.uid));//uncomment below line if want to see the entire memory dump. |
| 62 | + |
| 63 | + |
| 64 | +} |
| 65 | + |
| 66 | + |
| 67 | + |
| 68 | +//Write specific block |
| 69 | +int writeBlock(int blockNumber, byte arrayAddress[]) |
| 70 | +{ |
| 71 | + //check if the block number corresponds to data block or triler block, rtuen with error if it's trailer block. |
| 72 | + int largestModulo4Number = blockNumber / 4 * 4; |
| 73 | + int trailerBlock = largestModulo4Number + 3; //determine trailer block for the sector |
| 74 | + if (blockNumber > 2 && (blockNumber + 1) % 4 == 0) { |
| 75 | + Serial.print(blockNumber); |
| 76 | + Serial.println(" is a trailer block: Error"); |
| 77 | + return 2; |
| 78 | + } |
| 79 | + |
| 80 | + |
| 81 | + //authentication |
| 82 | + byte status = mfrc522.PCD_Authenticate(MFRC522::PICC_CMD_MF_AUTH_KEY_A, trailerBlock, &key, &(mfrc522.uid)); |
| 83 | + if (status != MFRC522::STATUS_OK) { |
| 84 | + Serial.print("Authentication failed: "); |
| 85 | + Serial.println(mfrc522.GetStatusCodeName(status)); |
| 86 | + return 3;//return "3" as error message |
| 87 | + } |
| 88 | + |
| 89 | + //writing data to the block |
| 90 | + status = mfrc522.MIFARE_Write(blockNumber, arrayAddress, 16); |
| 91 | + //status = mfrc522.MIFARE_Write(9, value1Block, 16); |
| 92 | + if (status != MFRC522::STATUS_OK) { |
| 93 | + Serial.print("Data write failed: "); |
| 94 | + Serial.println(mfrc522.GetStatusCodeName(status)); |
| 95 | + return 4;//return "4" as error message |
| 96 | + } |
| 97 | + Serial.print("Data written to block "); |
| 98 | + Serial.println(blockNumber); |
| 99 | +} |
| 100 | + |
| 101 | + |
| 102 | + |
| 103 | +//Read specific block |
| 104 | +int readBlock(int blockNumber, byte arrayAddress[]) |
| 105 | +{ |
| 106 | + int largestModulo4Number = blockNumber / 4 * 4; |
| 107 | + int trailerBlock = largestModulo4Number + 3; //determine trailer block for the sector |
| 108 | + |
| 109 | + //authentication of the desired block for access |
| 110 | + byte status = mfrc522.PCD_Authenticate(MFRC522::PICC_CMD_MF_AUTH_KEY_A, trailerBlock, &key, &(mfrc522.uid)); |
| 111 | + |
| 112 | + if (status != MFRC522::STATUS_OK) { |
| 113 | + Serial.print("Authentication failed : "); |
| 114 | + Serial.println(mfrc522.GetStatusCodeName(status)); |
| 115 | + return 3;//return "3" as error message |
| 116 | + } |
| 117 | + |
| 118 | + //reading data from the block |
| 119 | + byte buffersize = 18; |
| 120 | + status = mfrc522.MIFARE_Read(blockNumber, arrayAddress, &buffersize);//&buffersize is a pointer to the buffersize variable; MIFARE_Read requires a pointer instead of just a number |
| 121 | + if (status != MFRC522::STATUS_OK) { |
| 122 | + Serial.print("Data read failed: "); |
| 123 | + Serial.println(mfrc522.GetStatusCodeName(status)); |
| 124 | + return 4;//return "4" as error message |
| 125 | + } |
| 126 | + Serial.println("Data read successfully"); |
| 127 | +} |
0 commit comments