I am new to NFC and was trying out a few different things with the shield. I was able to use the examples properly, but I had some NTAG216 that I wanted to read and write to.
Unfortunately, whenever I run this, I get a reading from the tag. The problem is that my tag is not near the antenna. Is there something I am doing wrong, maybe an initialization that I cannot see?
Code: Select all
#include <Wire.h>
#include <Adafruit_PN532.h>
//Shield with I2C specific defines
#define PN532_IRQ (2)
#define PN532_RESET (3) //not connected on shield by default
Adafruit_PN532 nfc(PN532_IRQ, PN532_RESET);
//Name and description for all used variables
String action = ""; //Used to know what the program should do
String saveData = ""; //Used to capture user input to save to Ntag
//byte buf[31] = {}; //Used to convert string to character array
void setup() {
// put your setup code here, to run once:
//Serial added only to be able to see on computer
Serial.begin(115200); //Arbitraty baud rate can be changed
Serial.println("official testing started.");
//Initializing the NFC shield for us to use
nfc.begin();
//Extra Init might be needed for proper functionality
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);
//Configuration to read RFID tags
nfc.SAMConfig();
Serial.println("NFC ready to go waiting your response.");
}
void loop() {
// put your main code here, to run repeatedly:
Serial.println("What do you wish to do? 1. Read a tag, 2. Rewrite a tag, 3. Exit");
//Wait for user input
while ( Serial.available() == 0 )
{}
action = Serial.readString();
//Read a tag (Might need to put a delimiter for reading multiple pages, let us see what we can do
if ( action == "1" ){
Serial.println("Ready to read tag.");
uint8_t data[16]; //Can this 16 be modified to read the proper amount?
nfc.ntag2xx_ReadPage (4, data); //4 is just an arbitrarty page number we can change this in the future
//Show of confirmation
nfc.PrintHexChar(data,4);
Serial.println("");
delay (1000);
}
}