Guest User

Untitled

a guest
Nov 13th, 2018
351
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.75 KB | None | 0 0
  1. /* Initial version is we are writing our UIDs into volatile memory meaning when the power is turned off
  2. * the tables are reset with no UIDs
  3. *
  4. */
  5.  
  6. #include <SPI.h>
  7. #include <MFRC522.h>
  8.  
  9. #define SS_1_PIN 10 // Attach Reader #1
  10. #define SS_2_PIN 8 // Attach Reader #2
  11. #define RST_PIN 9
  12. #define NR_OF_READERS 2 //
  13.  
  14. byte ssPins[] = {SS_1_PIN, SS_2_PIN};
  15.  
  16. uint8_t reader;
  17. byte sector = 1;
  18.  
  19. MFRC522 mfrc522[NR_OF_READERS]; // Create MFRC522 instances
  20. MFRC522::MIFARE_Key key;
  21.  
  22.  
  23. void setup() {
  24.  
  25. Serial.begin(9600); // Initialize serial communications with the PC
  26. while (!Serial); // Do nothing if no serial port is opened (added for Arduinos based on ATMEGA32U4)
  27.  
  28. SPI.begin(); // Init SPI bus
  29.  
  30.  
  31. // Prepare the key (used both as key A and as key B)
  32. // using FFFFFFFFFFFFh which is the default at chip delivery from the factory
  33. for (byte i = 0; i < 6; i++) {
  34. key.keyByte[i] = 0xFF;
  35. }
  36.  
  37. Serial.println(F("Scan a MIFARE Classic PICC to demonstrate Value Block mode."));
  38. Serial.print(F("Using key (for A and B):"));
  39. dump_byte_array(key.keyByte, MFRC522::MF_KEY_SIZE);
  40. Serial.println();
  41.  
  42. Serial.println(F("BEWARE: Data will be written to the PICC, in sector #1"));
  43.  
  44. for (reader = 0; reader < NR_OF_READERS; reader++) {
  45. mfrc522[reader].PCD_Init(ssPins[reader], RST_PIN); // Init each MFRC522 card
  46. Serial.print(F("Reader "));
  47. Serial.print(reader);
  48. Serial.print(F(": "));
  49. mfrc522[reader].PCD_DumpVersionToSerial();
  50. }
  51. }
  52.  
  53. void loop() {
  54.  
  55. for (reader = 0; reader < NR_OF_READERS; reader++) {
  56. // Look for new cards
  57.  
  58. if (mfrc522[reader].PICC_IsNewCardPresent() && mfrc522[reader].PICC_ReadCardSerial()) {
  59. Serial.print(F("Reader "));
  60. Serial.print(reader);
  61. // Show some details of the PICC (that is: the tag/card)
  62. Serial.print(F(": Card UID:"));
  63. dump_byte_array(mfrc522[reader].uid.uidByte, mfrc522[reader].uid.size);
  64. Serial.println();
  65. Serial.print(F("PICC type: "));
  66. MFRC522::PICC_Type piccType = mfrc522[reader].PICC_GetType(mfrc522[reader].uid.sak);
  67. Serial.println(mfrc522[reader].PICC_GetTypeName(piccType));
  68.  
  69.  
  70. // Halt PICC
  71. mfrc522[reader].PICC_HaltA();
  72. // Stop encryption on PCD
  73. mfrc522[reader].PCD_StopCrypto1();
  74.  
  75. //LOCKER SETUP FILE STARTS HERE
  76.  
  77. // Check for compatibility
  78.  
  79. byte valueBlockA = 5;
  80. byte trailerBlock = 7;
  81. MFRC522::StatusCode status;
  82. byte buffer[18];
  83. byte size = sizeof(buffer);
  84. int32_t value;
  85.  
  86. // Authenticate using key A
  87. Serial.println(F("Authenticating using key A..."));
  88. status = mfrc522[reader].PCD_Authenticate(MFRC522::PICC_CMD_MF_AUTH_KEY_A, trailerBlock, &key, &(mfrc522[reader].uid));
  89. if (status != MFRC522::STATUS_OK) {
  90. Serial.print(F("PCD_Authenticate() failed:"));
  91. Serial.println(mfrc522[reader].GetStatusCodeName(status));
  92. return;
  93. }
  94.  
  95. Serial.println("Authentication success");
  96.  
  97. } //if (mfrc522[reader].PICC_IsNewC
  98. } //for(uint8_t reader
  99.  
  100. // Dump the sector data
  101. //mfrc522[reader].PICC_DumpMifareClassicSectorToSerial(&(mfrc522[reader].uid), &key, sector);
  102. //Serial.println();
  103.  
  104. // Halt PICC
  105. mfrc522[reader].PICC_HaltA();
  106. // Stop encryption on PCD
  107. mfrc522[reader].PCD_StopCrypto1();
  108. }
  109.  
  110. /**
  111. * Helper routine to dump a byte array as hex values to Serial.
  112. */
  113. void dump_byte_array(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. }
Advertisement
Add Comment
Please, Sign In to add comment