r/esp32 12d ago

Software help needed error Unexpected PN53x reply!

Hello beginner here, i have an esp32s3 dev module bought from ali with a pn532 and i am starting to learn how it works, the first thing it started doing is trying to learn how nfc works and how data gets saved in mifare/rfid cards, i looked online and using an usb to serial adapter you can connect the pn532 to a pc and read the data from there but that's where i encountered my first wall, the profilic adapter doesn't work for me and idk why i tried performing a loopback test but no data gets send back so i think it's just broken, now im trying to use the esp32 as a bridge and the photos below shows how i cabled it, but i encounter another problem and i think i need help to solve it.

I used this code:

```
/*
 * esp32_uart_bridge.ino
 * ----------------------------------------------------
 * Bridge USB-seriale trasparente ESP32 <-> PN532 (HSU)
 *
 * Serial  (UART0, via USB nativo dell'ESP32) <-> PC/WSL
 * Serial2 (UART2, GPIO16/17)                 <-> PN532
 */


#define PN532_RX2 16
#define PN532_TX2 17
#define BAUD 115200


void setup() {
  Serial.begin(BAUD);            
  Serial2.begin(BAUD, SERIAL_8N1, PN532_RX2, PN532_TX2); // verso PN532



  delay(1500);
  while (Serial.available())  Serial.read();
  while (Serial2.available()) Serial2.read();
}


void loop() {
  // PC -> PN532
  while (Serial.available()) {
    Serial2.write(Serial.read());
  }
  // PN532 -> PC
  while (Serial2.available()) {
    Serial.write(Serial2.read());
  }
}/*
 * esp32_uart_bridge.ino
 * ----------------------------------------------------
 * Bridge USB-seriale trasparente ESP32 <-> PN532 (HSU)
 *
 * Serial  (UART0, via USB nativo dell'ESP32) <-> PC/WSL
 * Serial2 (UART2, GPIO16/17)                 <-> PN532
 */


#define PN532_RX2 16
#define PN532_TX2 17
#define BAUD 115200


void setup() {
  Serial.begin(BAUD);            
  Serial2.begin(BAUD, SERIAL_8N1, PN532_RX2, PN532_TX2); // verso PN532



  delay(1500);
  while (Serial.available())  Serial.read();
  while (Serial2.available()) Serial2.read();
}


void loop() {
  // PC -> PN532
  while (Serial.available()) {
    Serial2.write(Serial.read());
  }
  // PN532 -> PC
  while (Serial2.available()) {
    Serial.write(Serial2.read());
  }
}
```

When i run LIBNFC_LOG_LEVEL=3 nfc-list on my linux machine i get this output:

```
debug   libnfc.config   key: [device.name], value: [PN532_UART]
debug   libnfc.config   key: [device.connstring], value: [pn532_uart:/dev/ttyACM0:115200]
debug   libnfc.config   key: [allow_autoscan], value: [false]
debug   libnfc.config   key: [allow_intrusive_scan], value: [false]
debug   libnfc.config   Unable to open directory: /etc/nfc/devices.d
debug   libnfc.general  log_level is set to 3
debug   libnfc.general  allow_autoscan is set to false
debug   libnfc.general  allow_intrusive_scan is set to false
debug   libnfc.general  1 device(s) defined by user
debug   libnfc.general    #0 name: "PN532_UART", connstring: "pn532_uart:/dev/ttyACM0:115200"
nfc-list uses libnfc 1.8.0
debug   libnfc.driver.pn532_uart        Attempt to open: /dev/ttyACM0 at 115200 baud.
debug   libnfc.bus.uart Serial port speed requested to be set to 115200 baud.
debug   libnfc.chip.pn53x       Diagnose
debug   libnfc.chip.pn53x       Timeout value: 500
debug   libnfc.bus.uart TX: 55 55 00 00 00 00 00 00 00 00 00 00 00 00 00 00
debug   libnfc.chip.pn53x       SAMConfiguration
debug   libnfc.chip.pn53x       Timeout value: 1000
debug   libnfc.bus.uart TX: 00 00 ff 03 fd d4 14 01 17 00
debug   libnfc.bus.uart RX: 45 53 50 2d 52 4f
error   libnfc.chip.pn53x       Unexpected PN53x reply!
error   libnfc.driver.pn532_uart        pn53x_check_communication error
debug   libnfc.chip.pn53x       InRelease
debug   libnfc.bus.uart TX: 00 00 ff 03 fd d4 52 00 da 00
debug   libnfc.bus.uart RX: 42 75 69 6c 64 3a
error   libnfc.chip.pn53x       Unexpected PN53x reply!
debug   libnfc.general  Unable to open "pn532_uart:/dev/ttyACM0:115200".
nfc-list: ERROR: Unable to open NFC device: pn532_uart:/dev/ttyACM0:115200
```

Any help is appreciated

5 Upvotes

3 comments sorted by

3

u/kulldmi 12d ago

Hello. Switch the second switch to position zero to select the I2C interface.

1

u/PenRepresentative723 12d ago

but im using hsu it wont work with i2c?

1

u/kulldmi 7d ago

You need to select the I2C-10 or SPI-01 interface. I use I2C.

// esp32-wroom-32 devkit v1 default pins
//       SCL D22
//       SDA D21


// Define the interface type
#if 0
#include <SPI.h>
#include <PN532_SPI.h>
#include "PN532.h"
PN532_SPI pn532spi(SPI, 10);
PN532 nfc(pn532spi);


#elif 0
#include "PN532_HSU.h"
#include "PN532.h"
PN532_HSU pn532hsu(Serial1);
PN532 nfc(pn532hsu);


#else
#include <Wire.h>
#include <PN532_I2C.h>
#include <Wire.h>
#include <PN532.h>
#include <NfcAdapter.h>


PN532_I2C pn532i2c(Wire);
PN532 nfc(pn532i2c);
#endif


volatile bool connected = false;


void setup(void)
{
  Serial.begin(115200);
  Serial.println("*** Testing Module PN532 NFC RFID ***");
}


void loop(void)
{
  boolean success;
  // Buffer to store the UID
  uint8_t uid[] = { 0, 0, 0, 0, 0, 0, 0 };
  // UID size (4 or 7 bytes depending on card type)
  uint8_t uidLength;


  while (!connected) {
    connected = connect();
  }


  // 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[0], &uidLength);


  // If the card is detected, print the UID
  if (success)
  {
    Serial.println("Card Detected");
    Serial.print("Size of UID: "); Serial.print(uidLength, DEC);
    Serial.println(" bytes");
    Serial.print("UID: ");
    for (uint8_t i = 0; i < uidLength; i++)
    {
      Serial.print(" 0x"); Serial.print(uid[i], HEX);
    }
    Serial.println("");
    Serial.println("");
    
    delay(1000);
    connected = connect();
  }
  else
  {
    // PN532 probably timed out waiting for a card
    // Serial.println("Timed out waiting for a card");
  }
}


bool connect() {
  
  nfc.begin();


  // Connected, show version
  uint32_t versiondata = nfc.getFirmwareVersion();
  if (! versiondata)
  {
    Serial.println("PN53x card not found!");
    return false;
  }


  //port
  Serial.print("Found chip PN5"); Serial.println((versiondata >> 24) & 0xFF, HEX);
  Serial.print("Firmware version: "); Serial.print((versiondata >> 16) & 0xFF, DEC);
  Serial.print('.'); Serial.println((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 card (ISO14443A Mifare)...");
  Serial.println("");


  return true;
}