Advertisement
Guest User

Untitled

a guest
Nov 15th, 2019
142
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /*
  2.  * Initial Author: ryand1011 (https://github.com/ryand1011)
  3.  *
  4.  * Reads data written by a program such as "rfid_write_personal_data.ino"
  5.  *
  6.  * See: https://github.com/miguelbalboa/rfid/tree/master/examples/rfid_write_personal_data
  7.  *
  8.  * Uses MIFARE RFID card using RFID-RC522 reader
  9.  * Uses MFRC522 - Library
  10.  * -----------------------------------------------------------------------------------------
  11.  *             MFRC522      Arduino       Arduino   Arduino    Arduino          Arduino
  12.  *             Reader/PCD   Uno/101       Mega      Nano v3    Leonardo/Micro   Pro Micro
  13.  * Signal      Pin          Pin           Pin       Pin        Pin              Pin
  14.  * -----------------------------------------------------------------------------------------
  15.  * RST/Reset   RST          9             5         D9         RESET/ICSP-5     RST
  16.  * SPI SS      SDA(SS)      10            53        D10        10               10
  17.  * SPI MOSI    MOSI         11 / ICSP-4   51        D11        ICSP-4           16
  18.  * SPI MISO    MISO         12 / ICSP-1   50        D12        ICSP-1           14
  19.  * SPI SCK     SCK          13 / ICSP-3   52        D13        ICSP-3           15
  20. */
  21.  
  22. #include <SPI.h>
  23. #include <MFRC522.h>
  24.  
  25. #define RST_PIN         9           // Configurable, see typical pin layout above
  26. #define SS_PIN          10          // Configurable, see typical pin layout above
  27.  
  28. MFRC522 mfrc522(SS_PIN, RST_PIN);   // Create MFRC522 instance
  29.  
  30. //*****************************************************************************************//
  31. void setup() {
  32.   Serial.begin(9600);                                           // Initialize serial communications with the PC
  33.   SPI.begin();                                                  // Init SPI bus
  34.   mfrc522.PCD_Init();                                              // Init MFRC522 card
  35.   Serial.println(F("Read personal data on a MIFARE PICC:"));    //shows in serial that it is ready to read
  36. }
  37.  
  38. //*****************************************************************************************//
  39. void loop() {
  40.  
  41.   // Prepare key - all keys are set to FFFFFFFFFFFFh at chip delivery from the factory.
  42.   MFRC522::MIFARE_Key key;
  43.   for (byte i = 0; i < 6; i++) key.keyByte[i] = 0xFF;
  44.  
  45.   //some variables we need
  46.   byte block;
  47.   byte len;
  48.   MFRC522::StatusCode status;
  49.  
  50.   //-------------------------------------------
  51.  
  52.   // Look for new cards
  53.   if ( ! mfrc522.PICC_IsNewCardPresent()) {
  54.     return;
  55.   }
  56.  
  57.   // Select one of the cards
  58.   if ( ! mfrc522.PICC_ReadCardSerial()) {
  59.     return;
  60.   }
  61.  
  62.   Serial.println(F("**Card Detected:**"));
  63.  
  64.   //-------------------------------------------
  65.  
  66.   mfrc522.PICC_DumpDetailsToSerial(&(mfrc522.uid)); //dump some details about the card
  67.  
  68.   //mfrc522.PICC_DumpToSerial(&(mfrc522.uid));      //uncomment this to see all blocks in hex
  69.  
  70.   //-------------------------------------------
  71.  
  72.   Serial.print(F("Name: "));
  73.  
  74.   byte buffer1[18];
  75.  
  76.   block = 4;
  77.   len = 18;
  78.  
  79.   //------------------------------------------- GET FIRST NAME
  80.   status = mfrc522.PCD_Authenticate(MFRC522::PICC_CMD_MF_AUTH_KEY_A, 4, &key, &(mfrc522.uid)); //line 834 of MFRC522.cpp file
  81.   if (status != MFRC522::STATUS_OK) {
  82.     Serial.print(F("Authentication failed: "));
  83.     Serial.println(mfrc522.GetStatusCodeName(status));
  84.     return;
  85.   }
  86.  
  87.   status = mfrc522.MIFARE_Read(block, buffer1, &len);
  88.   if (status != MFRC522::STATUS_OK) {
  89.     Serial.print(F("Reading failed: "));
  90.     Serial.println(mfrc522.GetStatusCodeName(status));
  91.     return;
  92.   }
  93.  
  94.   //PRINT FIRST NAME
  95.   for (uint8_t i = 0; i < 16; i++)
  96.   {
  97.     if (buffer1[i] != 32)
  98.     {
  99.       Serial.write(buffer1[i]);
  100.     }
  101.   }
  102.   Serial.print(" ");
  103.  
  104.   //---------------------------------------- GET LAST NAME
  105.  
  106.   byte buffer2[18];
  107.   block = 1;
  108.  
  109.   status = mfrc522.PCD_Authenticate(MFRC522::PICC_CMD_MF_AUTH_KEY_A, 1, &key, &(mfrc522.uid)); //line 834
  110.   if (status != MFRC522::STATUS_OK) {
  111.     Serial.print(F("Authentication failed: "));
  112.     Serial.println(mfrc522.GetStatusCodeName(status));
  113.     return;
  114.   }
  115.  
  116.   status = mfrc522.MIFARE_Read(block, buffer2, &len);
  117.   if (status != MFRC522::STATUS_OK) {
  118.     Serial.print(F("Reading failed: "));
  119.     Serial.println(mfrc522.GetStatusCodeName(status));
  120.     return;
  121.   }
  122.  
  123.   //PRINT LAST NAME
  124.   for (uint8_t i = 0; i < 16; i++) {
  125.     Serial.write(buffer2[i] );
  126.   }
  127.  
  128.  
  129.   //----------------------------------------
  130.  
  131.   Serial.println(F("\n**End Reading**\n"));
  132.  
  133.   delay(1000); //change value if you want to read cards faster
  134.  
  135.   mfrc522.PICC_HaltA();
  136.   mfrc522.PCD_StopCrypto1();
  137. }
  138. //*****************************************************************************************//
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement