feat: update rfid code

This commit is contained in:
2021-10-02 11:57:18 +02:00
parent 0d45f8c8d0
commit 00468f0b3a
5 changed files with 18 additions and 109 deletions

Submodule FW/leo_muziekdoos_esp32/lib/rfid_PN532 deleted from 12663d9dc3

View File

@@ -9,4 +9,5 @@
#define NFC_SS 25 #define NFC_SS 25
#define NFC_SCK 18 #define NFC_SCK 18
#define NFC_MOSI 23 #define NFC_MOSI 23
#define NFC_MISO 19 #define NFC_MISO 19
#define NFC_RST 22 //not connectedx

View File

@@ -8,8 +8,8 @@
void setup() void setup()
{ {
Serial.begin(115200); Serial.begin(115200);
delay(1000); delay(2000);
initStorage(); initStorage();
initAudio(); initAudio();

View File

@@ -1,117 +1,24 @@
#include "rfid.h" #include "rfid.h"
#define RST_PIN 9 // Configurable, see typical pin layout above
#define SS_PIN 10 // Configurable, see typical pin layout above
MFRC522 mfrc522(SS_PIN, RST_PIN); // Create MFRC522 instance PN532_SPI pn532spi(SPI, NFC_SS, NFC_SCK, NFC_MISO, NFC_MOSI);
NfcAdapter nfc = NfcAdapter(pn532spi);
//*****************************************************************************************// //*****************************************************************************************//
void initRfid() { void initRfid()
{
//int8_t sck=-1, int8_t miso=-1, int8_t mosi=-1, int8_t ss=-1); //int8_t sck=-1, int8_t miso=-1, int8_t mosi=-1, int8_t ss=-1);
SPI.begin(NFC_SCK,NFC_MISO,NFC_MOSI,NFC_SS ); // Init SPI bus nfc.begin(true);
mfrc522.PCD_Init(); // Init MFRC522 card Serial.println(F("rfid init done")); //shows in serial that it is ready to read
Serial.println(F("Read personal data on a MIFARE PICC:")); //shows in serial that it is ready to read
} }
//*****************************************************************************************// //*****************************************************************************************//
void handleRfid() { void handleRfid()
{
// Prepare key - all keys are set to FFFFFFFFFFFFh at chip delivery from the factory. if (nfc.tagPresent())
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()) {
return;
}
// Select one of the cards
if ( ! mfrc522.PICC_ReadCardSerial()) {
return;
}
Serial.println(F("**Card Detected:**"));
//-------------------------------------------
mfrc522.PICC_DumpDetailsToSerial(&(mfrc522.uid)); //dump some details about the card
//mfrc522.PICC_DumpToSerial(&(mfrc522.uid)); //uncomment this to see all blocks in hex
//-------------------------------------------
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) NfcTag tag = nfc.read();
{ tag.print();
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
mfrc522.PICC_HaltA();
mfrc522.PCD_StopCrypto1();
} }
//*****************************************************************************************// //*****************************************************************************************//

View File

@@ -3,7 +3,9 @@
#include "board.h" #include "board.h"
#include <SPI.h> #include <SPI.h>
#include <MFRC522.h> #include "PN532_SPI.h"
#include "PN532.h"
#include "NfcAdapter.h"
void initRfid(void); void initRfid(void);
void handleRfid(void); void handleRfid(void);