Advertisement
zbeucler

RFID.ino

Jul 20th, 2020
2,183
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.60 KB | None | 0 0
  1. /*
  2.  * ----------------------------------------------------------------------
  3.  * Example program showing how to read new NUID from a PICC to serial.
  4.  * ----------------------------------------------------------------------
  5.  * https://circuits4you.com
  6.  *
  7.  * LINK: https://circuits4you.com/2018/10/03/rfid-reader-rc522-interface-with-nodemcu-using-arduino-ide/
  8.  *
  9.  * RC522 Interfacing with NodeMCU
  10.  *
  11.  * Typical pin layout used:
  12.  * ----------------------------------
  13.  *             MFRC522      Node    
  14.  *             Reader/PCD   MCU      
  15.  * Signal      Pin          Pin      
  16.  * ----------------------------------
  17.  * RST/Reset   RST          D1 (GPIO5)        
  18.  * SPI SS      SDA(SS)      D2 (GPIO4)      
  19.  * SPI MOSI    MOSI         D7 (GPIO13)
  20.  * SPI MISO    MISO         D6 (GPIO12)
  21.  * SPI SCK     SCK          D5 (GPIO14)
  22.  * 3.3V        3.3V         3.3V
  23.  * GND         GND          GND
  24.  */
  25.  
  26. #include <SPI.h>
  27. #include <MFRC522.h>
  28.  
  29. constexpr uint8_t RST_PIN = 5;     // Configurable, see typical pin layout above
  30. constexpr uint8_t SS_PIN = 4;     // Configurable, see typical pin layout above
  31.  
  32. MFRC522 rfid(SS_PIN, RST_PIN); // Instance of the class
  33.  
  34. MFRC522::MIFARE_Key key;
  35.  
  36. // Init array that will store new NUID
  37. byte nuidPICC[4];
  38.  
  39. void setup() {
  40.   Serial.begin(115200);
  41.   SPI.begin(); // Init SPI bus
  42.   rfid.PCD_Init(); // Init MFRC522
  43.  
  44.   for (byte i = 0; i < 6; i++) {
  45.     key.keyByte[i] = 0xFF;
  46.   }
  47.  
  48.   Serial.println(F("This code scan the MIFARE Classsic NUID."));
  49.   Serial.print(F("Using the following key:"));
  50.   printHex(key.keyByte, MFRC522::MF_KEY_SIZE);
  51. }
  52.  
  53. void loop() {
  54.  
  55.   // Look for new cards
  56.   if ( ! rfid.PICC_IsNewCardPresent())
  57.     return;
  58.  
  59.   // Verify if the NUID has been readed
  60.   if ( ! rfid.PICC_ReadCardSerial())
  61.     return;
  62.  
  63.   Serial.print(F("PICC type: "));
  64.   MFRC522::PICC_Type piccType = rfid.PICC_GetType(rfid.uid.sak);
  65.   Serial.println(rfid.PICC_GetTypeName(piccType));
  66.  
  67.   // Check is the PICC of Classic MIFARE type
  68.   if (piccType != MFRC522::PICC_TYPE_MIFARE_MINI &&  
  69.     piccType != MFRC522::PICC_TYPE_MIFARE_1K &&
  70.     piccType != MFRC522::PICC_TYPE_MIFARE_4K) {
  71.     Serial.println(F("Your tag is not of type MIFARE Classic."));
  72.     return;
  73.   }
  74.  
  75.   if (rfid.uid.uidByte[0] != nuidPICC[0] ||
  76.     rfid.uid.uidByte[1] != nuidPICC[1] ||
  77.     rfid.uid.uidByte[2] != nuidPICC[2] ||
  78.     rfid.uid.uidByte[3] != nuidPICC[3] ) {
  79.     Serial.println(F("A new card has been detected."));
  80.  
  81.     // Store NUID into nuidPICC array
  82.     for (byte i = 0; i < 4; i++) {
  83.       nuidPICC[i] = rfid.uid.uidByte[i];
  84.     }
  85.    
  86.     Serial.println(F("The NUID tag is:"));
  87.     Serial.print(F("In hex: "));
  88.     printHex(rfid.uid.uidByte, rfid.uid.size);
  89.     Serial.println();
  90.     Serial.print(F("In dec: "));
  91.     printDec(rfid.uid.uidByte, rfid.uid.size);
  92.     Serial.println();
  93.     Serial.println("------------------------");
  94.    
  95.   }
  96.   else {
  97.     Serial.println(F("Card read previously."));
  98.     Serial.println("------------------------");
  99.   }
  100.  
  101.  
  102.   // Halt PICC
  103.   rfid.PICC_HaltA();
  104.  
  105.   // Stop encryption on PCD
  106.   rfid.PCD_StopCrypto1();
  107. }
  108.  
  109.  
  110. /**
  111.  * Helper routine to dump a byte array as hex values to Serial.
  112.  */
  113. void printHex(byte *buffer, byte bufferSize) {
  114.   for (byte i = 0; i < bufferSize; i++) {
  115.     Serial.print(buffer[i] < 0x10 ? " 0" : " ");
  116.     Serial.print(buffer[i], HEX);
  117.   }
  118. }
  119.  
  120. /**
  121.  * Helper routine to dump a byte array as dec values to Serial.
  122.  */
  123. void printDec(byte *buffer, byte bufferSize) {
  124.   for (byte i = 0; i < bufferSize; i++) {
  125.     Serial.print(buffer[i] < 0x10 ? " 0" : " ");
  126.     Serial.print(buffer[i], DEC);
  127.   }
  128. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement