gpt4 book ai didi

Arduino NFC门解锁

转载 作者:行者123 更新时间:2023-12-01 16:49:13 31 4
gpt4 key购买 nike

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

开发板:Arduino UNO Rev 3NFC 扩展板:Seeed Studio v2.0b

因此,当 NFC 标签存在时,信号会将伺服器旋转 180 度以解锁门。目前的问题是我只希望一个 NFC 标签能够解锁/锁定门,而不是任意一个。

目前任何 NFC 标签都可以转动 Helm 机。

有谁知道调用哪个函数只返回 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() 仅返回一个 bool 值,指示是否存在任何目标,但 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 进行访问控制的通常注意事项:不要这样做!在许多现有系统中,这已被证明是一个非常糟糕的主意。请参阅这些帖子以获取更多信息:

关于Arduino NFC门解锁,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23446584/

31 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com