Hi,
I was having a play with the RFID RC522 and I'm quite impressed with the amount of data a single card can actually store. I tried to find a sample sketch that I could use to decode and print ALL the block address but I couldn't find one. So I butchered the Random Nerds version created my own and it's below:
/*
Rui Santos & Sara Santos - Random Nerd Tutorials
Complete project details at https://RandomNerdTutorials.com/esp32-mfrc522-rfid-reader-arduino/
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files.
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
*/
#include <MFRC522v2.h>
#include <MFRC522DriverSPI.h>
//#include <MFRC522DriverI2C.h>
#include <MFRC522DriverPinSimple.h>
#include <MFRC522Debug.h>
// Learn more about using SPI/I2C or check the pin assigment for your board: https://github.com/OSSLibraries/Arduino_MFRC522v2#pin-layout
MFRC522DriverPinSimple ss_pin(5);
MFRC522DriverSPI driver{ss_pin}; // Create SPI driver
//MFRC522DriverI2C driver{}; // Create I2C driver
MFRC522 mfrc522{driver}; // Create MFRC522 instance
MFRC522::MIFARE_Key key;
const byte BLOCKS [] = {0,1,2,4,5,6,8,9,10,12,13,14,16,17,18,20,21,22,24,25,26,28,29,30,32,33,34,36,37,38,40,41,42,44,45,46,48,49,50,52,53,54,56,57,58,60,61,62};
//byte blockAddress = 2;
byte bufferblocksize = 18;
byte blockDataRead[18];
int IndLED = 33;
void setup() {
pinMode(IndLED, OUTPUT);
digitalWrite(IndLED, HIGH);
Serial.begin(115200); // Initialize serial communication
while (!Serial); // Do nothing if no serial port is opened (added for Arduinos based on ATMEGA32U4).
mfrc522.PCD_Init(); // Init MFRC522 board.
Serial.println(F("Warning: this example overwrites a block in your card, use with care!"));
// Prepare key - all keys are set to FFFFFFFFFFFF at chip delivery from the factory.
for (byte i = 0; i < 6; i++) {
key.keyByte[i] = 0xFF;
}
}
void loop() {
// Check if a new card is present
if (!mfrc522.PICC_IsNewCardPresent() || !mfrc522.PICC_ReadCardSerial()) {
delay(500);
return;
}
Serial.print("----------------\nCard UID: ");
MFRC522Debug::PrintUID(Serial, (mfrc522.uid));
Serial.println();
//Card is present so loop through all blocks
for (byte BLOCKADD : BLOCKS) { // for each block address in the blocks
ReadBlocks(BLOCKADD);
}
// Halt communication with the card
mfrc522.PICC_HaltA();
mfrc522.PCD_StopCrypto1();
digitalWrite(IndLED, LOW);
delay(2000); // Delay for readability
digitalWrite(IndLED, HIGH);
delay(2000); // Delay for readability
}
void ReadBlocks(byte BLOCKADD){
// Authenticate the specified block using KEY_A = 0x60
if (mfrc522.PCD_Authenticate(0x60, BLOCKADD, &key, &(mfrc522.uid)) != 0) {
Serial.println("Authentication failed.");
return;
}
// Read data from the specified block
if (mfrc522.MIFARE_Read(BLOCKADD, blockDataRead, &bufferblocksize) != 0) {
Serial.println("Read failed.");
} else {
Serial.print("Data in block ");
Serial.print(BLOCKADD);
Serial.print(": ");
for (byte i = 0; i < 16; i++) {
Serial.print((char)blockDataRead[i]); // Print as character
}
Serial.println();
}
}
Now that I have the basics I'm going to create an iPhone app that sends data either using Bluetooth or WiFi to program various blocks with the data I want on the card. I'll send a command to initialise the Write process, using an LED as an indication its ready to write data to the relevant data block then give a 10 second window to place the card on the RC522. If the card isn't placed on the RC522 then it will revert back to the Read Mode.
I'm thinking of using a ST7735S TFT but I've heard that there will be a conflict using the RFID and TFT on the same ESP32. Has anyone done this? If so, could you please share?
