0

I am new to Arduino. I am trying to write an ID to my NFC card. I used the PN532 library and code from the example. I can't go further than authenticateBlock(). Also which block is trying to authenticate? I can't see it in output.

Output:

  • Found 1 tag
  • Sens Response: 0x44
  • Sel Response: 0x0
  • 0x4 0xB9 0xC9 0xBA 0x20 0x4B 0x80
  • Read card #3122678656

Code is:

// This example writes a MIFARE memory block 0x08. It is tested with a new MIFARE 1K cards. Uses default keys.
// Note: Memory block 0 is readonly and contains manufacturer data. Do not write to Sector Trailer block
// unless you know what you are doing. Otherwise, the MIFARE card may be unusable in the future.

//Contributed by Seeed Technology Inc (www.seeedstudio.com)

#include <PN532.h>
#include <SPI.h>

/*Chip select pin can be connected to D10 or D9 which is hareware optional*/
/*if you the version of NFC Shield from SeeedStudio is v2.0.*/
#define PN532_CS 10
PN532 nfc(PN532_CS);

uint8_t written = 0;
#define  NFC_DEMO_DEBUG 1

void setup(void) {
#ifdef NFC_DEMO_DEBUG
  Serial.begin(9600);
  Serial.println("Hello!");
#endif
  nfc.begin();

  uint32_t versiondata = nfc.getFirmwareVersion();
  if (! versiondata) {
#ifdef NFC_DEMO_DEBUG
    Serial.print("Didn't find PN53x board");
#endif
    while (1); // halt
  }
#ifdef NFC_DEMO_DEBUG
  // 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);
  Serial.print("Supports ");
  Serial.println(versiondata & 0xFF, HEX);
#endif
  // configure board to read RFID tags and cards
  nfc.SAMConfig();
}

void loop(void) {
  uint32_t id;
  // look for MiFare type cards
  id = nfc.readPassiveTargetID(PN532_MIFARE_ISO14443A);
  if (id != 0) {
#ifdef NFC_DEMO_DEBUG
    Serial.print("Read card #");
    Serial.println(id);
    Serial.println();
#endif
    uint8_t keys[] = {
      0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF
    };
    uint8_t writeBuffer[16];
    for (uint8_t i = 0; i < 16; i ++) {
      writeBuffer[i] = i; //Fill buffer with 0,1,2....F
    }
    if (nfc.authenticateBlock(1, id , 0x08, KEY_A, keys)) {
      //authenticate block 0x08
      //if authentication successful
      if (written == 0) {
        //Not written
        written = nfc.writeMemoryBlock(1, 0x08, writeBuffer); // Write writeBuffer[] to block 0x08
        if (written)
#ifdef NFC_DEMO_DEBUG
          Serial.println("Write Successful");
#endif
      }
      uint8_t block[16];
      //read memory block 0x08
      if (nfc.readMemoryBlock(1, 0x08, block)) {
#ifdef NFC_DEMO_DEBUG
        Serial.println("Read block 0x08:");
        //if read operation is successful
        for (uint8_t i = 0; i < 16; i++) {
          //print memory block
          Serial.print(block[i], HEX);
          Serial.print(" ");
        }
        Serial.println();
#endif
      }
    }
  }
  delay(500);
}
Michael Roland
  • 39,663
  • 10
  • 99
  • 206
  • What is the library version for the PN532 you are using? I think the problem can be in the authentication command. In the libraries I am looking on the internet I see these parameters in the authenticateBlock function: "uint8_t * uid, uint8_t uidLen, uint32_t blockNumber, uint8_t keyNumber, uint8_t * keyData". In your case the uid of the card is a uint32_t variable, maybe you need to retrieve the uid or reconvert in a uint8_t variable, can you please share the authenticateBlock function definition you are using? – Luis Pascual Mar 22 '18 at 07:53

1 Answers1

0

The combination of the SENS_RES (ATQA) = 0x0044, SEL_RES (SAK) = 0x00, and the UID = 0x04B9C9BA204B80 suggest that your tag is a MIFARE Ultralight tag or an NTAG tag. However, the code that you show in your question is designed for access to MIFARE Classic tags.

MIFARE Ultralight/NTAG tags don't support the authentication mechanism performed by nfc.authenticateBlock() (which is specific to MIFARE Classic). As a result the code will fail on nfc.authenticateBlock(). Since your tag does not support that type of authentication (it might support a different mechanism though), you could try to skip the authentication step and only use read/write methods. These should work (with some limitations, see Byte array gets truncated when writing to memory of RFID tag using Adafruit PN532 library) for your tag type.

Michael Roland
  • 39,663
  • 10
  • 99
  • 206
  • Can we change tag targetID? –  Mar 22 '18 at 16:13
  • yup my card is ntag213 so how i store data any website you can suggest? –  Mar 22 '18 at 16:37
  • @Mr_Rj No, you can't change the anti-collision identifier (the one partially returned by `readPassiveTargetID()`). See [Is there a software that can change NFC Tag's serial number?](https://stackoverflow.com/q/25840366/2425802) – Michael Roland Mar 22 '18 at 19:32
  • @Mr_Rj See the post linked in my answer on how to write data to the tag. You might also want to check e.g. [Writing NDEF data to NTAG216 tag using low-level NFC communication methods](https://stackoverflow.com/q/42105626/2425802) on how to produce NDEF formatted data that could be interpreted by other devices (e.g. Android). – Michael Roland Mar 22 '18 at 19:35
  • i am able to get data from ntag card but i cant understand it is some kind of hax you know how to decode or how can we translate it to plain text form? –  Mar 23 '18 at 05:27
  • i am using arduino uno , pn532 and ntag tag so i don't think i can use software for it –  Mar 23 '18 at 05:28
  • You already have the read method in your code (`nfc.readMemoryBlock()`), I linked to an explanation on how to read data with that method and how to interpret that data. What elese do you expect? – Michael Roland Mar 23 '18 at 07:41