feat: update rfid code
This commit is contained in:
@@ -1,82 +1,154 @@
|
|||||||
#include "rfid.h"
|
#include "rfid.h"
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
// void initRfid(void)
|
||||||
|
// {
|
||||||
|
|
||||||
|
// }
|
||||||
|
|
||||||
|
// void handleRfid(void)
|
||||||
|
// {
|
||||||
|
|
||||||
|
// }
|
||||||
|
|
||||||
/*
|
/*
|
||||||
--------------------------------------------------------------------------------------------------------------------
|
* Initial Author: ryand1011 (https://github.com/ryand1011)
|
||||||
Example sketch/program showing how to read data from a PICC to serial.
|
*
|
||||||
--------------------------------------------------------------------------------------------------------------------
|
* Reads data written by a program such as "rfid_write_personal_data.ino"
|
||||||
This is a MFRC522 library example; for further details and other examples see: https://github.com/miguelbalboa/rfid
|
*
|
||||||
|
* See: https://github.com/miguelbalboa/rfid/tree/master/examples/rfid_write_personal_data
|
||||||
Example sketch/program showing how to read data from a PICC (that is: a RFID Tag or Card) using a MFRC522 based RFID
|
*
|
||||||
Reader on the Arduino SPI interface.
|
* Uses MIFARE RFID card using RFID-RC522 reader
|
||||||
|
* Uses MFRC522 - Library
|
||||||
When the Arduino and the MFRC522 module are connected (see the pin layout below), load this sketch into Arduino IDE
|
* -----------------------------------------------------------------------------------------
|
||||||
then verify/compile and upload it. To see the output: use Tools, Serial Monitor of the IDE (hit Ctrl+Shft+M). When
|
* MFRC522 Arduino Arduino Arduino Arduino Arduino
|
||||||
you present a PICC (that is: a RFID Tag or Card) at reading distance of the MFRC522 Reader/PCD, the serial output
|
* Reader/PCD Uno/101 Mega Nano v3 Leonardo/Micro Pro Micro
|
||||||
will show the ID/UID, type and any data blocks it can read. Note: you may see "Timeout in communication" messages
|
* Signal Pin Pin Pin Pin Pin Pin
|
||||||
when removing the PICC from reading distance too early.
|
* -----------------------------------------------------------------------------------------
|
||||||
|
* RST/Reset RST 9 5 D9 RESET/ICSP-5 RST
|
||||||
@license Released into the public domain.
|
* SPI SS SDA(SS) 10 53 D10 10 10
|
||||||
|
* SPI MOSI MOSI 11 / ICSP-4 51 D11 ICSP-4 16
|
||||||
Typical pin layout used:
|
* SPI MISO MISO 12 / ICSP-1 50 D12 ICSP-1 14
|
||||||
-----------------------------------------------------------------------------------------
|
* SPI SCK SCK 13 / ICSP-3 52 D13 ICSP-3 15
|
||||||
MFRC522 Arduino Arduino Arduino Arduino Arduino
|
*
|
||||||
Reader/PCD Uno/101 Mega Nano v3 Leonardo/Micro Pro Micro Due/Mega2560
|
* More pin layouts for other boards can be found here: https://github.com/miguelbalboa/rfid#pin-layout
|
||||||
Signal Pin Pin Pin Pin Pin Pin
|
|
||||||
-----------------------------------------------------------------------------------------
|
|
||||||
RST/Reset RST 9 5 D9 RESET/ICSP-5 23 9
|
|
||||||
scl CLK 5 5 3 22
|
|
||||||
sda SC 4 4 2 21
|
|
||||||
|
|
||||||
*/
|
*/
|
||||||
|
|
||||||
constexpr uint8_t RST_PIN = 2; // Configurable, see typical pin layout above
|
#include <SPI.h>
|
||||||
|
#include <MFRC522.h>
|
||||||
|
|
||||||
// The default address of the MFRC522 is 0X3C -- specify it here if you
|
#define RST_PIN 9 // Configurable, see typical pin layout above
|
||||||
// have it configured differently (pull up/down pins on the chip).
|
#define SS_PIN 10 // Configurable, see typical pin layout above
|
||||||
//
|
|
||||||
// MFRC522_I2C mfrc522(RST_PIN, 0x28 /*, chipAddr */); // Create MFRC522 instance
|
|
||||||
|
|
||||||
TwoWire i2cBus = TwoWire(0);
|
MFRC522 mfrc522(SS_PIN, RST_PIN); // Create MFRC522 instance
|
||||||
MFRC522_I2C dev = MFRC522_I2C(RST_PIN, 0x28, i2cBus);
|
|
||||||
MFRC522 mfrc522 = MFRC522((MFRC522_BUS_DEVICE)dev);
|
|
||||||
|
|
||||||
|
//*****************************************************************************************//
|
||||||
void initRfid() {
|
void initRfid() {
|
||||||
|
SPI.begin(); // Init SPI bus
|
||||||
i2cBus.begin(5, 4, 400000);
|
mfrc522.PCD_Init(); // Init MFRC522 card
|
||||||
mfrc522.PCD_Init(); // Init MFRC522
|
Serial.println(F("Read personal data on a MIFARE PICC:")); //shows in serial that it is ready to read
|
||||||
mfrc522.PCD_DumpVersionToSerial(); // Show details of PCD - MFRC522 Card Reader details
|
|
||||||
|
|
||||||
Serial.println(F("Scan PICC to see UID, SAK, type, and data blocks..."));
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
//*****************************************************************************************//
|
||||||
void handleRfid() {
|
void handleRfid() {
|
||||||
// Look for new cards
|
|
||||||
|
// Prepare key - all keys are set to FFFFFFFFFFFFh at chip delivery from the factory.
|
||||||
|
MFRC522::MIFARE_Key key;
|
||||||
|
for (byte i = 0; i < 6; i++) key.keyByte[i] = 0xFF;
|
||||||
|
|
||||||
|
//some variables we need
|
||||||
|
byte block;
|
||||||
|
byte len;
|
||||||
|
MFRC522::StatusCode status;
|
||||||
|
|
||||||
|
//-------------------------------------------
|
||||||
|
|
||||||
|
// Reset the loop if no new card present on the sensor/reader. This saves the entire process when idle.
|
||||||
if ( ! mfrc522.PICC_IsNewCardPresent()) {
|
if ( ! mfrc522.PICC_IsNewCardPresent()) {
|
||||||
return; // no card in sight.
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Select one of the cards
|
// Select one of the cards
|
||||||
if ( ! mfrc522.PICC_ReadCardSerial()) {
|
if ( ! mfrc522.PICC_ReadCardSerial()) {
|
||||||
Serial.println("Bad read (was card removed too quickly?)");
|
|
||||||
return;
|
|
||||||
};
|
|
||||||
|
|
||||||
if ( mfrc522.uid.size == 0) {
|
|
||||||
Serial.println("Bad card read (size = 0)");
|
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
char buff[sizeof(mfrc522.uid.uidByte)* 5] = { 0 };
|
Serial.println(F("**Card Detected:**"));
|
||||||
for (int i = 0; i < mfrc522.uid.size; i++) {
|
|
||||||
char tag[5]; // 3 digits, dash and \0.
|
//-------------------------------------------
|
||||||
snprintf(buff, sizeof(buff), "%s%d", i ? "-" : "", mfrc522.uid.uidByte[i]);
|
|
||||||
strncat(buff, tag, sizeof(tag));
|
mfrc522.PICC_DumpDetailsToSerial(&(mfrc522.uid)); //dump some details about the card
|
||||||
};
|
|
||||||
Serial.println("Good scan: ");
|
//mfrc522.PICC_DumpToSerial(&(mfrc522.uid)); //uncomment this to see all blocks in hex
|
||||||
Serial.println(buff);
|
|
||||||
|
//-------------------------------------------
|
||||||
|
|
||||||
|
Serial.print(F("Name: "));
|
||||||
|
|
||||||
|
byte buffer1[18];
|
||||||
|
|
||||||
|
block = 4;
|
||||||
|
len = 18;
|
||||||
|
|
||||||
|
//------------------------------------------- GET FIRST NAME
|
||||||
|
status = mfrc522.PCD_Authenticate(MFRC522::PICC_CMD_MF_AUTH_KEY_A, 4, &key, &(mfrc522.uid)); //line 834 of MFRC522.cpp file
|
||||||
|
if (status != MFRC522::STATUS_OK) {
|
||||||
|
Serial.print(F("Authentication failed: "));
|
||||||
|
Serial.println(mfrc522.GetStatusCodeName(status));
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
status = mfrc522.MIFARE_Read(block, buffer1, &len);
|
||||||
|
if (status != MFRC522::STATUS_OK) {
|
||||||
|
Serial.print(F("Reading failed: "));
|
||||||
|
Serial.println(mfrc522.GetStatusCodeName(status));
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
//PRINT FIRST NAME
|
||||||
|
for (uint8_t i = 0; i < 16; i++)
|
||||||
|
{
|
||||||
|
if (buffer1[i] != 32)
|
||||||
|
{
|
||||||
|
Serial.write(buffer1[i]);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
Serial.print(" ");
|
||||||
|
|
||||||
|
//---------------------------------------- GET LAST NAME
|
||||||
|
|
||||||
|
byte buffer2[18];
|
||||||
|
block = 1;
|
||||||
|
|
||||||
|
status = mfrc522.PCD_Authenticate(MFRC522::PICC_CMD_MF_AUTH_KEY_A, 1, &key, &(mfrc522.uid)); //line 834
|
||||||
|
if (status != MFRC522::STATUS_OK) {
|
||||||
|
Serial.print(F("Authentication failed: "));
|
||||||
|
Serial.println(mfrc522.GetStatusCodeName(status));
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
status = mfrc522.MIFARE_Read(block, buffer2, &len);
|
||||||
|
if (status != MFRC522::STATUS_OK) {
|
||||||
|
Serial.print(F("Reading failed: "));
|
||||||
|
Serial.println(mfrc522.GetStatusCodeName(status));
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
//PRINT LAST NAME
|
||||||
|
for (uint8_t i = 0; i < 16; i++) {
|
||||||
|
Serial.write(buffer2[i] );
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
//----------------------------------------
|
||||||
|
|
||||||
|
Serial.println(F("\n**End Reading**\n"));
|
||||||
|
|
||||||
|
delay(1000); //change value if you want to read cards faster
|
||||||
|
|
||||||
// disengage with the card.
|
|
||||||
//
|
|
||||||
mfrc522.PICC_HaltA();
|
mfrc522.PICC_HaltA();
|
||||||
}
|
mfrc522.PCD_StopCrypto1();
|
||||||
|
}
|
||||||
|
//*****************************************************************************************//
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user