Je me permets de demander de l'aide pour la mise en place d'un capteur NFC de type PN532 sur un ESP32-S3.
J'ai déjà fait plusieurs sites, mais je n'arrive sincèrement pas à trouver le code ni le schéma qui correspond à mon souci.
Quelqu'un a t il déjà été confronté à ce problème ? Pourriez vous m'aider svp ?
Peut tu décrire ton souci, car à priori tu n'arrive pas a utiliser un NFC avec ton ESP32-S3, mais on ne sait pas ce qui cloche.
Donc il est difficile de savoir si on a été confronté à ton problème ou comment corriger ce problème.
Mon souci c'est que j'ai beau essayé en UART, SPI ou I2C, je n'arrive pas à lire une carte nfc.
Cependant, je récupère des schémas de connexions et des bouts de code à gauche et à droite, et j'imagine que je ne fais ce qu'il faut.
Je recherche donc une personne qui a déjà configuré du NFC PN532 avec un ESP32-S3-Zero (https://www.waveshare.com/esp32-s3-zero.htm).
Désolé si cela pour vous sembler un peu brouillon.
Merci pour le lien web qui permet de lever toute ambiguïté concernant la carte ESP32-S3 que tu utilises, mais en faisant une recherche en utilisant le motif "NFC PN532", les propositions retournées présentent 4 cartes différentes qui sont équipées avec la puce PN352.
Pourrais-tu, comme pour ta carte ESP32-S3, donner un lien qui permettrait de voir les caractéristiques de la carte NFC PN532 que tu utilises ?
Ok, donc comme l'indique @amic si tu nous dit quelle carte NFC tu utilises, on pourra déjà déterminer avec quel protocole elle communique ou comment la configurer pour l'un d'eux, si elle en accepte plusieurs.
En général sur les modules la sérigraphie indique le nom des broches.
Elechouse a également un github, mais même en le suivant je n'arrive pas à trouver une configuration qui me permet de lire des cartes nfc. J'ai essayé en UART, et I2C, qui ont des connexions plus simples que le SPI.
C'est pour cela que je demande si quelqu'un s'est déjà confronté à ce souci.
Pour info, dernier test en date :
Côté branchement :
ESP32
PN532
GND
GND
5V
VCC
8
SDA
9
SCL
Côté code :
#include <SoftwareSerial.h>
#include <PN532_SWHSU.h>
#include <PN532.h>
SoftwareSerial SWSerial( 8, 9 ); // RX, TX
PN532_SWHSU pn532swhsu( SWSerial );
PN532 nfc( pn532swhsu );
String tagId = "None", dispTag = "None";
byte nuidPICC[4];
void setup(void) {
Serial.begin(115200);
Serial.println("Hello Maker!");
// Serial2.begin(115200, SERIAL_8N1, RXD2, TXD2);
nfc.begin();
uint32_t versiondata = nfc.getFirmwareVersion();
if (! versiondata) {
Serial.print("Didn't Find PN53x Module");
while (1); // Halt
}
// Got valid 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() {
readNFC();
}
void readNFC() {
boolean 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)
success = nfc.readPassiveTargetID(PN532_MIFARE_ISO14443A, &uid[0], &uidLength);
if (success) {
Serial.print("UID Length: "); Serial.print(uidLength, DEC); Serial.println(" bytes");
Serial.print("UID Value: ");
for (uint8_t i = 0; i < uidLength; i++) {
nuidPICC[i] = uid[i];
Serial.print(" "); Serial.print(uid[i], DEC);
}
Serial.println();
tagId = tagToString(nuidPICC);
dispTag = tagId;
Serial.print(F("tagId is : "));
Serial.println(tagId);
Serial.println("");
delay(1000); // 1 second halt
} else {
// PN532 probably timed out waiting for a card
//Serial.println("Timed out! Waiting for a card...");
}
}
String tagToString(byte id[4]) {
String tagId = "";
for (byte i = 0; i < 4; i++) {
if (i < 3) tagId += String(id[i]) + ".";
else tagId += String(id[i]);
}
return tagId;
}
Qu'entends tu par à trouver une configuration qui te permet de lire?
tu as essayer différentes combinaison des micro-switch pour sélectionner le protocole et utiliser le programme correspondant et cela ne fonctionne pas ?
si tu utilises les broches SDA et SCL, c'est que tu as configuré les micro-switch comme indiqué à la page 2 de ton document.
Edit: Ton code indique que tu veux utiliser le UART et le I2C partage ses broches avec l'UART.
par contre sur la documentation le TX partage sa broche avec SDA et RX avec SCL.
donc tu dois inversé les numéros des broches dans ton initialisation de l'instance SoftwareSerial.
J'ai enfin réussi à le faire fonctionner avec le mode I2C
Pour ce qui est du montage :
Pour ce qui est du code :
/**************************************************************************/
/*!
This example will attempt to connect to an ISO14443A
card or tag and retrieve some basic information about it
that can be used to determine what type of card it is.
Note that you need the baud rate to be 115200 because we need to print
out the data and read from the card at the same time!
To enable debug message, define DEBUG in PN532/PN532_debug.h
*/
/**************************************************************************/
/* When the number after #if set as 1, it will be switch to SPI Mode*/
#if 0
#include <SPI.h>
#include <PN532_SPI.h>
#include "PN532.h"
PN532_SPI pn532spi(SPI, 10);
PN532 nfc(pn532spi);
/* When the number after #elif set as 1, it will be switch to HSU Mode*/
#elif 0
#include <PN532_HSU.h>
#include <PN532.h>
PN532_HSU pn532hsu(Serial1);
PN532 nfc(pn532hsu);
/* When the number after #if & #elif set as 0, it will be switch to I2C Mode*/
#else
#include <Wire.h>
#include <PN532_I2C.h>
#include <PN532.h>
#include <NfcAdapter.h>
PN532_I2C pn532i2c(Wire);
PN532 nfc(pn532i2c);
#endif
void setup(void) {
Serial.begin(115200);
Serial.println("Hello!");
nfc.begin();
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);
// 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");
}
void loop(void) {
boolean 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[0], &uidLength);
if (success) {
Serial.println("Found a card!");
Serial.print("UID Length: ");Serial.print(uidLength, DEC);Serial.println(" bytes");
Serial.print("UID Value: ");
for (uint8_t i=0; i < uidLength; i++)
{
Serial.print(" 0x");Serial.print(uid[i], HEX);
}
Serial.println("");
// Wait 1 second before continuing
delay(1000);
}
else
{
// PN532 probably timed out waiting for a card
Serial.println("Timed out waiting for a card");
}
}
Quand je rapproche la carte, voici la log dans la partie Serial Monitor :
23:46:26.062 -> Hello!
23:46:26.062 -> Found chip PN532
23:46:26.062 -> Firmware ver. 1.6
23:46:26.062 -> Waiting for an ISO14443A card
23:46:29.806 -> Found a card!
23:46:29.806 -> UID Length: 4 bytes
23:46:29.806 -> UID Value: 0xC9 0x8A 0x1A 0x83