Amateur at Arduino programming, even moreso for the PN532 library. I've spent days and days combing through forums, reading tutorials, looking through the PN532 Library, and asking friends for help with this question, and I'm stumped...
Hardware: PN532 Shield on an Arduino Uno R3
I've had no issue writing strings to my nfc tags, but when it comes to getting that same string spat back out in the Serial Monitor as one string I'm stumped. The closest I've gotten is to get the pages printed on one line with any empty spaces not printed, but if the string on the NFC tag is longer than 4 characters it still ends up coming into the serial as multiple chunks, which is messing me up down the line when I'm having another program (Node-Red) to look for the entire string.
Here is my code, it's a modified version of the ntag2xx_reader example sketch:
Code: Select all
/**************************************************************************/
#include <Wire.h>
#include <SPI.h>
#include <Adafruit_PN532.h>
// If using the breakout or shield with I2C, define just the pins connected
// to the IRQ and reset lines. Use the values below (2, 3) for the shield!
#define PN532_IRQ (2)
#define PN532_RESET (3) // Not connected by default on the NFC Shield
// Use this line for a breakout or shield with an I2C connection:
Adafruit_PN532 nfc(PN532_IRQ, PN532_RESET);
//** snippet of code below added to force the serial to print text from the NFC tag
// on one line. Modification of the PrintHexChar function **
#define PN532DEBUGPRINT Serial
#define result String // string that will be output in the Serial window
String result = "beep boop the word is: ";
void printHexCharAsOneLine(const byte *data, const uint32_t numBytes) {
uint32_t szPos;
for (szPos = 0; szPos < numBytes; szPos++) {
if (data[szPos] <= 0x1F)
PN532DEBUGPRINT.print(F(""));
else if (data[szPos] == 0XFE)
PN532DEBUGPRINT.print(F(""));
else
PN532DEBUGPRINT.print((char)data[szPos]);
result = result + String((char)data[szPos]);
}
}
void setup(void) {
Serial.begin(115200);
while (!Serial) delay(10); // for Leonardo/Micro/Zero
Serial.println("Hello!");
nfc.begin();
uint32_t versiondata = nfc.getFirmwareVersion();
if (! versiondata) {
Serial.print("Didn't find PN53x board");
while (1); // halt
}
// Got ok data, print it out!
Serial.print("Found chip PN5"); Serial.println((versiondata>>24) & 0xFF, HEX);
Serial.print("Firmware ver. "); Serial.print((versiondata>>16) & 0xFF, DEC);
Serial.print('.'); Serial.println((versiondata>>8) & 0xFF, DEC);
// configure board to read RFID tags
nfc.SAMConfig();
Serial.println("Waiting for an ISO14443A Card ...");
}
void loop(void) {
uint8_t success;
uint8_t uid[] = { 0, 0, 0, 0, 0, 0, 0 }; // Buffer to store the returned UID
uint8_t uidLength; // Length of the UID (4 or 7 bytes depending on ISO14443A card type)
// Wait for an NTAG203 card. When one is found 'uid' will be populated with
// the UID, and uidLength will indicate the size of the UUID (normally 7)
success = nfc.readPassiveTargetID(PN532_MIFARE_ISO14443A, uid, &uidLength);
if (success) {
// Display some basic information about the card
Serial.println("Found an ISO14443A card");
Serial.print(" UID Length: ");Serial.print(uidLength, DEC);Serial.println(" bytes");
Serial.print(" UID Value: ");
nfc.PrintHex(uid, uidLength);
Serial.println("");
if (uidLength == 7)
{
uint8_t data[32];
// We probably have an NTAG2xx card (though it could be Ultralight as well)
Serial.println("Seems to be an NTAG2xx tag (7 byte UID)");
for (uint8_t i = 7; i < 42; i++) // starting serial output at Page 7 and stop reading at Page 42
{
success = nfc.ntag2xx_ReadPage(i, data);
// Display the results, depending on 'success'
if (success)
{
// Dump the page data
printHexCharAsOneLine(data,4);
}
else
{
Serial.println("Unable to read the requested page!");
}
}
Serial.println ();
Serial.println(result);
}
else
{
Serial.println("This doesn't seem to be an NTAG203 tag (UUID length != 7 bytes)!");
}
// Wait a bit before trying again
Serial.println("\n\nSend a character to scan another tag!");
Serial.flush();
while (!Serial.available());
while (Serial.available()) {
Serial.read();
}
Serial.flush();
}
}
Here's what my Serial Monitor currently looks like, with the word "volcano" having been written on my NFC tag: