PN532 with SoftwareSerial

Hey Guys,

i want to use the PN532 nfc-reader with Software Serial on my ESP32. On arduino i can use the PN532_SWHSU library for connection via software serial but on Plattform io i dont know why it will not working.

i have added the softwareSerial library and the PN532 library.

following code doesn’t work:

#include <SoftwareSerial.h>
#include <Adafruit_PN532.h>
#include <WKPNFC.h>

SoftwareSerial SWSerial( 33, 32 ); // no errror // this
Adafruit_PN532 nfc( SWSerial ); // Error = no instance of constructor

String tagId = “None”, dispTag = “None”;

thank you

You need to carefully look at the library code. You have the following constructors

The last one takes a HardwareSerial* as the second argument, the first one being the reset pin.

However, you have another problem: a SoftwareSerial object implementation, such as the one from this library or this library, does not inherit from HardwareSerial. Really, the Adafruit library should be more liberal and take in Stream*; but it doesn’t it.

So this leads you to either modify the library to acommodate the possibility of a software serial, or, use a hardware serial. Since the ESP32 has three hardware UARTs, I don’t see a point in using a software implementation in the first place.

And thus, you could e.g. write

#define RXD2 16
#define TXD2 17

#define PN532_RESET 13 /* put correct GPIO pin here */
Adafruit_PN532 nfc(PN532_RESET, &Serial2);

void setup() { 
  Serial2.begin(115200, SERIAL_8N1, RXD2, TXD2);
  /* old code, nfc.begin() etc */ 

with Serial2 pins as GPIO 16, GPIO17.

Okay, correction.

You talk about the PN532_SWHSU library but use the Adafruit library in your code. That library really takes a software serial reference.

And I don’t see why that would not work if you supply the correct SoftwareSerial libary. Especially if it worked in the Arduino IDE.

So please post your full platformio.ini and error mesage that you had when you tried to use the PN532_SWHSU library.

Also, the point about using a hardware serial such as Serial2 still holds. That library has PN532_HSU for that purpose.