Advertisement
pleasedontcode

"RFID Identification" rev_01

Jun 12th, 2024
497
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /********* Pleasedontcode.com **********
  2.  
  3.     Pleasedontcode thanks you for automatic code generation! Enjoy your code!
  4.  
  5.     - Terms and Conditions:
  6.     You have a non-exclusive, revocable, worldwide, royalty-free license
  7.     for personal and commercial use. Attribution is optional; modifications
  8.     are allowed, but you're responsible for code maintenance. We're not
  9.     liable for any loss or damage. For full terms,
  10.     please visit pleasedontcode.com/termsandconditions.
  11.  
  12.     - Project: "RFID Identification"
  13.     - Source Code NOT compiled for: ESP32 DevKit V1
  14.     - Source Code created on: 2024-06-12 22:54:58
  15.  
  16. ********* Pleasedontcode.com **********/
  17.  
  18. /****** SYSTEM REQUIREMENTS *****/
  19. /****** SYSTEM REQUIREMENT 1 *****/
  20.     /* Enhance the RFID system to assign a unique random */
  21.     /* identifier to each new RFID tag detected using the */
  22.     /* MFRC522 module. Ensure the identifier is stored */
  23.     /* and displayed via serial communication for */
  24.     /* verification. */
  25. /****** END SYSTEM REQUIREMENTS *****/
  26.  
  27. /****** DEFINITION OF LIBRARIES *****/
  28. #include <SPI.h>
  29. #include <MFRC522.h> //https://github.com/miguelbalboa/rfid
  30.  
  31. /****** FUNCTION PROTOTYPES *****/
  32. void setup(void);
  33. void loop(void);
  34. void updateOutputs(void);
  35. void printHex(byte *buffer, byte bufferSize);
  36. void printDec(byte *buffer, byte bufferSize);
  37. void generateRandomIdentifier(byte *buffer, byte bufferSize);
  38.  
  39. /***** DEFINITION OF DIGITAL INPUT PINS *****/
  40. const uint8_t rfid_MFRC522_IRQ_PIN_D13 = 13;
  41.  
  42. /***** DEFINITION OF DIGITAL OUTPUT PINS *****/
  43. const uint8_t rfid_MFRC522_RST_PIN_D4 = 4;
  44.  
  45. /***** DEFINITION OF SPI PINS *****/
  46. const uint8_t rfid_MFRC522_SPI_PIN_MOSI_D23 = 23;
  47. const uint8_t rfid_MFRC522_SPI_PIN_MISO_D19 = 19;
  48. const uint8_t rfid_MFRC522_SPI_PIN_SCLK_D18 = 18;
  49. const uint8_t rfid_MFRC522_SPI_PIN_CS_D5 = 5;
  50.  
  51. /***** DEFINITION OF OUTPUT RAW VARIABLES *****/
  52. /***** used to store raw data *****/
  53. bool rfid_MFRC522_RST_PIN_D4_rawData = 0;
  54.  
  55. /***** DEFINITION OF OUTPUT PHYSICAL VARIABLES *****/
  56. /***** used to store data after characteristic curve transformation *****/
  57. float rfid_MFRC522_RST_PIN_D4_phyData = 0.0;
  58.  
  59. /****** DEFINITION OF LIBRARIES CLASS INSTANCES*****/
  60. MFRC522 rfid(rfid_MFRC522_SPI_PIN_CS_D5, rfid_MFRC522_RST_PIN_D4); // Instance of the class
  61. MFRC522::MIFARE_Key key; // Key instance
  62.  
  63. byte nuidPICC[4]; // Array to store new NUID
  64. byte uniqueID[4]; // Array to store unique random identifier
  65.  
  66. void setup(void)
  67. {
  68.   // put your setup code here, to run once:
  69.   Serial.begin(9600); // Initialize serial communication
  70.   pinMode(rfid_MFRC522_IRQ_PIN_D13, INPUT_PULLUP);
  71.   pinMode(rfid_MFRC522_RST_PIN_D4, OUTPUT);
  72.   pinMode(rfid_MFRC522_SPI_PIN_CS_D5, OUTPUT);
  73.  
  74.   // start the SPI library:
  75.   SPI.begin();
  76.   rfid.PCD_Init(); // Init MFRC522
  77.  
  78.   for (byte i = 0; i < 6; i++) {
  79.     key.keyByte[i] = 0xFF;
  80.   }
  81.  
  82.   Serial.println(F("This code scans the MIFARE Classic NUID."));
  83.   Serial.print(F("Using the following key:"));
  84.   printHex(key.keyByte, MFRC522::MF_KEY_SIZE);
  85.  
  86.   // Seed the random number generator
  87.   randomSeed(analogRead(0));
  88. }
  89.  
  90. void loop(void)
  91. {
  92.   // put your main code here, to run repeatedly:
  93.   updateOutputs(); // Refresh output data
  94.  
  95.   if (!rfid.PICC_IsNewCardPresent() || !rfid.PICC_ReadCardSerial()) return;
  96.  
  97.   Serial.print(F("PICC type: "));
  98.   MFRC522::PICC_Type piccType = rfid.PICC_GetType(rfid.uid.sak);
  99.   Serial.println(rfid.PICC_GetTypeName(piccType));
  100.  
  101.   if (piccType != MFRC522::PICC_TYPE_MIFARE_MINI &&
  102.       piccType != MFRC522::PICC_TYPE_MIFARE_1K &&
  103.       piccType != MFRC522::PICC_TYPE_MIFARE_4K) {
  104.     Serial.println(F("Your tag is not of type MIFARE Classic."));
  105.     return;
  106.   }
  107.  
  108.   if (memcmp(rfid.uid.uidByte, nuidPICC, 4) != 0) {
  109.     Serial.println(F("A new card has been detected."));
  110.     memcpy(nuidPICC, rfid.uid.uidByte, 4);
  111.  
  112.     Serial.println(F("The NUID tag is:"));
  113.     Serial.print(F("In hex: "));
  114.     printHex(rfid.uid.uidByte, rfid.uid.size);
  115.     Serial.println();
  116.     Serial.print(F("In dec: "));
  117.     printDec(rfid.uid.uidByte, rfid.uid.size);
  118.     Serial.println();
  119.  
  120.     // Generate and display a unique random identifier for the new card
  121.     generateRandomIdentifier(uniqueID, 4);
  122.     Serial.println(F("Assigned unique identifier:"));
  123.     Serial.print(F("In hex: "));
  124.     printHex(uniqueID, 4);
  125.     Serial.println();
  126.     Serial.print(F("In dec: "));
  127.     printDec(uniqueID, 4);
  128.     Serial.println();
  129.   } else {
  130.     Serial.println(F("Card read previously."));
  131.   }
  132.  
  133.   rfid.PICC_HaltA(); // Halt PICC
  134.   rfid.PCD_StopCrypto1(); // Stop encryption on PCD
  135. }
  136.  
  137. void updateOutputs()
  138. {
  139.   digitalWrite(rfid_MFRC522_RST_PIN_D4, rfid_MFRC522_RST_PIN_D4_rawData);
  140. }
  141.  
  142. void printHex(byte *buffer, byte bufferSize) {
  143.   for (byte i = 0; i < bufferSize; i++) {
  144.     Serial.print(buffer[i] < 0x10 ? " 0" : " ");
  145.     Serial.print(buffer[i], HEX);
  146.   }
  147. }
  148.  
  149. void printDec(byte *buffer, byte bufferSize) {
  150.   for (byte i = 0; i < bufferSize; i++) {
  151.     Serial.print(buffer[i] < 0x10 ? " 0" : " ");
  152.     Serial.print(buffer[i], DEC);
  153.   }
  154. }
  155.  
  156. void generateRandomIdentifier(byte *buffer, byte bufferSize) {
  157.   for (byte i = 0; i < bufferSize; i++) {
  158.     buffer[i] = random(0, 256); // Generate a random byte
  159.   }
  160. }
  161.  
  162. /* END CODE */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement