RFID1356MIFARE with ESP32 over UART = possible ?

GeorgeFlorian1
Posts: 160
Joined: Thu Jan 31, 2019 2:32 pm

RFID1356MIFARE with ESP32 over UART = possible ?

Postby GeorgeFlorian1 » Mon Feb 08, 2021 1:41 pm

Hello.

I am interested in integrating the RFID1356MIFARE with the ESP32-EVB an using it as a card reader.

The first step was to connect the RFID to an USB hub using a mini-USB cable and selecting UART mode (both LEDs blinking at the same time).

The second step was to disconnect it from USB and connect it to the ESP32 using the included UEXT ribbon.

Next I uploaded the following code to the ESP:

Code: Select all

PN532_HSU pn532hsu(Serial1);
PN532 nfc(pn532hsu);

void setup() {
  enableCore1WDT();
  delay(100);
  Serial.begin(115200);

  nfc.begin();
  delay(100);

    uint32_t versiondata = nfc.getFirmwareVersion();
  if (! versiondata) {
    Serial.print("Didn't find PN53x board");
    // while (1) delay(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()
{
  delay(1);

  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 ISO14443A type cards (Mifare, etc.).  When one is found
  // 'uid' will be populated with the UID, and uidLength will indicate
  // if the uid is 4 bytes (Mifare Classic) or 7 bytes (Mifare Ultralight)
  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 == 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];
		
        // 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
        // data = { 'a', 'd', 'a', 'f', 'r', 'u', 'i', 't', '.', 'c', 'o', 'm', 0, 0, 0, 0};
        // 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)");
	  
      // Try to read the first general-purpose user page (#4)
      Serial.println("Reading page 4");
      uint8_t data[32];
      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);
      }
      else
      {
        Serial.println("Ooops ... unable to read the requested page!?");
      }
    }
  }
}

The above code uses https://github.com/elechouse/PN532 library.

Inside it I've modified:

Code: Select all

void PN532_HSU::begin()
{
    _serial->begin(38400, SERIAL_8N1, 36, 4);
}
If I want to get a read I connect the 3.3V_UEXT_E jumper.

The code above returns only "Didn't find PN53x board".

If I change the code to a simple Serial read:

Code: Select all

if(Serial1.available()) {
  Serial.print(Serial1.read());
  Serial.print(" ");
}
I get some random characters each time I connect the jumper, even when I don't have a card in front of the reader:

Code: Select all

0 0 91 61 25 160 128 0 0 0 0 0 0 0 0 0 0 118 118 54 16 18 32 1 1 1 1 1 0 0 248 126 118 55 50 162 129 32 0 1 1 1 0 0 0 119 247 38 33 17 1 9 128 0 1 0 0 0 55 103 39 1 17 9 32 0 0 0 0 255 255 119 55 49 34 34 128 128 1 1 1 0 0 126 119 103 51 17 25 25 128 1 1 0 0 0 118 54 38 38 32 1 1 1 1 1 1 0 0 0 55 50 54 50 2 128 128 1 1 1 1 0 0 126 54 50 162 130 129 32 1 0 1 0 0 0 255 119 118 55 54 25 25 128 1 1 1 0 127 255 126 118 102 39 35 1 9 1 0 0 1 0 0 126 254 54 54 51 38 1 17 17 9 0 0 0 118 118 16 35 33 1 9 32 0 0 1 0 55 54 54 16 34 1 1 1 1 1 1 0 118 55 54 38 33 1 1 9 0 0 0 0 0 254 118 118 18 34 34 1 1 1 1 0 0 0 55 54 50 34 130 1 1 1 9 1 0 0 126 55 54 35 1 25 9 0 1 0 0 0 0 0 0 0 136 119 54 50 36 32 1 1 1 1 0 0 0 0 119 54 55 51 25 25 32 1 1 1 1 0 0 119 103 51 51 17 25 32 0 1 1 0 0 0 0 255 54 54 54 39 17 17 25 32 0 1 0 1 0 0 0 127 183 55 16 38 33 1 1 1 0 1 0 0 0 0 254 118 103 55 51 54 16 32 33 1 1 1 1 0 248 55 55 118 52 103 35 1 17 9 1 0 255 118 63 25 54 9 0 1 0 0 1 0 55 118 55 35 17 25 32 0 1 0 9 1 1 0 0 0 0 127 118 54 51 49 17 9 0 1 1 1 0 0 252 118 118 55 55 35 33 17 9 0 0 0 119 183 54 39 33 25 25 0 0 1 0 118 119 54 54 35 49 1 9 0 0 0 0 0 0 55 54 54 33 17 9 9 128 0 0 0 118 39 33 25 25 0 128 0 0 0 0 255 55 54 18 36 33 1 1 1 0 0 0 0 0 0 119 55 54 33 17 17 9 128 0 0 0 55 54 50 36 37 1 17 1 1 1 1 0 0 0 0 0 126 55 50 102 36 33 1 1 1 1 0 0 247 118 54 54 16 32 32 1 1 1 0 0 55 116 18 38 33 1 1 1 1 0 0 126 114 102 38 33 1 1 1 0 0 0 0 127 118 114 39 33 1 17 9 0 1 0 0 1 0 118 114 39 35 1 1 9 0 0 0 1 0 1 0 55 16 38 32 1 1 1 1 0 0 114 50 33 33 1 9 9 0 0 0 0 118 50 39 54 33 1 17 9 
And when I put a card in front of the reader I get a normalized sequence. But I don't know where it starts or where it ends.

Is there anybody who made something similar ?

Thank you.

Who is online

Users browsing this forum: No registered users and 59 guests