我正在尝试使用arduino板与NFC盾牌,以便能够解锁我的车门。
主板: Arduino UNO Rev 3 NFC Shield: v2.0b,Seeed Studio提供
因此,当存在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);
}这段代码可以工作。
发布于 2014-05-04 01:20:21
当检测到任何标签时,您表示当前正在解锁。所以你一定已经在轮询标签了。如果你使用的是原始的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进行访问控制,我通常要注意的是:不要这样做!在许多现有的系统中,这已被证明是一个非常糟糕的想法。有关更多信息,请参阅以下帖子:
发布于 2014-07-09 22:06:44
我也在这样做,并且,使用来自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 'pill‘标签的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") ;
} 发布于 2014-05-03 23:56:39
我相信你可以使用:
readPassiveTargetID(PN532_MIFARE_ISO14443A)来获取身份信息。
https://stackoverflow.com/questions/23446584
复制相似问题