nfc read / write problem
maybe something to do with the auth? there seems to be some problems instantiating the original data block file toounknown
plain_text
2 years ago
6.8 kB
21
Indexable
// NFC INITIATION
#include <Wire.h>
#include <PN532_I2C.h>
#include <PN532.h>
#include <NfcAdapter.h>
PN532_I2C pn532i2c(Wire);
PN532 nfc(pn532i2c);
// OLED INITIATION
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>
#define SCREEN_WIDTH 128
#define SCREEN_HEIGHT 64
#define OLED_RESET -1
#define SCREEN_ADDRESS 0x3C
Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, OLED_RESET);
// printing words
#define result String // string that will be output in the Serial window
void setup(void) {
Serial.begin(115200);
Serial.println("Hello!");
nfc.begin();
// SSD1306_SWITCHCAPVCC = generate display voltage from 3.3V internally
if (!display.begin(SSD1306_SWITCHCAPVCC, SCREEN_ADDRESS)) {
Serial.println(F("SSD1306 allocation failed"));
for (;;); // Don't proceed, loop forever
}
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);
display.clearDisplay();
display.setCursor(0, 0); //oled display
display.setTextSize(1);
display.setTextColor(WHITE);
display.print("Found chip PN5");
display.print((versiondata >> 24) & 0xFF, HEX);
display.setCursor(0, 20); //oled display
display.setTextSize(1);
display.setTextColor(WHITE);
display.print("Firmware ver. ");
display.print((versiondata >> 16) & 0xFF, DEC);
display.print(".");
display.print((versiondata >> 8) & 0xFF, DEC);
// Set the max number of retry attempts to read from a card
// This prevents us from waiting forever for a card, which is
// the default behaviour of the PN532.
nfc.setPassiveActivationRetries(0xFF);
// configure board to read RFID tags
nfc.SAMConfig();
Serial.println("Waiting for an ISO14443A card");
display.setCursor(0, 40); //oled display
display.setTextSize(1);
display.setTextColor(WHITE);
display.print("Waiting for NFC Card");
display.display();
}
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("");
display.clearDisplay();
display.setCursor(10, 0);
display.setTextSize(1);
display.setTextColor(WHITE);
display.print("UID Length:");
display.print(uidLength, DEC);
display.print(" bytes");
display.setCursor(35, 20);
display.setTextSize(1);
display.setTextColor(WHITE);
display.print("Text on Card: ");
display.setCursor(5, 35);
if (uidLength == 4)
{
// We probably have a Mifare Classic card ...
Serial.println("Seems to be a Mifare Classic card (4 byte UID)");
// Now we need to try to authenticate it for read/write access
// Try with the factory default KeyA: 0xFF 0xFF 0xFF 0xFF 0xFF 0xFF
Serial.println("Trying to authenticate block 4 with default KEYA value");
uint8_t keya[6] = { 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF };
// Start with block 4 (the first block of sector 1) since sector 0
// contains the manufacturer data and it's probably better just
// to leave it alone unless you know what you're doing
success = nfc.mifareclassic_AuthenticateBlock(uid, uidLength, 4, 0, keya);
if (success)
{
// Serial.println("Sector 1 (Blocks 4..7) has been authenticated");
uint8_t data[16] = { 'g', 'r', 'a', 'p', 'e', 'f', 'r', 'u', 'i', 't', 0, 0, 0, 0, 0, 0};
// If you want to write something to block 4 to test with, uncomment
// the following line and this text should be read back in a minute
// success = nfc.mifareclassic_WriteDataBlock (4, data);
// Try to read the contents of block 4
success = nfc.mifareclassic_ReadDataBlock(4, data);
if (success)
{
// Data seems to have been read ... spit it out
Serial.println("Reading Block 4:");
nfc.PrintHexChar(data, 16);
Serial.println("");
// Wait a bit before reading the card again
delay(1000);
}
else
{
Serial.println("Ooops ... unable to read the requested block. Try another key?");
}
}
else
{
Serial.println("Ooops ... authentication failed: Try another key?");
}
}
if (uidLength == 7)
{
// We probably have a Mifare Ultralight card ...
Serial.println("Seems to be a Mifare Ultralight tag (7 byte UID)");
uint8_t data[32] = { 'g', 'r', 'a', 'p', 'e', 'f', 'r', 'u', 'i', 't', 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0};
// Try to read the first general-purpose user page (#4)
Serial.println("Reading page 4");
success = nfc.mifareultralight_ReadPage (4, data);
if (success)
{
// Data seems to have been read ... spit it out
nfc.PrintHexChar(data, 4);
Serial.println("");
// Wait a bit before reading the card again
delay(1000);
}
for (uint8_t i = 7; i < 42; i++) // starting serial output at Page 7 and stop reading at Page 42
{
success = nfc.PrintHexChar(data, i);
// Display the results, depending on 'success'
if (success)
{
// Dump the page data
printHexCharAsOneLine(data, 4);
}
else
{
Serial.println("Ooops ... unable to read the requested page!?");
}
Serial.println ();
// Serial.println(nfc.PrintHexChar(data, 4));
}
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();
}
}
}Editor is loading...
Leave a Comment