我正在尝试创建一个基于NFC的储物柜系统。每个储物柜都将连接到它自己的NFC读取器,该读取器全部由一个单独的arduino管理。
Here is my code:
#include <SPI.h>
#include <MFRC522.h>
#define SS_1_PIN 10 // Attach Reader #1
#define SS_2_PIN 8 // Attach Reader #2
#define RST_PIN 9
#define NR_OF_READERS 2 //
byte ssPins[] = {SS_1_PIN, SS_2_PIN};
int referTable[2] = {0,1};// Reference table
int lockerStatus[2] = {0,0}; //0 for Unoccuupied & 1 for Occupied
byte uidRecord[2][4]; // Array of UID to be Set
int cardBalance;
int lockerPrice = 20;
uint8_t reader;
MFRC522 mfrc522[NR_OF_READERS]; // Create MFRC522 instances
MFRC522::MIFARE_Key key;
void setup() {
Serial.begin(9600); // Initialize serial communications with the PC
while (!Serial); // Do nothing if no serial port is opened (added for Arduinos based on ATMEGA32U4)
SPI.begin(); // Init SPI bus
// Prepare the key (used both as key A and as key B)
// using FFFFFFFFFFFFh which is the default at chip delivery from the factory
for (byte i = 0; i < 6; i++) {
key.keyByte[i] = 0xFF;
}
Serial.println(F("Scan a MIFARE Classic PICC to demonstrate Value Block mode."));
Serial.print(F("Using key (for A and B):"));
dump_byte_array(key.keyByte, MFRC522::MF_KEY_SIZE);
Serial.println();
Serial.println(F("BEWARE: Data will be written to the PICC, in sector #1"));
for (reader = 0; reader < NR_OF_READERS; reader++) {
mfrc522[reader].PCD_Init(ssPins[reader], RST_PIN); // Init each MFRC522 card
Serial.print(F("Reader "));
Serial.print(reader);
Serial.print(F(": "));
mfrc522[reader].PCD_DumpVersionToSerial();
}
}
void loop() {
for (reader = 0; reader < NR_OF_READERS; reader++) {
// Look for new cards
if (mfrc522[reader].PICC_IsNewCardPresent() && mfrc522[reader].PICC_ReadCardSerial()) {
Serial.print(F("Reader "));
Serial.print(reader);
// Show some details of the PICC (that is: the tag/card)
Serial.print(F(": Card UID:"));
dump_byte_array(mfrc522[reader].uid.uidByte, mfrc522[reader].uid.size);
Serial.println();
Serial.print(F("PICC type: "));
MFRC522::PICC_Type piccType = mfrc522[reader].PICC_GetType(mfrc522[reader].uid.sak);
Serial.println(mfrc522[reader].PICC_GetTypeName(piccType));
// Halt PICC
mfrc522[reader].PICC_HaltA();
// Stop encryption on PCD
mfrc522[reader].PCD_StopCrypto1();
//LOCKER SETUP FILE STARTS HERE
// 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;
}
//Check if Current reader Locker is occupied
if (lockerStatus[reader] == 0) {
Serial.print(F("Locker is Available to use"));
Serial.println();
//Assign Locker to UID
for (byte i = 0; i < 4; i++) {
uidRecord[reader][i] = mfrc522[reader].uid.uidByte[i];
}
Serial.print(F("The UID:"));
dump_byte_array(mfrc522[reader].uid.uidByte, mfrc522[reader].uid.size);
Serial.print(F(" has been assgined to Reader "));
Serial.print(reader);
Serial.println();
//Subtract Amount on Sector1 B5
byte sector = 1;
byte valueBlockA = 5;
byte trailerBlock = 7;
MFRC522::StatusCode status;
byte buffer[18];
byte size = sizeof(buffer);
int32_t value;
// Authenticate using key A
Serial.println(F("Authenticating using key A..."));
status = mfrc522[reader].PCD_Authenticate(MFRC522::PICC_CMD_MF_AUTH_KEY_A, trailerBlock, &key, &(mfrc522[reader].uid));
if (status != MFRC522::STATUS_OK) {
Serial.print(F("PCD_Authenticate() failed:"));
Serial.println(mfrc522[reader].GetStatusCodeName(status));
return;
}
byte trailerBuffer[] = {
255, 255, 255, 255, 255, 255, // Keep default key A
0, 0, 0,
0,
255, 255, 255, 255, 255, 255}; // Keep default key B
当涉及多个阅读器时,我无法通过密钥A进行身份验证。当它只是一个阅读器时,我没有问题,但是当我添加第二个NFC阅读器时,我无法弄清楚身份验证部分出了什么问题。
我已经找到问题所在了
status = mfrc522[reader].PCD_Authenticate(MFRC522::PICC_CMD_MF_AUTH_KEY_A, trailerBlock, &key, &(mfrc522[reader].uid));
我认为是问题所在,因为
The UID: AB 3F 3E E9 has been assgined to Reader 0
Authenticating using key A...
PCD_Authenticate() failed:Timeout in communication.
是我在串行监视器上看到的错误。 我认为它与uid指针或尾部块或键有某种关系。但是我只是想不通。
P.S。问题区域后,我已省略了大部分代码。
任何帮助将不胜感激!