|
本篇文章主要介绍如何使用Arduino开发板读取NFC标签并在其上写入信息!
什么是NFC? 近场通信(Near Field Communication )是电子设备用于在彼此之间通信和传输数据的协议。近场通信设备必须彼此非常近,通常在10cm之间,但是该范围可以根据正在发送的设备和标签的大小而变化。 NFC标签无需任何电源输入。它们在两个小环形天线之间使用磁感应。现今的标签带有96到4096字节的信息。
使用的设备清单 ● Arduino Uno R3开发板 ● Adafruit PN532 RFID / NFC Shield
● Arduino IDE(集成开发环境) ● 可重写的NFC标签
NFC标签是可重写的,这是非常重要的一点,否则这将不起作用。
为了测试我们在标签上写的内容是否成功,我们可以使用Arduino或支持NFC的手机进行测试。大多数运行Android的智能手机应该能够读取NFC标签,我将使用Nexus 5进行测试。对于iPhone用户来说,支持NFC的iPhone是iPhone 6及以上机型,但它们不支持NFC标签读取,所以只需使用Arduino来测试你的标签上写的是什么。 iPhone仅使用其NFC功能进行苹果支付,因此您无法使用它们来读取标签或其他任何内容。
我们将所有组件准备完成后,就需要在Arduino IDE中安装两个库,这些库可以使得我们可以读写标签。库是来自don / NDEF和Seeedstudio,我们将主要使用的是don的库,因为如果你有Seeedstudio NFC shield,就会使用Seeedstudio的库。我们将它安装为库以防万一。您必须使用Arduino IDE的Sketch>>Include Library下的“Add .zip Library”下载并安装这两个库。确保单独安装这两个库并在默认的Arduino目录下,否则您将遇到编译错误。
启动Arduino IDE,这时会出现一个新的Sketch文件。将新文件起个名字并保存,例如“Read NFC Tag”。我们首先要包含头文件,以下是包含头文件的列表。他们位于在void setup()函数之前。 - #include <Wire.h>
- #include <PN532_I2C.h>
- #include <PN532.h> // The following files are included in the libraries Installed
- #include <NfcAdapter.h>
- PN532_I2C pn532_i2c(Wire);
- NfcAdapter nfc = NfcAdapter(pn532_i2c); // Indicates the Shield you are using
复制代码
读取NFC标签 这些头文件非常重要,如果没有它们,项目将无法工作。之后编写以下代码。 - void setup(void) {
- Serial.begin(9600);
- Serial.println("NFC TAG READER"); // Header used when using the serial monitor
- nfc.begin();
- }
- void loop(void) {
- Serial.println("\nScan your NFC tag on the NFC Shield\n"); // Command so that you an others will know what to do
- if (nfc.tagPresent())
- {
- NfcTag tag = nfc.read();
- Serial.println(tag.getTagType());
- Serial.print("UID: ");Serial.println(tag.getUidString()); // Retrieves the Unique Identification from your tag
- if (tag.hasNdefMessage()) // If your tag has a message
- {
- NdefMessage message = tag.getNdefMessage();
- Serial.print("\nThis Message in this Tag is ");
- Serial.print(message.getRecordCount());
- Serial.print(" NFC Tag Record");
- if (message.getRecordCount() != 1) {
- Serial.print("s");
- }
- Serial.println(".");
- // If you have more than 1 Message then it wil cycle through them
- int recordCount = message.getRecordCount();
- for (int i = 0; i < recordCount; i++)
- {
- Serial.print("\nNDEF Record ");Serial.println(i+1);
- NdefRecord record = message.getRecord(i);
- int payloadLength = record.getPayloadLength();
- byte payload[payloadLength];
- record.getPayload(payload);
- String payloadAsString = ""; // Processes the message as a string vs as a HEX value
- for (int c = 0; c < payloadLength; c++) {
- payloadAsString += (char)payload[c];
- }
- Serial.print(" Information (as String): ");
- Serial.println(payloadAsString);
- String uid = record.getId();
- if (uid != "") {
- Serial.print(" ID: ");Serial.println(uid); // Prints the Unique Identification of the NFC Tag
- }
- }
- }
- }
- delay(10000);
- }
复制代码
一旦您将此代码保存并上传到带有扩展板(Shield)的Arduino中,您就可以开始测试您的标签有哪些消息。当您将程序上传到Arduino时,打开串口监视器,您应该会看到一条消息“NFC TAG Reader”,并且在其下面显示“Scan your NFC tag on your NFC Shield.”的说明。在串口显示器上显示信息如下:
请注意,它给出了NFC标签的唯一标识,并显示了标签上有哪些信息。在这个标签上,有一个简单的欢迎信息和Arduino Twitter的官网链接。 Arduino成功读取了标签上的信息。
向NFC标签上写入信息 现在,为了能够在标签上写消息,除了我们要稍微改变一下代码之外,这个过程是类似的。 void setup()之前的头文件保持不变,以下是编写的代码。 - void setup() {
- Serial.begin(9600);
- Serial.println("NFC Tag Writer"); // Serial Monitor Message
- nfc.begin();
- }
- void loop() {
- Serial.println("\nPlace an NFC Tag that you want to Record these Messages on!"); // Command for the Serial Monitor
- if (nfc.tagPresent()) {
- NdefMessage message = NdefMessage();
- message.addTextRecord("My First NFC Tag Write"); // Text Message you want to Record
- message.addUriRecord("http://allaboutcircuits.com"); // Website you want to Record
- message.addTextRecord("Way to Go, It Worked!"); // Ednding Message for you to Record
- boolean success = nfc.write(message);
- if (success) {
- Serial.println("Good Job, now read it with your phone!"); // if it works you will see this message
- } else {
- Serial.println("Write failed"); // If the the rewrite failed you will see this message
- }
- }
- delay(10000);
- }
复制代码
此代码在标签上保存三条消息:“My First NFC Tag Write”介绍文本,然后是一个指向AllAboutCircuits的链接,最后是一条结束消息,说“Way to Go, It Worked!”
现在,当我在手机上扫描NFC标签时,会得到这两条信息和链接。您可以更改代码,在信息上列出您想要的任何内容,以及引导您转到另一个链接。 进行任何更改时,请确保使用Arduino或智能手机检查NFC标签信息。
|