Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
#include <SPI.h>
#include <MFRC522.h>
#define SS_PIN 11 /* Slave Select pin, maps to SDA */
#define RST_PIN 6 /* RST pin, a digital pin should suffice*/
/* Create the mfrc522 object */
MFRC522 mfrc522(SS_PIN, RST_PIN);
/* Create the key. They key object will be used for Key A
We can create another key object for key B
*/
MFRC522::MIFARE_Key key; // will hold card info
/* Define the blocksize */
const int blockSize = 16;
/* Buffer that holds the write block content, initialised with Key A and B as FF FF FF FF FF FA */
byte writeBlockContent[blockSize] = {0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFA, 0xFF, 0x7, 0x80, 0x69, 0xFF, 0xFF, 0xFF, 0xFF, 0xFA, 0xFA };
/* Buffer that holds the read block content*/
byte readBlockContent[18] = { 0 };
/*
* Function that writes data to a block.
* param: block The block number where to write the data.
* param: data An byte array containing the data
*/
int writeBlock(int block, byte data[]) {
int status;
if ( (block + 1) % 4 == 0) {
Serial.println("Warning: Writing on a trailer block!");
}
/* We need to authenticate using the trailer block. */
/* For example, block 1 and 2 have the trailer block 3 */
int trailerBlock = block + (4 - block % 4) - 1;
/* Authenticate using the A key. */
status = mfrc522.PCD_Authenticate(MFRC522::PICC_CMD_MF_AUTH_KEY_A, trailerBlock, &key, &mfrc522.uid);
if ( status != MFRC522::STATUS_OK ) {
Serial.print("PCD_Authenticate() failed: ");
Serial.println(mfrc522.GetStatusCodeName((MFRC522::StatusCode) status));
return status;
}
status = mfrc522.MIFARE_Write(block, data, blockSize);
if ( status != MFRC522::STATUS_OK ) {
Serial.print("MIFARE_Write() failed: ");
Serial.println(mfrc522.GetStatusCodeName((MFRC522::StatusCode) status));
return status;
}
return MFRC522::STATUS_OK;
}
/*
* Function that will dump all card blocks on the screen
* via the serial monitor.
*/
void dumpCard() {
for(int i = 0; i < blockSize; ++i) {
Serial.print("Block ");
Serial.print(i);
Serial.print(" ");
if ( readBlock(i, readBlockContent) == MFRC522::STATUS_OK ) {
printReadBlock();
}
}
}
/*
* This function will read a bloc from the card.
* param: block The block from where to read.
* param: data An array to store the read contents. It's size must be at least 16 bytes
*/
int readBlock(int block, byte data[]) {
int status;
byte readSize = blockSize + 2;
int trailerBlock = block + (4 - block % 4) - 1;
status = mfrc522.PCD_Authenticate(MFRC522::PICC_CMD_MF_AUTH_KEY_A, trailerBlock, &key, &mfrc522.uid);
if ( status != MFRC522::STATUS_OK ) {
Serial.print("PCD_Authenticate() failed: ");
Serial.println(mfrc522.GetStatusCodeName((MFRC522::StatusCode)status));
return status;
}
status = mfrc522.MIFARE_Read(block, data, &readSize);
if ( status != MFRC522::STATUS_OK ) {
Serial.print("MIFARE_Read() failed: ");
Serial.println(mfrc522.GetStatusCodeName((MFRC522::StatusCode)status));
return status;
}
return MFRC522::STATUS_OK;
}
/* Helper function that prints the contents of the readBlockContent */
void printReadBlock() {
for (int i = 0; i < blockSize; ++i) {
Serial.print(readBlockContent[i], HEX);
Serial.print(" ");
}
Serial.print(" -> ");
for (int i = 0; i < blockSize; ++i) {
Serial.write(readBlockContent[i]);
}
Serial.println();
}
/* Helper function that prints the card id. */
void printCardUid() {
Serial.print("Card ID: ");
for (int i = 0; i < mfrc522.uid.size; ++i) {
Serial.print(mfrc522.uid.uidByte[i], HEX);
Serial.print(" ");
}
Serial.println();
}
void setup() {
Serial.begin(115200);
while (!Serial); // Wait for serial.
SPI.begin();
mfrc522.PCD_Init();
Serial.println("Initialized...");
/*
* Initializing the key
* You may create other key objects and modify the function
* parameters to accept the key as a parameter.
*/
key.keyByte[0] = 0xFF;
key.keyByte[1] = 0xFF;
key.keyByte[2] = 0xFF;
key.keyByte[3] = 0xFF;
key.keyByte[4] = 0xFF;
key.keyByte[5] = 0xFA;
}
void loop() {
if ( ! mfrc522.PICC_IsNewCardPresent() ) {
return; // No card present, continue.
}
if ( ! mfrc522.PICC_ReadCardSerial() ) {
// On success this will return 1 and set the uid struct mfrc522.uid
return;
}
printCardUid();
/* This snippet will write the writeBlockContent to the trailer block 7*/
// writeBlock(7, writeBlockContent);
// if ( readBlock(7, readBlockContent) == MFRC522::STATUS_OK )
// printReadBlock();
dumpCard();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment