/********* Pleasedontcode.com **********
Pleasedontcode thanks you for automatic code generation! Enjoy your code!
- Terms and Conditions:
You have a non-exclusive, revocable, worldwide, royalty-free license
for personal and commercial use. Attribution is optional; modifications
are allowed, but you're responsible for code maintenance. We're not
liable for any loss or damage. For full terms,
please visit pleasedontcode.com/termsandconditions.
- Project: "RFID Identification"
- Source Code NOT compiled for: ESP32 DevKit V1
- Source Code created on: 2024-06-12 22:54:58
********* Pleasedontcode.com **********/
/****** SYSTEM REQUIREMENTS *****/
/****** SYSTEM REQUIREMENT 1 *****/
/* Enhance the RFID system to assign a unique random */
/* identifier to each new RFID tag detected using the */
/* MFRC522 module. Ensure the identifier is stored */
/* and displayed via serial communication for */
/* verification. */
/****** END SYSTEM REQUIREMENTS *****/
/****** DEFINITION OF LIBRARIES *****/
#include
#include //https://github.com/miguelbalboa/rfid
/****** FUNCTION PROTOTYPES *****/
void setup(void);
void loop(void);
void updateOutputs(void);
void printHex(byte *buffer, byte bufferSize);
void printDec(byte *buffer, byte bufferSize);
void generateRandomIdentifier(byte *buffer, byte bufferSize);
/***** DEFINITION OF DIGITAL INPUT PINS *****/
const uint8_t rfid_MFRC522_IRQ_PIN_D13 = 13;
/***** DEFINITION OF DIGITAL OUTPUT PINS *****/
const uint8_t rfid_MFRC522_RST_PIN_D4 = 4;
/***** DEFINITION OF SPI PINS *****/
const uint8_t rfid_MFRC522_SPI_PIN_MOSI_D23 = 23;
const uint8_t rfid_MFRC522_SPI_PIN_MISO_D19 = 19;
const uint8_t rfid_MFRC522_SPI_PIN_SCLK_D18 = 18;
const uint8_t rfid_MFRC522_SPI_PIN_CS_D5 = 5;
/***** DEFINITION OF OUTPUT RAW VARIABLES *****/
/***** used to store raw data *****/
bool rfid_MFRC522_RST_PIN_D4_rawData = 0;
/***** DEFINITION OF OUTPUT PHYSICAL VARIABLES *****/
/***** used to store data after characteristic curve transformation *****/
float rfid_MFRC522_RST_PIN_D4_phyData = 0.0;
/****** DEFINITION OF LIBRARIES CLASS INSTANCES*****/
MFRC522 rfid(rfid_MFRC522_SPI_PIN_CS_D5, rfid_MFRC522_RST_PIN_D4); // Instance of the class
MFRC522::MIFARE_Key key; // Key instance
byte nuidPICC[4]; // Array to store new NUID
byte uniqueID[4]; // Array to store unique random identifier
void setup(void)
{
// put your setup code here, to run once:
Serial.begin(9600); // Initialize serial communication
pinMode(rfid_MFRC522_IRQ_PIN_D13, INPUT_PULLUP);
pinMode(rfid_MFRC522_RST_PIN_D4, OUTPUT);
pinMode(rfid_MFRC522_SPI_PIN_CS_D5, OUTPUT);
// start the SPI library:
SPI.begin();
rfid.PCD_Init(); // Init MFRC522
for (byte i = 0; i < 6; i++) {
key.keyByte[i] = 0xFF;
}
Serial.println(F("This code scans the MIFARE Classic NUID."));
Serial.print(F("Using the following key:"));
printHex(key.keyByte, MFRC522::MF_KEY_SIZE);
// Seed the random number generator
randomSeed(analogRead(0));
}
void loop(void)
{
// put your main code here, to run repeatedly:
updateOutputs(); // Refresh output data
if (!rfid.PICC_IsNewCardPresent() || !rfid.PICC_ReadCardSerial()) return;
Serial.print(F("PICC type: "));
MFRC522::PICC_Type piccType = rfid.PICC_GetType(rfid.uid.sak);
Serial.println(rfid.PICC_GetTypeName(piccType));
if (piccType != MFRC522::PICC_TYPE_MIFARE_MINI &&
piccType != MFRC522::PICC_TYPE_MIFARE_1K &&
piccType != MFRC522::PICC_TYPE_MIFARE_4K) {
Serial.println(F("Your tag is not of type MIFARE Classic."));
return;
}
if (memcmp(rfid.uid.uidByte, nuidPICC, 4) != 0) {
Serial.println(F("A new card has been detected."));
memcpy(nuidPICC, rfid.uid.uidByte, 4);
Serial.println(F("The NUID tag is:"));
Serial.print(F("In hex: "));
printHex(rfid.uid.uidByte, rfid.uid.size);
Serial.println();
Serial.print(F("In dec: "));
printDec(rfid.uid.uidByte, rfid.uid.size);
Serial.println();
// Generate and display a unique random identifier for the new card
generateRandomIdentifier(uniqueID, 4);
Serial.println(F("Assigned unique identifier:"));
Serial.print(F("In hex: "));
printHex(uniqueID, 4);
Serial.println();
Serial.print(F("In dec: "));
printDec(uniqueID, 4);
Serial.println();
} else {
Serial.println(F("Card read previously."));
}
rfid.PICC_HaltA(); // Halt PICC
rfid.PCD_StopCrypto1(); // Stop encryption on PCD
}
void updateOutputs()
{
digitalWrite(rfid_MFRC522_RST_PIN_D4, rfid_MFRC522_RST_PIN_D4_rawData);
}
void printHex(byte *buffer, byte bufferSize) {
for (byte i = 0; i < bufferSize; i++) {
Serial.print(buffer[i] < 0x10 ? " 0" : " ");
Serial.print(buffer[i], HEX);
}
}
void printDec(byte *buffer, byte bufferSize) {
for (byte i = 0; i < bufferSize; i++) {
Serial.print(buffer[i] < 0x10 ? " 0" : " ");
Serial.print(buffer[i], DEC);
}
}
void generateRandomIdentifier(byte *buffer, byte bufferSize) {
for (byte i = 0; i < bufferSize; i++) {
buffer[i] = random(0, 256); // Generate a random byte
}
}
/* END CODE */