2014-05-03 114 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") ; 
    }