Advertisement
muezza29

NFC PN532 Payload Data

Jun 28th, 2019
101
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.03 KB | None | 0 0
  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.  
  6. PN532_I2C pn532_i2c(Wire);
  7. NfcAdapter nfc = NfcAdapter(pn532_i2c);  // Indicates the Shield you are using
  8.  
  9. void setup(void) {
  10.   Serial.begin(9600);
  11.   Serial.println("NFC TAG READER"); // Header used when using the serial monitor
  12.   nfc.begin();
  13. }
  14.  
  15. void loop(void) {
  16.   Serial.println("\nTempelkan NFC Tag\n");  // Command so that you an others will know what to do
  17.  
  18.   if (nfc.tagPresent())
  19.   {
  20.     NfcTag tag = nfc.read();
  21.     Serial.println(tag.getTagType());
  22.     Serial.print("UID: "); Serial.println(tag.getUidString()); // Retrieves the Unique Identification from your tag
  23.  
  24.     if (tag.hasNdefMessage()) // If your tag has a message
  25.     {
  26.  
  27.       NdefMessage message = tag.getNdefMessage();
  28.       Serial.print("\nPesan dari NFC tag ini adalah ");
  29.       Serial.print(message.getRecordCount());
  30.       Serial.print(" NFC Tag Record");
  31.       if (message.getRecordCount() != 1) {
  32.         Serial.print("s");
  33.       }
  34.       Serial.println(".");
  35.  
  36.       // If you have more than 1 Message then it wil cycle through them
  37.       int recordCount = message.getRecordCount();
  38.       for (int i = 0; i < recordCount; i++)
  39.       {
  40.         Serial.print("\nNDEF Record "); Serial.println(i + 1);
  41.         NdefRecord record = message.getRecord(i);
  42.  
  43.         int payloadLength = record.getPayloadLength();
  44.         byte payload[payloadLength];
  45.         record.getPayload(payload);
  46.  
  47.  
  48.         String payloadAsString = ""; // Processes the message as a string vs as a HEX value
  49.         for (int c = 3; c < payloadLength; c++) {
  50.           payloadAsString += (char)payload[c];
  51.         }
  52.         //Serial.print("  Information (as String): ");
  53.         Serial.println(payloadAsString);
  54.  
  55.  
  56.         String uid = record.getId();
  57.         if (uid != "") {
  58.           Serial.print("  ID: "); Serial.println(uid); // Prints the Unique Identification of the NFC Tag
  59.         }
  60.       }
  61.     }
  62.   }
  63.   delay(1000);
  64. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement