我正在尝试使用带有NFC防护罩的arduino板来解锁我的车门。
董事会:Arduino UNO Rev 3 NFC Shield:Seeed Studio的v2.0b
因此,当存在NFC标签时,信号将伺服器旋转180度以解锁门。目前的问题是,我希望只有一个NFC标签能够解锁/锁定门,而不仅仅是任何一个。
目前任何NFC标签都可以转动伺服。
有没有人知道要调用的功能只返回NFC标签的UID,然后可以将其与已知的NFC标签进行比较。
http://www.seeedstudio.com/wiki/NFC_Shield_V2.0
#include <SPI.h>
#include "PN532_SPI.h"
#include "PN532.h"
#include "NfcAdapter.h"
String const myUID = "1B B3 C6 EF"; // replace this UID with your NFC tag's UID
int const greenLedPin = 3; // green led used for correct key notification
int const redLedPin = 4; // red led used for incorrect key notification
PN532_SPI interface(SPI, 10); // create a SPI interface for the shield with the SPI CS terminal at digital pin 10
NfcAdapter nfc = NfcAdapter(interface); // create an NFC adapter object
void setup(void) {
Serial.begin(115200); // start serial comm
Serial.println("NDEF Reader");
nfc.begin(); // begin NFC comm
// make LED pins outputs
pinMode(greenLedPin,OUTPUT);
pinMode(redLedPin,OUTPUT);
// turn off the LEDs
digitalWrite(greenLedPin,LOW);
digitalWrite(redLedPin,LOW);
}
void loop(void) {
Serial.println("Scanning...");
if (nfc.tagPresent()) // check if an NFC tag is present on the antenna area
{
NfcTag tag = nfc.read(); // read the NFC tag
String scannedUID = tag.getUidString(); // get the NFC tag's UID
if( myUID.compareTo(scannedUID) == 0) // compare the NFC tag's UID with the correct tag's UID (a match exists when compareTo returns 0)
{
// The correct NFC tag was used
Serial.println("Correct Key");
// Blink the green LED and make sure the RED led is off
digitalWrite(greenLedPin,HIGH);
digitalWrite(redLedPin,LOW);
delay(500);
digitalWrite(greenLedPin,LOW);
delay(500);
digitalWrite(greenLedPin,HIGH);
delay(500);
digitalWrite(greenLedPin,LOW);
// put your here to trigger the unlocking mechanism (e.g. motor, transducer)
}else{
// an incorrect NFC tag was used
Serial.println("Incorrect key");
// blink the red LED and make sure the green LED is off
digitalWrite(greenLedPin,LOW);
digitalWrite(redLedPin,HIGH);
delay(500);
digitalWrite(redLedPin,LOW);
delay(500);
digitalWrite(redLedPin,HIGH);
delay(500);
digitalWrite(redLedPin,LOW);
// DO NOT UNLOCK! an incorrect NFC tag was used.
// put your code here to trigger an alarm (e.g. buzzard, speaker) or do something else
}
}
delay(2000);
}
此代码有效。
答案 0 :(得分:2)
您在检测到任何标记时表明您当前正在解锁。所以你必须已经轮询标签。如果您使用的是原始的seeedstudio库,则可以使用inListPassiveTarget()
方法或(如已经指明的那样)readPassiveTargetID()
方法对任何被动目标执行轮询。
虽然inListPassiveTarget()
只返回一个布尔值,表示是否存在任何目标,但readPassiveTargetID()
方法为您提供了一组配置参数,并允许您检索防冲突标识符(例如,ISO 14443 A类的UID):
bool PN532::readPassiveTargetID(uint8_t cardbaudrate, uint8_t *uid, uint8_t *uidLength, uint16_t timeout);
您可以使用以下方法:
uint8_t uid[16] = { 0 };
uint8_t uidLen = 0;
if (nfc.readPassiveTargetID(PN532_MIFARE_ISO14443A, &uid[0], &uidLen)) {
// uid will now contain uidLen bytes of the anti-collision identifier
}
如果您要轮询除ISO 14443 A类以外的其他卡,您可以使用以下定义而不是PN532_MIFARE_ISO14443A
:
// ISO 14443 Type B
#define PN532_BAUD_ISO14443B (0x03)
// FeliCa 212 kbps
#define PN532_BAUD_FELICA212 (0x01)
// FeliCa 424 kbps
#define PN532_BAUD_FELICA424 (0x02)
// Jewel Tag (NFC Forum Type 1)
#define PN532_BAUD_JEWEL (0x04)
最后,我通常会注意到使用标记的UID进行访问控制:不要这样做!在许多现有系统中,这已被证明是一个非常糟糕的主意。有关详细信息,请参阅这些帖子:
答案 1 :(得分:1)
我也是这样做的,使用NFC库示例中的示例ReadTag,我有UID:
Serial.println("\nScan electronic key\n");
if (nfc.tagPresent())
{
NfcTag tag = nfc.read();
Serial.println(tag.getTagType());
String idnumber = tag.getUidString();
Serial.print("UID: ");Serial.println(idnumber);
例如,这提供了来自Mifare Classic&#39;丸剂的30 5C 6F 80。标签。经过一些分析后,这将作为格式化的字符串11从库中返回,因此我将其与进行比较
String valid = ("30 5C 6F 80") ;
这种比较有效:
if (valid == idnumber) {
Serial.println("Yes") ;
// simulate door open by turning LED on
digitalWrite(lockopen, HIGH);
delay(lockopen_interval);
digitalWrite(lockopen, LOW);
} else {
Serial.println("No") ;
}
答案 2 :(得分:0)
我相信你可以使用:
readPassiveTargetID(PN532_MIFARE_ISO14443A)
获取ID。