Guest User

Untitled

a guest
Aug 22nd, 2019
52
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.74 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. * RC522 Interfacing with NodeMCU
  8. *
  9. Typical pin layout used:
  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. constexpr uint8_t RST_PIN = 9; // Configurable, see typical pin layout above
  26. constexpr uint8_t SS_PIN = 10; // Configurable, see typical pin layout above
  27.  
  28. MFRC522 rfid(SS_PIN, RST_PIN); // Instance of the class
  29.  
  30. MFRC522::MIFARE_Key key;
  31.  
  32. // Init array that will store new NUID
  33. byte nuidPICC[4];
  34.  
  35. void setup() {
  36. Serial.begin(9600);
  37. SPI.begin(); // Init SPI bus
  38. rfid.PCD_Init(); // Init MFRC522
  39.  
  40. for (byte i = 0; i < 6; i++) {
  41. key.keyByte[i] = 0xFF;
  42. }
  43.  
  44. Serial.println(F("This code scan the MIFARE Classsic NUID."));
  45. Serial.print(F("Using the following key:"));
  46. printHex(key.keyByte, MFRC522::MF_KEY_SIZE);
  47. }
  48.  
  49. void loop() {
  50.  
  51. // Look for new cards
  52. if ( ! rfid.PICC_IsNewCardPresent())
  53. return;
  54.  
  55. // Verify if the NUID has been readed
  56. if ( ! rfid.PICC_ReadCardSerial())
  57. return;
  58.  
  59. Serial.print(F("PICC type: "));
  60. MFRC522::PICC_Type piccType = rfid.PICC_GetType(rfid.uid.sak);
  61. Serial.println(rfid.PICC_GetTypeName(piccType));
  62.  
  63. // Check is the PICC of Classic MIFARE type
  64. if (piccType != MFRC522::PICC_TYPE_MIFARE_MINI &&
  65. piccType != MFRC522::PICC_TYPE_MIFARE_1K &&
  66. piccType != MFRC522::PICC_TYPE_MIFARE_4K) {
  67. Serial.println(F("Your tag is not of type MIFARE Classic."));
  68. return;
  69. }
  70.  
  71. if (rfid.uid.uidByte[0] != nuidPICC[0] ||
  72. rfid.uid.uidByte[1] != nuidPICC[1] ||
  73. rfid.uid.uidByte[2] != nuidPICC[2] ||
  74. rfid.uid.uidByte[3] != nuidPICC[3] ) {
  75. Serial.println(F("A new card has been detected."));
  76.  
  77. // Store NUID into nuidPICC array
  78. for (byte i = 0; i < 4; i++) {
  79. nuidPICC[i] = rfid.uid.uidByte[i];
  80. }
  81.  
  82. Serial.println(F("The NUID tag is:"));
  83. Serial.print(F("In hex: "));
  84. printHex(rfid.uid.uidByte, rfid.uid.size);
  85. Serial.println();
  86. Serial.print(F("In dec: "));
  87. printDec(rfid.uid.uidByte, rfid.uid.size);
  88. Serial.println();
  89. }
  90. else Serial.println(F("Card read previously."));
  91.  
  92. // Halt PICC
  93. rfid.PICC_HaltA();
  94.  
  95. // Stop encryption on PCD
  96. rfid.PCD_StopCrypto1();
  97. }
  98.  
  99.  
  100. /**
  101. * Helper routine to dump a byte array as hex values to Serial.
  102. */
  103. void printHex(byte *buffer, byte bufferSize) {
  104. for (byte i = 0; i < bufferSize; i++) {
  105. Serial.print(buffer[i] < 0x10 ? " 0" : " ");
  106. Serial.print(buffer[i], HEX);
  107. }
  108. }
  109.  
  110. /**
  111. * Helper routine to dump a byte array as dec values to Serial.
  112. */
  113. void printDec(byte *buffer, byte bufferSize) {
  114. for (byte i = 0; i < bufferSize; i++) {
  115. Serial.print(buffer[i] < 0x10 ? " 0" : " ");
  116. Serial.print(buffer[i], DEC);
  117. }
  118. }
Add Comment
Please, Sign In to add comment