风筝
发表于: 2018-8-15 22:40:31 | 显示全部楼层

本篇文章主要介绍如何使用Arduino开发板读取NFC标签并在其上写入信息!


什么是NFC?

近场通信(Near Field Communication )是电子设备用于在彼此之间通信和传输数据的协议。近场通信设备必须彼此非常近,通常在10cm之间,但是该范围可以根据正在发送的设备和标签的大小而变化。 NFC标签无需任何电源输入。它们在两个小环形天线之间使用磁感应。现今的标签带有96到4096字节的信息。


使用的设备清单

●    Arduino Uno R3开发板

●    Adafruit PN532 RFID / NFC Shield

●    Arduino IDE(集成开发环境)

●    可重写的NFC标签

untitled.png

NFC标签是可重写的,这是非常重要的一点,否则这将不起作用。


为了测试我们在标签上写的内容是否成功,我们可以使用Arduino或支持NFC的手机进行测试。大多数运行Android的智能手机应该能够读取NFC标签,我将使用Nexus 5进行测试。对于iPhone用户来说,支持NFC的iPhone是iPhone 6及以上机型,但它们不支持NFC标签读取,所以只需使用Arduino来测试你的标签上写的是什么。 iPhone仅使用其NFC功能进行苹果支付,因此您无法使用它们来读取标签或其他任何内容。


我们将所有组件准备完成后,就需要在Arduino IDE中安装两个库,这些库可以使得我们可以读写标签。库是来自don / NDEFSeeedstudio,我们将主要使用的是don的库,因为如果你有Seeedstudio NFC shield,就会使用Seeedstudio的库。我们将它安装为库以防万一。您必须使用Arduino IDE的Sketch>>Include Library下的“Add .zip Library”下载并安装这两个库。确保单独安装这两个库并在默认的Arduino目录下,否则您将遇到编译错误。


启动Arduino IDE,这时会出现一个新的Sketch文件。将新文件起个名字并保存,例如“Read NFC Tag”。我们首先要包含头文件,以下是包含头文件的列表。他们位于在void setup()函数之前。

  1. #include <Wire.h>
  2. #include <PN532_I2C.h>
  3. #include <PN532.h>   // The following files are included in the libraries Installed
  4. #include <NfcAdapter.h>

  5. PN532_I2C pn532_i2c(Wire);
  6. NfcAdapter nfc = NfcAdapter(pn532_i2c);  // Indicates the Shield you are using
复制代码

读取NFC标签

这些头文件非常重要,如果没有它们,项目将无法工作。之后编写以下代码。

  1. void setup(void) {
  2.   Serial.begin(9600);
  3.   Serial.println("NFC TAG READER"); // Header used when using the serial monitor
  4.   nfc.begin();
  5. }

  6. void loop(void) {
  7.   Serial.println("\nScan your NFC tag on the NFC Shield\n");  // Command so that you an others will know what to do

  8.   if (nfc.tagPresent())
  9.   {
  10.     NfcTag tag = nfc.read();
  11.     Serial.println(tag.getTagType());
  12.     Serial.print("UID: ");Serial.println(tag.getUidString()); // Retrieves the Unique Identification from your tag

  13.     if (tag.hasNdefMessage()) // If your tag has a message
  14.     {

  15.       NdefMessage message = tag.getNdefMessage();
  16.       Serial.print("\nThis Message in this Tag is ");
  17.       Serial.print(message.getRecordCount());
  18.       Serial.print(" NFC Tag Record");
  19.       if (message.getRecordCount() != 1) {
  20.         Serial.print("s");
  21.       }
  22.       Serial.println(".");

  23.       // If you have more than 1 Message then it wil cycle through them
  24.       int recordCount = message.getRecordCount();
  25.       for (int i = 0; i < recordCount; i++)
  26.       {
  27.         Serial.print("\nNDEF Record ");Serial.println(i+1);
  28.         NdefRecord record = message.getRecord(i);

  29.         int payloadLength = record.getPayloadLength();
  30.         byte payload[payloadLength];
  31.         record.getPayload(payload);


  32.         String payloadAsString = ""; // Processes the message as a string vs as a HEX value
  33.         for (int c = 0; c < payloadLength; c++) {
  34.           payloadAsString += (char)payload[c];
  35.         }
  36.         Serial.print("  Information (as String): ");
  37.         Serial.println(payloadAsString);


  38.         String uid = record.getId();
  39.         if (uid != "") {
  40.           Serial.print("  ID: ");Serial.println(uid); // Prints the Unique Identification of the NFC Tag
  41.         }
  42.       }
  43.     }
  44.   }
  45.   delay(10000);
  46. }
复制代码

一旦您将此代码保存并上传到带有扩展板(Shield)的Arduino中,您就可以开始测试您的标签有哪些消息。当您将程序上传到Arduino时,打开串口监视器,您应该会看到一条消息“NFC TAG Reader”,并且在其下面显示“Scan your NFC tag on your NFC Shield.”的说明。在串口显示器上显示信息如下:

NFC_Serial.png

请注意,它给出了NFC标签的唯一标识,并显示了标签上有哪些信息。在这个标签上,有一个简单的欢迎信息和Arduino Twitter的官网链接。 Arduino成功读取了标签上的信息。


向NFC标签上写入信息

现在,为了能够在标签上写消息,除了我们要稍微改变一下代码之外,这个过程是类似的。 void setup()之前的头文件保持不变,以下是编写的代码。

  1. void setup() {
  2.     Serial.begin(9600);
  3.     Serial.println("NFC Tag Writer"); // Serial Monitor Message
  4.     nfc.begin();
  5. }

  6. void loop() {
  7.     Serial.println("\nPlace an NFC Tag that you want to Record these Messages on!"); // Command for the Serial Monitor
  8.     if (nfc.tagPresent()) {
  9.         NdefMessage message = NdefMessage();
  10.         message.addTextRecord("My First NFC Tag Write"); // Text Message you want to Record
  11.         message.addUriRecord("http://allaboutcircuits.com"); // Website you want to Record
  12.         message.addTextRecord("Way to Go, It Worked!");  // Ednding Message for you to Record
  13.         boolean success = nfc.write(message);
  14.         if (success) {
  15.             Serial.println("Good Job, now read it with your phone!"); // if it works you will see this message
  16.         } else {
  17.             Serial.println("Write failed"); // If the the rewrite failed you will see this message
  18.         }
  19.     }
  20.     delay(10000);
  21. }
复制代码

此代码在标签上保存三条消息:“My First NFC Tag Write”介绍文本,然后是一个指向AllAboutCircuits的链接,最后是一条结束消息,说“Way to Go, It Worked!

Screenshot_20151023-012201.png


现在,当我在手机上扫描NFC标签时,会得到这两条信息和链接。您可以更改代码,在信息上列出您想要的任何内容,以及引导您转到另一个链接。 进行任何更改时,请确保使用Arduino或智能手机检查NFC标签信息。

跳转到指定楼层
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

主题 700 | 回复: 1479



手机版|

GMT+8, 2024-3-29 04:47 , Processed in 0.090857 second(s), 6 queries , Gzip On, MemCache On. Powered by Discuz! X3.5

YiBoard一板网 © 2015-2022 地址:河北省石家庄市长安区高营大街 ( 冀ICP备18020117号 )

快速回复 返回顶部 返回列表