RFID mit dem Arduino programmieren: Unterschied zwischen den Versionen
(→Referenzen und Links) |
(→Video Erklärung) |
||
(Eine dazwischenliegende Version desselben Benutzers wird nicht angezeigt) | |||
Zeile 6: | Zeile 6: | ||
* MFRC522 --> Diese Library muss bei Verwendung der Arduino IDE mit Hilfe des Library Managers installiert werden | * MFRC522 --> Diese Library muss bei Verwendung der Arduino IDE mit Hilfe des Library Managers installiert werden | ||
* [https://de.wikipedia.org/wiki/Serial_Peripheral_Interface SPI Erklärung auf Wikipedia] | * [https://de.wikipedia.org/wiki/Serial_Peripheral_Interface SPI Erklärung auf Wikipedia] | ||
+ | |||
+ | = Video Erklärung = | ||
+ | {{#ev:youtube|https://youtu.be/gWKyRyll_nU|800}} | ||
+ | |||
+ | == Der Code zum Video == | ||
+ | <pre> | ||
+ | |||
+ | #include <MFRC522.h> | ||
+ | |||
+ | #define SS_PIN 10 | ||
+ | #define RST_PIN 13 | ||
+ | |||
+ | MFRC522 rfid(SS_PIN, RST_PIN); // Instance of the class | ||
+ | |||
+ | MFRC522::MIFARE_Key key; | ||
+ | |||
+ | // Init array that will store new NUID | ||
+ | byte nuidPICC[4]; | ||
+ | |||
+ | |||
+ | void setup() { | ||
+ | |||
+ | |||
+ | //while (!Serial); | ||
+ | Serial.begin(9600); | ||
+ | |||
+ | |||
+ | SPI.begin(); // Init SPI bus | ||
+ | rfid.PCD_Init(); // Init MFRC522 | ||
+ | |||
+ | //for (byte i = 0; i < 6; i++) { | ||
+ | // key.keyByte[i] = 0xFF; | ||
+ | //} | ||
+ | |||
+ | //Serial.println(F("This code scan the MIFARE Classsic NUID.")); | ||
+ | //Serial.print(F("Using the following key:")); | ||
+ | //printHex(key.keyByte, MFRC522::MF_KEY_SIZE); | ||
+ | |||
+ | |||
+ | |||
+ | } | ||
+ | |||
+ | void loop() { | ||
+ | checkRFID(); | ||
+ | } | ||
+ | |||
+ | |||
+ | void checkRFID() { | ||
+ | //Serial.println("Checking RFID"); | ||
+ | // RFID Reader | ||
+ | // Reset the loop if no new card present on the sensor/reader. This saves the entire process when idle. | ||
+ | if ( ! rfid.PICC_IsNewCardPresent()) | ||
+ | return; | ||
+ | |||
+ | // Verify if the NUID has been readed | ||
+ | if ( ! rfid.PICC_ReadCardSerial()) | ||
+ | return; | ||
+ | |||
+ | //Serial.print(F("PICC type: ")); | ||
+ | MFRC522::PICC_Type piccType = rfid.PICC_GetType(rfid.uid.sak); | ||
+ | //Serial.println(rfid.PICC_GetTypeName(piccType)); | ||
+ | |||
+ | // Check is the PICC of Classic MIFARE type | ||
+ | if (piccType != MFRC522::PICC_TYPE_MIFARE_MINI && | ||
+ | piccType != MFRC522::PICC_TYPE_MIFARE_1K && | ||
+ | piccType != MFRC522::PICC_TYPE_MIFARE_4K) { | ||
+ | Serial.println(F("Your tag is not of type MIFARE Classic.")); | ||
+ | return; | ||
+ | } | ||
+ | |||
+ | |||
+ | printDec(rfid.uid.uidByte, rfid.uid.size); | ||
+ | |||
+ | rfid.PICC_HaltA(); | ||
+ | |||
+ | // Stop encryption on PCD | ||
+ | rfid.PCD_StopCrypto1(); | ||
+ | } | ||
+ | |||
+ | /** | ||
+ | Helper routine to dump a byte array as hex values to Serial. | ||
+ | */ | ||
+ | void printHex(byte *buffer, byte bufferSize) { | ||
+ | for (byte i = 0; i < bufferSize; i++) { | ||
+ | Serial.print(buffer[i] < 0x10 ? " 0" : " "); | ||
+ | Serial.print(buffer[i], HEX); | ||
+ | } | ||
+ | } | ||
+ | |||
+ | /** | ||
+ | Helper routine to dump a byte array as dec values to Serial. | ||
+ | */ | ||
+ | void printDec(byte *buffer, byte bufferSize) { | ||
+ | Serial.println("Printing RFID Tag"); | ||
+ | |||
+ | for (byte i = 0; i < bufferSize; i++) { | ||
+ | //Serial.print(buffer[i] < 0x10 ? " 0" : " "); | ||
+ | Serial.print(buffer[i], DEC); | ||
+ | } | ||
+ | |||
+ | Serial.println(); | ||
+ | } | ||
+ | </pre> |
Aktuelle Version vom 24. August 2020, 14:04 Uhr
Bitte erst alle Kabel (Juper Wire) aufbauen, dann den Arduino über USB-Kabel anstecken. Ansonsten kann es sein, dass es nicht funktioniert.
Hinweise
- MFRC522 --> Diese Library muss bei Verwendung der Arduino IDE mit Hilfe des Library Managers installiert werden
- SPI Erklärung auf Wikipedia
Video Erklärung
Der Code zum Video
#include <MFRC522.h> #define SS_PIN 10 #define RST_PIN 13 MFRC522 rfid(SS_PIN, RST_PIN); // Instance of the class MFRC522::MIFARE_Key key; // Init array that will store new NUID byte nuidPICC[4]; void setup() { //while (!Serial); Serial.begin(9600); SPI.begin(); // Init SPI bus rfid.PCD_Init(); // Init MFRC522 //for (byte i = 0; i < 6; i++) { // key.keyByte[i] = 0xFF; //} //Serial.println(F("This code scan the MIFARE Classsic NUID.")); //Serial.print(F("Using the following key:")); //printHex(key.keyByte, MFRC522::MF_KEY_SIZE); } void loop() { checkRFID(); } void checkRFID() { //Serial.println("Checking RFID"); // RFID Reader // Reset the loop if no new card present on the sensor/reader. This saves the entire process when idle. if ( ! rfid.PICC_IsNewCardPresent()) return; // Verify if the NUID has been readed if ( ! rfid.PICC_ReadCardSerial()) return; //Serial.print(F("PICC type: ")); MFRC522::PICC_Type piccType = rfid.PICC_GetType(rfid.uid.sak); //Serial.println(rfid.PICC_GetTypeName(piccType)); // Check is the PICC of Classic MIFARE type if (piccType != MFRC522::PICC_TYPE_MIFARE_MINI && piccType != MFRC522::PICC_TYPE_MIFARE_1K && piccType != MFRC522::PICC_TYPE_MIFARE_4K) { Serial.println(F("Your tag is not of type MIFARE Classic.")); return; } printDec(rfid.uid.uidByte, rfid.uid.size); rfid.PICC_HaltA(); // Stop encryption on PCD rfid.PCD_StopCrypto1(); } /** Helper routine to dump a byte array as hex values to Serial. */ void printHex(byte *buffer, byte bufferSize) { for (byte i = 0; i < bufferSize; i++) { Serial.print(buffer[i] < 0x10 ? " 0" : " "); Serial.print(buffer[i], HEX); } } /** Helper routine to dump a byte array as dec values to Serial. */ void printDec(byte *buffer, byte bufferSize) { Serial.println("Printing RFID Tag"); for (byte i = 0; i < bufferSize; i++) { //Serial.print(buffer[i] < 0x10 ? " 0" : " "); Serial.print(buffer[i], DEC); } Serial.println(); }