This shows you the differences between two versions of the page.
pm:prj2023:tmiu:card_reader [2023/05/30 01:19] teodor.macoveiciuc |
pm:prj2023:tmiu:card_reader [2023/05/30 20:21] (current) teodor.macoveiciuc |
||
---|---|---|---|
Line 1: | Line 1: | ||
- | ====== Card reader ====== | + | ====== Card cloner ====== |
===== Introducere ===== | ===== Introducere ===== | ||
<note tip> | <note tip> | ||
- | Scopul proiectului este realizarea unui cititor de carduri, un aparat care permite citirea unui card/cartele prin intermediul unui modul special pentru scrierea si citirea cardurilor. Reusirea citirii cardului va fi semnalata de aprinderea unui LED. | + | Scopul proiectului este realizarea unui copiator de carduri, un aparat care permite citirea unui card/cartele prin intermediul unui modul special pentru scrierea si citirea cardurilor si copierea datelor de pe un card pe altul.. Reusirea citirii cardului va fi semnalata de aprinderea unui LED. |
</note> | </note> | ||
===== Descriere generală ===== | ===== Descriere generală ===== | ||
Line 34: | Line 34: | ||
**Cod** | **Cod** | ||
- | Functia void setup() este folosita pentru a se initializa circuitul | + | #include <SPI.h> |
- | Serial.begin(9600); | + | #include <MFRC522.h> |
- | SPI.begin(); | + | |
- | rfid.PCD_Init(); | + | |
- | pinMode(LED_PIN, OUTPUT); | + | |
- | Functia void loop() este principala functie care verifica daca un nou card exista. Daca gaseste un card, determina tipul acestuia, ii citeste ID-ul, iar pe baza acestuia decide daca sa lucreze mai departe sau nu. Daca da, retine cardul si opreste algoritmul. | + | #define RST_PIN 9 |
- | if (isNewCardPresent() && readCardSerial()) { | + | #define SS_PIN 10 |
- | MFRC522::PICC_Type piccType = getCardType(); | + | |
- | if (isMifareClassic(piccType)) { | + | MFRC522 mfrc522(SS_PIN, RST_PIN); |
- | String cardID = getCardID(); | + | |
- | Serial.print("Card ID: "); | + | byte buffer[18]; |
- | Serial.println(cardID); | + | byte block; |
- | if (cardID.equals("20:6B:18:83")) { | + | byte waarde[64][16]; |
- | turnOnLED(); | + | MFRC522::StatusCode status; |
- | } else { | + | |
- | turnOffLED(); | + | MFRC522::MIFARE_Key key; |
- | } | + | |
- | } else { | + | #define NR_KNOWN_KEYS 8 |
- | Serial.println("Your tag is not of type MIFARE Classic."); | + | |
+ | byte knownKeys[NR_KNOWN_KEYS][MFRC522::MF_KEY_SIZE] = { | ||
+ | {0xff, 0xff, 0xff, 0xff, 0xff, 0xff}, | ||
+ | {0xa0, 0xa1, 0xa2, 0xa3, 0xa4, 0xa5}, | ||
+ | {0xb0, 0xb1, 0xb2, 0xb3, 0xb4, 0xb5}, | ||
+ | {0x4d, 0x3a, 0x99, 0xc3, 0x51, 0xdd}, | ||
+ | {0x1a, 0x98, 0x2c, 0x7e, 0x45, 0x9a}, | ||
+ | {0xd3, 0xf7, 0xd3, 0xf7, 0xd3, 0xf7}, | ||
+ | {0xaa, 0xbb, 0xcc, 0xdd, 0xee, 0xff}, | ||
+ | {0x00, 0x00, 0x00, 0x00, 0x00, 0x00} | ||
+ | }; | ||
+ | |||
+ | char choice; | ||
+ | |||
+ | void setup() { | ||
+ | Serial.begin(9600); | ||
+ | while (!Serial); | ||
+ | SPI.begin(); | ||
+ | mfrc522.PCD_Init(); | ||
+ | pinMode(7, OUTPUT); | ||
+ | Serial.println(F("Try the most used default keys to print block 0 to 63 of a MIFARE PICC.")); | ||
+ | Serial.println("1.Read card \n2.Write to card \n3.Copy the data."); | ||
+ | |||
+ | for (byte i = 0; i < 6; i++) { | ||
+ | key.keyByte[i] = 0xFF; | ||
} | } | ||
- | haltCard(); | + | } |
- | stopCrypto1(); | + | |
+ | 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); | ||
+ | } | ||
+ | } | ||
+ | |||
+ | void dump_byte_array1(byte *buffer, byte bufferSize) { | ||
+ | for (byte i = 0; i < bufferSize; i++) { | ||
+ | Serial.print(buffer[i] < 0x10 ? " 0" : " "); | ||
+ | Serial.write(buffer[i]); | ||
} | } | ||
- | + | } | |
- | Functiile | + | |
- | bool isNewCardPresent() | + | |
- | return rfid.PICC_IsNewCardPresent(); | + | |
- | | + | |
- | bool readCardSerial() | + | |
- | return rfid.PICC_ReadCardSerial(); | + | bool try_key(MFRC522::MIFARE_Key *key) |
- | verifica daca exista un card si ii citeste numarul de serie. | + | { |
+ | bool result = false; | ||
+ | |||
+ | for(byte block = 0; block < 64; block++){ | ||
+ | |||
+ | status = mfrc522.PCD_Authenticate(MFRC522::PICC_CMD_MF_AUTH_KEY_A, block, key, &(mfrc522.uid)); | ||
+ | if (status != MFRC522::STATUS_OK) { | ||
+ | Serial.print(F("PCD_Authenticate() failed: ")); | ||
+ | Serial.println(mfrc522.GetStatusCodeName(status)); | ||
+ | return false; | ||
+ | } | ||
+ | byte byteCount = sizeof(buffer); | ||
+ | status = mfrc522.MIFARE_Read(block, buffer, &byteCount); | ||
+ | if (status != MFRC522::STATUS_OK) { | ||
+ | Serial.print(F("MIFARE_Read() failed: ")); | ||
+ | Serial.println(mfrc522.GetStatusCodeName(status)); | ||
+ | } | ||
+ | else { | ||
+ | result = true; | ||
+ | Serial.print(F("Success with key:")); | ||
+ | dump_byte_array((*key).keyByte, MFRC522::MF_KEY_SIZE); | ||
+ | Serial.println(); | ||
+ | | ||
+ | Serial.print(F("Block ")); Serial.print(block); Serial.print(F(":")); | ||
+ | dump_byte_array1(buffer, 16); | ||
+ | Serial.println(); | ||
+ | | ||
+ | for (int p = 0; p < 16; p++) | ||
+ | { | ||
+ | waarde [block][p] = buffer[p]; | ||
+ | Serial.print(waarde[block][p]); | ||
+ | Serial.print(" "); | ||
+ | } | ||
+ | | ||
+ | } | ||
+ | } | ||
+ | Serial.println(); | ||
+ | | ||
+ | Serial.println("1.Read card \n2.Write to card \n3.Copy the data."); | ||
- | Functiile | + | mfrc522.PICC_HaltA(); |
- | MFRC522::PICC_Type getCardType() | + | mfrc522.PCD_StopCrypto1(); |
- | return rfid.PICC_GetType(rfid.uid.sak); | + | return result; |
+ | |||
+ | start(); | ||
+ | } | ||
+ | void loop() { | ||
+ | start(); | ||
+ | | ||
+ | } | ||
- | bool isMifareClassic(MFRC522::PICC_Type piccType) { | + | void start(){ |
- | return piccType == MFRC522::PICC_TYPE_MIFARE_MINI || | + | choice = Serial.read(); |
- | piccType == MFRC522::PICC_TYPE_MIFARE_1K || | + | |
- | piccType == MFRC522::PICC_TYPE_MIFARE_4K; | + | if(choice == '1') |
+ | { | ||
+ | Serial.println("Read the card"); | ||
+ | keuze1(); | ||
+ | digitalWrite(7, HIGH); | ||
+ | delay(2000); | ||
+ | digitalWrite(7, LOW); | ||
+ | | ||
+ | } | ||
+ | else if(choice == '2') | ||
+ | { | ||
+ | Serial.println("See what is in the variables"); | ||
+ | keuze2(); | ||
+ | digitalWrite(7, HIGH); | ||
+ | delay(2000); | ||
+ | digitalWrite(7, LOW); | ||
+ | } | ||
+ | else if(choice == '3') | ||
+ | { | ||
+ | Serial.println("Copying the data on to the new card"); | ||
+ | keuze3(); | ||
+ | digitalWrite(7, HIGH); | ||
+ | delay(2000); | ||
+ | digitalWrite(7, LOW); | ||
+ | } | ||
+ | } | ||
- | String getCardID() | + | void keuze2(){ |
- | String cardID = ""; | + | |
- | for (byte i = 0; i < 4; i++) { | + | for(block = 4; block <= 62; block++){ |
- | cardID += (rfid.uid.uidByte[i] < 0x10 ? "0" : ""); | + | if(block == 7 || block == 11 || block == 15 || block == 19 || block == 23 || block == 27 || block == 31 || block == 35 || block == 39 || block == 43 || block == 47 || block == 51 || block == 55 || block == 59){ |
- | cardID += String(rfid.uid.uidByte[i], HEX); | + | block ++; |
- | if (i != 3) { | + | |
- | cardID += ":"; | + | |
} | } | ||
+ | | ||
+ | Serial.print(F("Writing data into block ")); | ||
+ | Serial.print(block); | ||
+ | Serial.println("\n"); | ||
+ | | ||
+ | for(int j = 0; j < 16; j++){ | ||
+ | Serial.print(waarde[block][j]); | ||
+ | Serial.print(" "); | ||
+ | } | ||
+ | Serial.println("\n"); | ||
+ | | ||
} | } | ||
- | cardID.toUpperCase(); | + | |
- | return cardID; | + | Serial.println("1.Read card \n2.Write to card \n3.Copy the data."); |
- | citesc tipul cardului apeland functia PICC_GetType() din libraria MFRC522, verifica daca cardul e de tipul MIFARE Classic si citesc ID-ul cardului astfel incat sa concateneze IU (identificatorul unic) pentru ca la final ID-ul cardului sa fie de tip string. | + | start(); |
+ | } | ||
- | Functiile | + | void keuze3(){ |
- | void turnOnLED() | + | Serial.println("Insert new card..."); |
- | digitalWrite(LED_PIN, HIGH); | + | if ( ! mfrc522.PICC_IsNewCardPresent()) |
+ | return; | ||
+ | if ( ! mfrc522.PICC_ReadCardSerial()) | ||
+ | return; | ||
- | void turnOffLED() | + | Serial.print(F("Card UID:")); |
- | digitalWrite(LED_PIN, LOW); | + | dump_byte_array(mfrc522.uid.uidByte, mfrc522.uid.size); |
+ | Serial.println(); | ||
+ | Serial.print(F("PICC type: ")); | ||
+ | MFRC522::PICC_Type piccType = mfrc522.PICC_GetType(mfrc522.uid.sak); | ||
+ | Serial.println(mfrc522.PICC_GetTypeName(piccType)); | ||
+ | |||
+ | for (byte i = 0; i < 6; i++) { | ||
+ | key.keyByte[i] = 0xFF; | ||
+ | } | ||
+ | for(int i = 4; i <= 62; i++){ | ||
+ | if(i == 7 || i == 11 || i == 15 || i == 19 || i == 23 || i == 27 || i == 31 || i == 35 || i == 39 || i == 43 || i == 47 || i == 51 || i == 55 || i == 59){ | ||
+ | i++; | ||
+ | } | ||
+ | block = i; | ||
+ | | ||
+ | Serial.println(F("Authenticating using key A...")); | ||
+ | status = (MFRC522::StatusCode) mfrc522.PCD_Authenticate(MFRC522::PICC_CMD_MF_AUTH_KEY_A, block, &key, &(mfrc522.uid)); | ||
+ | if (status != MFRC522::STATUS_OK) { | ||
+ | Serial.print(F("PCD_Authenticate() failed: ")); | ||
+ | Serial.println(mfrc522.GetStatusCodeName(status)); | ||
+ | return; | ||
+ | } | ||
+ | | ||
+ | Serial.println(F("Authenticating again using key B...")); | ||
+ | status = (MFRC522::StatusCode) mfrc522.PCD_Authenticate(MFRC522::PICC_CMD_MF_AUTH_KEY_B, block, &key, &(mfrc522.uid)); | ||
+ | if (status != MFRC522::STATUS_OK) { | ||
+ | Serial.print(F("PCD_Authenticate() failed: ")); | ||
+ | Serial.println(mfrc522.GetStatusCodeName(status)); | ||
+ | return; | ||
+ | } | ||
+ | | ||
+ | Serial.print(F("Writing data into block ")); | ||
+ | Serial.print(block); | ||
+ | Serial.println("\n"); | ||
+ | | ||
+ | dump_byte_array(waarde[block], 16); | ||
+ | | ||
+ | | ||
+ | status = (MFRC522::StatusCode) mfrc522.MIFARE_Write(block, waarde[block], 16); | ||
+ | if (status != MFRC522::STATUS_OK) { | ||
+ | Serial.print(F("MIFARE_Write() failed: ")); | ||
+ | Serial.println(mfrc522.GetStatusCodeName(status)); | ||
+ | } | ||
+ | | ||
+ | | ||
+ | Serial.println("\n"); | ||
+ | |||
+ | } | ||
+ | mfrc522.PICC_HaltA(); | ||
+ | mfrc522.PCD_StopCrypto1(); | ||
+ | | ||
+ | Serial.println("1.Read card \n2.Write to card \n3.Copy the data."); | ||
+ | start(); | ||
+ | } | ||
- | void haltCard() | + | void keuze1(){ |
- | rfid.PICC_HaltA(); | + | Serial.println("Insert card..."); |
+ | if ( ! mfrc522.PICC_IsNewCardPresent()) | ||
+ | return; | ||
+ | if ( ! mfrc522.PICC_ReadCardSerial()) | ||
+ | return; | ||
- | void stopCrypto1() | + | Serial.print(F("Card UID:")); |
- | rfid.PCD_StopCrypto1(); | + | dump_byte_array(mfrc522.uid.uidByte, mfrc522.uid.size); |
- | se ocupa de pornirea/oprirea LED-ului, cat si oprirea comunicarii dintre card si algoritm. | + | Serial.println(); |
+ | Serial.print(F("PICC type: ")); | ||
+ | MFRC522::PICC_Type piccType = mfrc522.PICC_GetType(mfrc522.uid.sak); | ||
+ | Serial.println(mfrc522.PICC_GetTypeName(piccType)); | ||
+ | |||
+ | MFRC522::MIFARE_Key key; | ||
+ | for (byte k = 0; k < NR_KNOWN_KEYS; k++) { | ||
+ | for (byte i = 0; i < MFRC522::MF_KEY_SIZE; i++) { | ||
+ | key.keyByte[i] = knownKeys[k][i]; | ||
+ | } | ||
+ | if (try_key(&key)) { | ||
+ | break; | ||
+ | } | ||
+ | } | ||
+ | } | ||
</note> | </note> | ||
Line 115: | Line 297: | ||
<note tip> | <note tip> | ||
- | **Work in progress...** | + | S-a reusit citirea, copierea si transferul datelor unui card pe celalalt. Din pacate nu merge si cu cartela de camin caci e pe alta frecventa. |
</note> | </note> | ||
===== Concluzii ===== | ===== Concluzii ===== | ||
+ | <note tip> | ||
+ | Acest proiect m-a facut sa fiu mai curios despre ce device-uri poti concepe folosind materia predata si in continuare voi face mai mult research pe acest domeniu. | ||
+ | </note> | ||
===== Download ===== | ===== Download ===== | ||
<note warning> | <note warning> | ||
- | **Work in progress...** | + | |
</note> | </note> | ||
Line 129: | Line 314: | ||
<note tip> | <note tip> | ||
- | **Work in progress...** | + | |
</note> | </note> | ||
Line 135: | Line 320: | ||
<note> | <note> | ||
- | **Work in progress...** | + | Wikipedia, youtube si niste site-uri mai shady. |
</note> | </note> | ||
<html><a class="media mediafile mf_pdf" href="?do=export_pdf">Export to PDF</a></html> | <html><a class="media mediafile mf_pdf" href="?do=export_pdf">Export to PDF</a></html> | ||