Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <MFRC522.h>
- #define SS_PIN 21
- #define RST_PIN 22
- String Card_code;
- int scan_to_post = 0;
- MFRC522 RFID(SS_PIN, RST_PIN); // 創建新的RFID實例
- MFRC522::MIFARE_Key key;
- void RFID_init()
- {
- SPI.begin();
- RFID.PCD_Init();
- for (byte i = 0; i < 6; i++)
- {
- key.keyByte[i] = 0xFF;
- }
- }
- void dump_byte_array(byte *buffer, byte bufferSize)
- {
- Card_code = "";
- String code_temp[4];
- for (byte i = 0; i < bufferSize; i++)
- {
- code_temp[i] = buffer[i];
- Card_code += code_temp[i];
- }
- scan_to_post = 1;
- Serial.println(Card_code);
- Serial.println();
- }
- void read_RFID()
- {
- if ( ! RFID.PICC_ReadCardSerial())
- {
- delay(50);
- return;
- }
- Serial.println("RFID Tag Detected...");
- Serial.print(F("Card UID:"));
- dump_byte_array(RFID.uid.uidByte, RFID.uid.size);
- }
- int money_dec(byte *buffer ,byte bufferSize)
- {
- String tmp[3];
- for (byte i = 0; i < bufferSize; i++)
- {
- tmp[i] = String(buffer[i], HEX);
- }
- tmp[3] = tmp[0] + tmp[1];
- long money_dec = strtol(tmp[3].c_str(), NULL, 16);
- return money_dec;
- }
- int dump_byte_array_RW(byte *buffer, byte bufferSize)
- {
- for (byte i = 0; i < bufferSize; i++)
- {
- Serial.print(buffer[i] < 0x10 ? " 0" : " ");
- Serial.print(buffer[i], HEX);
- }
- int money = money_dec(buffer,bufferSize);
- return money;
- }
- int read_RFID_sector()
- {
- byte sector = 1;
- byte blockAddr = 4;
- byte buffer[18];
- byte size = sizeof(buffer);
- RFID.PICC_DumpMifareClassicSectorToSerial(&(RFID.uid), &key, sector);
- Serial.println();
- //read
- RFID.MIFARE_Read(blockAddr, buffer, &size);
- Serial.print(F("Data in block "));
- Serial.print(blockAddr);
- Serial.println(F(":"));
- int money = dump_byte_array_RW(buffer, 2);
- Serial.println();
- RFID.PICC_HaltA();
- // Stop encryption on PCD
- RFID.PCD_StopCrypto1();
- return money;
- }
- int write_rfid(int money)
- {
- // Show some details of the PICC (that is: the tag/card)
- Serial.print(F("Card UID:"));
- dump_byte_array_RW(RFID.uid.uidByte, RFID.uid.size);
- Serial.println();
- Serial.print(F("PICC type: "));
- MFRC522::PICC_Type piccType = RFID.PICC_GetType(RFID.uid.sak);
- Serial.println(RFID.PICC_GetTypeName(piccType));
- // Check for compatibility
- if ( piccType != MFRC522::PICC_TYPE_MIFARE_MINI
- && piccType != MFRC522::PICC_TYPE_MIFARE_1K
- && piccType != MFRC522::PICC_TYPE_MIFARE_4K)
- {
- Serial.println(F("This sample only works with MIFARE Classic cards."));
- return '0';
- }
- byte sector = 1;
- byte blockAddr = 4;
- String tmp[3];
- tmp[2] = String(money, HEX);
- tmp[0] = tmp[2].substring(0,2);
- tmp[1] = tmp[2].substring(2);
- byte tmp1 = strtol(tmp[0].c_str(), NULL, 16);
- byte tmp2 = strtol(tmp[1].c_str(), NULL, 16);
- byte dataBlock[] =
- {
- tmp1, tmp2, 0x00, 0x00,
- 0x00, 0x00, 0x00, 0x00,
- 0x00, 0x00, 0x00, 0x00,
- 0x00, 0x00, 0x00, 0x00
- };
- MFRC522::StatusCode status;
- byte buffer[18];
- byte size = sizeof(buffer);
- Serial.println(dataBlock[0]);
- Serial.println(dataBlock[1]);
- byte trailerBlock = 7;
- // Authenticate using key A
- Serial.println(F("Authenticating using key A..."));
- status = (MFRC522::StatusCode) RFID.PCD_Authenticate(MFRC522::PICC_CMD_MF_AUTH_KEY_A, trailerBlock, &key, &(RFID.uid));
- if (status != MFRC522::STATUS_OK)
- {
- Serial.print(F("PCD_Authenticate() failed: "));
- Serial.println(RFID.GetStatusCodeName(status));
- return '1';
- }
- // Show the whole sector as it currently is
- Serial.println(F("Current data in sector:"));
- RFID.PICC_DumpMifareClassicSectorToSerial(&(RFID.uid), &key, sector);
- Serial.println();
- // Read data from the block
- Serial.print(F("Reading data from block ")); Serial.print(blockAddr);
- Serial.println(F(" ..."));
- status = (MFRC522::StatusCode) RFID.MIFARE_Read(blockAddr, buffer, &size);
- if (status != MFRC522::STATUS_OK)
- {
- Serial.print(F("MIFARE_Read() failed: "));
- Serial.println(RFID.GetStatusCodeName(status));
- }
- Serial.print(F("Data in block ")); Serial.print(blockAddr); Serial.println(F(":"));
- dump_byte_array_RW(buffer, 16); Serial.println();
- Serial.println();
- // Authenticate using key B
- Serial.println(F("Authenticating again using key B..."));
- status = (MFRC522::StatusCode) RFID.PCD_Authenticate(MFRC522::PICC_CMD_MF_AUTH_KEY_B, trailerBlock, &key, &(RFID.uid));
- if (status != MFRC522::STATUS_OK) {
- Serial.print(F("PCD_Authenticate() failed: "));
- Serial.println(RFID.GetStatusCodeName(status));
- return '2';
- }
- // Write data to the block
- Serial.print(F("Writing data into block ")); Serial.print(blockAddr);
- Serial.println(F(" ..."));
- dump_byte_array_RW(dataBlock, 16); Serial.println();
- status = (MFRC522::StatusCode) RFID.MIFARE_Write(blockAddr, dataBlock, 16);
- if (status != MFRC522::STATUS_OK)
- {
- Serial.print(F("MIFARE_Write() failed: "));
- Serial.println(RFID.GetStatusCodeName(status));
- }
- Serial.println();
- // Read data from the block (again, should now be what we have written)
- Serial.print(F("Reading data from block ")); Serial.print(blockAddr);
- Serial.println(F(" ..."));
- status = (MFRC522::StatusCode) RFID.MIFARE_Read(blockAddr, buffer, &size);
- if (status != MFRC522::STATUS_OK)
- {
- Serial.print(F("MIFARE_Read() failed: "));
- Serial.println(RFID.GetStatusCodeName(status));
- }
- Serial.print(F("Data in block ")); Serial.print(blockAddr); Serial.println(F(":"));
- dump_byte_array_RW(buffer, 16); Serial.println();
- // Check that data in block is what we have written
- // by counting the number of bytes that are equal
- Serial.println(F("Checking result..."));
- byte count = 0;
- for (byte i = 0; i < 16; i++)
- {
- // Compare buffer (= what we've read) with dataBlock (= what we've written)
- if (buffer[i] == dataBlock[i])
- count++;
- }
- Serial.print(F("Number of bytes that match = ")); Serial.println(count);
- if (count == 16)
- {
- Serial.println(F("Success :-)"));
- }
- else
- {
- Serial.println(F("Failure, no match :-("));
- Serial.println(F(" perhaps the write didn't work properly..."));
- }
- Serial.println();
- // Dump the sector data
- Serial.println(F("Current data in sector:"));
- RFID.PICC_DumpMifareClassicSectorToSerial(&(RFID.uid), &key, sector);
- Serial.println();
- // Halt PICC
- RFID.PICC_HaltA();
- // Stop encryption on PCD
- RFID.PCD_StopCrypto1();
- }
- void Detect_RFID()
- {
- if ( ! RFID.PICC_IsNewCardPresent())
- {
- return;
- }
- read_RFID();
- int money = read_RFID_sector();
- money -= 60;
- delay(1000);
- write_rfid(money);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement