Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /* Initial version is we are writing our UIDs into volatile memory meaning when the power is turned off
- * the tables are reset with no UIDs
- *
- */
- #include <SPI.h>
- #include <MFRC522.h>
- #define SS_1_PIN 10 // Attach Reader #1
- #define SS_2_PIN 8 // Attach Reader #2
- #define RST_PIN 9
- #define NR_OF_READERS 2 //
- byte ssPins[] = {SS_1_PIN, SS_2_PIN};
- uint8_t reader;
- byte sector = 1;
- MFRC522 mfrc522[NR_OF_READERS]; // Create MFRC522 instances
- MFRC522::MIFARE_Key key;
- void setup() {
- Serial.begin(9600); // Initialize serial communications with the PC
- while (!Serial); // Do nothing if no serial port is opened (added for Arduinos based on ATMEGA32U4)
- SPI.begin(); // Init SPI bus
- // Prepare the key (used both as key A and as key B)
- // using FFFFFFFFFFFFh which is the default at chip delivery from the factory
- for (byte i = 0; i < 6; i++) {
- key.keyByte[i] = 0xFF;
- }
- Serial.println(F("Scan a MIFARE Classic PICC to demonstrate Value Block mode."));
- Serial.print(F("Using key (for A and B):"));
- dump_byte_array(key.keyByte, MFRC522::MF_KEY_SIZE);
- Serial.println();
- Serial.println(F("BEWARE: Data will be written to the PICC, in sector #1"));
- for (reader = 0; reader < NR_OF_READERS; reader++) {
- mfrc522[reader].PCD_Init(ssPins[reader], RST_PIN); // Init each MFRC522 card
- Serial.print(F("Reader "));
- Serial.print(reader);
- Serial.print(F(": "));
- mfrc522[reader].PCD_DumpVersionToSerial();
- }
- }
- void loop() {
- for (reader = 0; reader < NR_OF_READERS; reader++) {
- // Look for new cards
- if (mfrc522[reader].PICC_IsNewCardPresent() && mfrc522[reader].PICC_ReadCardSerial()) {
- Serial.print(F("Reader "));
- Serial.print(reader);
- // Show some details of the PICC (that is: the tag/card)
- Serial.print(F(": Card UID:"));
- dump_byte_array(mfrc522[reader].uid.uidByte, mfrc522[reader].uid.size);
- Serial.println();
- Serial.print(F("PICC type: "));
- MFRC522::PICC_Type piccType = mfrc522[reader].PICC_GetType(mfrc522[reader].uid.sak);
- Serial.println(mfrc522[reader].PICC_GetTypeName(piccType));
- // Halt PICC
- mfrc522[reader].PICC_HaltA();
- // Stop encryption on PCD
- mfrc522[reader].PCD_StopCrypto1();
- //LOCKER SETUP FILE STARTS HERE
- // Check for compatibility
- byte valueBlockA = 5;
- byte trailerBlock = 7;
- MFRC522::StatusCode status;
- byte buffer[18];
- byte size = sizeof(buffer);
- int32_t value;
- // Authenticate using key A
- Serial.println(F("Authenticating using key A..."));
- status = mfrc522[reader].PCD_Authenticate(MFRC522::PICC_CMD_MF_AUTH_KEY_A, trailerBlock, &key, &(mfrc522[reader].uid));
- if (status != MFRC522::STATUS_OK) {
- Serial.print(F("PCD_Authenticate() failed:"));
- Serial.println(mfrc522[reader].GetStatusCodeName(status));
- return;
- }
- Serial.println("Authentication success");
- } //if (mfrc522[reader].PICC_IsNewC
- } //for(uint8_t reader
- // Dump the sector data
- //mfrc522[reader].PICC_DumpMifareClassicSectorToSerial(&(mfrc522[reader].uid), &key, sector);
- //Serial.println();
- // Halt PICC
- mfrc522[reader].PICC_HaltA();
- // Stop encryption on PCD
- mfrc522[reader].PCD_StopCrypto1();
- }
- /**
- * Helper routine to dump a byte array as hex values to Serial.
- */
- void dump_byte_array(byte *buffer, byte bufferSize) {
- for (byte i = 0; i < bufferSize; i++) {
- Serial.print(buffer[i] < 0x10 ? " 0" : " ");
- Serial.print(buffer[i], HEX);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment