【问题标题】:Arduino NFC door unlockArduino NFC 门解锁
【发布时间】:2014-06-20 05:26:26
【问题描述】:

我正在尝试使用带有 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);
}

此代码有效。

【问题讨论】:

    标签: arduino nfc uniqueidentifier serial-number unlock


    【解决方案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 '药丸'标签的 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】:

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

      inListPassiveTarget() 只返回一个布尔值,指示是否存在 any 目标,readPassiveTargetID() 方法为您提供一组配置参数,还允许您检索防冲突标识符(例如 ISO 14443 Type 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 标签,但是没有人知道我的车可以通过这种方式解锁。它是 1992 年的汽车;)我明天会试试代码。
      【解决方案3】:

      我相信你可以使用:

      readPassiveTargetID(PN532_MIFARE_ISO14443A)
      

      获取ID。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多