2014-05-03 66 views
1

我想用NFC盾牌的arduino板能够解锁我的车的门。Arduino NFC门解锁

板:Arduino的UNO启3 NFC盾:通过Seeed工作室

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

什么码你尝试过这么远吗? –

回答

0

我相信你可以使用:

readPassiveTargetID(PN532_MIFARE_ISO14443A) 

获得ID。

2

您表明您当前正在解锁检测到任何标签。所以你必须已经轮询标签。如果您正在使用原始的seeedstudio库,则可以使用inListPassiveTarget()方法或(如已注明的已知)readPassiveTargetID()方法对任何被动目标执行轮询。

虽然inListPassiveTarget()只是返回一个布尔值指示是否有任何目标目前,readPassiveTargetID()方法提供了一组配置参数并且还允许您检索防冲突标识符(例如UID的ISO 14443类型A):

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型,Y您可以同时使用下面的定义,而不是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访问控制通常注意:不要这么做!在许多现有系统中,这被证明是一个非常糟糕的主意。查看这些信息了解更多信息:

+0

感谢您的信息和警告。我了解NFC标签可以被克隆,但是没有人会知道我的车可以这样解锁。它是1992年的一辆车;) 明天我会试试这个代码。 – user3599457

0

我也这样做,并使用例如ReadTag从NFC库的例子,我有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); 

这给出,例如,30 5F 6F 80来自Mifare Classic'药丸'标签。经过稍加分析,这个来自图书馆作为一个格式化字符串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") ; 
    }