Arduino NFC door unlock



我正在尝试使用带有NFC屏蔽的arduino板来解锁我的车门。

板:Arduino UNO Rev 3NFC Shield: v2.0b by 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);
}

表示当前在检测到任何标记时都在解锁。所以你必须已经轮询标签。如果您正在使用原始的seeedstudio库,您可以使用inListPassiveTarget()方法或(如commesan已经指出的)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 Type 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进行访问控制,我通常的注意事项是:不要这样做!在许多现有系统中,这已被证明是一个非常糟糕的主意。查看这些帖子以获取更多信息:

  • NFC标签上的序列-真正独特?可克隆吗?
  • 如何防止NFC标签克隆?
  • NFC标签作为认证工具

我也这样做,使用NFC库示例中的示例ReadTag,我有UID:

Serial.println("nScan electronic keyn");
if (nfc.tagPresent())
{
    NfcTag tag = nfc.read();
    Serial.println(tag.getTagType());
    String idnumber = tag.getUidString();
    Serial.print("UID: ");Serial.println(idnumber);

例如,这给出了Mifare Classic"药丸"标签中的305c 6f80。稍作分析后,它从库返回为格式化字符串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") ;
    }  

我相信你可以使用:

readPassiveTargetID(PN532_MIFARE_ISO14443A)

获取id。

最新更新