vendredi 24 août 2018

Arduino Episode 6 : Module NFC PN532




(Arduino Uno)
Bonjour,

J'ai acquis depuis un moment maintenant un module NFC (sans marque ni modèle) qui pourrait s'aparenter à un PN532 de chez IteadStudio.
Voici la bête :

Au delà de ses 2 rangées de PIN, on peut trouver 2 curseurs (à droite) Indiquant SET0 et SET1 position LOW ou High.
En retournant la carte on trouve le tableau suivant:


SET0SET1
LOWLOWUART
LOWHIGHSPI
HIGHLOWIIC

1°) Les Curseurs
Déjà nous allons utiliser le protocole SPI (je n'ai rien inventé, j'ai juste repris  les infos d'autres personnes
Donc SET0 = LOW et SET1 = HIGH

2°) Branchements
Laissons la grande rangée de PIN et focalisons nous sur la petite:
NFC > Arduino
SCK > 13
MI > 12
MO/SDA/TX > 11
NSS/SCL/RX >10
IRQ

RST
GND > GND
5V > 5V


3°) Le Code
Nous allons utiliser le code d'ADAFRUIT et le modifier légèrement
Dans Arduino IDE, Croquis, Inclure une bibliothèque, Gérer les Bibliothèques, chercher PN532 de Adafruit.
Ouvrir l'exemple : Fichier, exemple, Adafruit PN532, ISO14443a_uid

Changez les ports ainsi: (pour correspondre au branchement)
// If using the breakout with SPI, define the pins for SPI communication.
#define PN532_SCK  (13)
#define PN532_MOSI (12)
#define PN532_SS   (10)
#define PN532_MISO (11)

Soit au final:
/**************************************************************************/
/*!
@file iso14443a_uid.pde
@author Adafruit Industries
@license BSD (see license.txt)

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!

This is an example sketch for the Adafruit PN532 NFC/RFID breakout boards
This library works with the Adafruit NFC breakout
----> https://www.adafruit.com/products/364

Check out the links above for our tutorials and wiring diagrams
These chips use SPI or I2C to communicate.

Adafruit invests time and resources providing this open source code,
please support Adafruit and open-source hardware by purchasing
products from Adafruit!

*/
/**************************************************************************/
#include <Wire.h>
#include <SPI.h>
#include <Adafruit_PN532.h>

// If using the breakout with SPI, define the pins for SPI communication.
#define PN532_SCK (13)
#define PN532_MOSI (12)
#define PN532_SS (10)
#define PN532_MISO (11)

// If using the breakout or shield with I2C, define just the pins connected
// to the IRQ and reset lines. Use the values below (2, 3) for the shield!
#define PN532_IRQ (2)
#define PN532_RESET (3) // Not connected by default on the NFC Shield

// Uncomment just _one_ line below depending on how your breakout or shield
// is connected to the Arduino:

// Use this line for a breakout with a SPI connection:
//Adafruit_PN532 nfc(PN532_SCK, PN532_MISO, PN532_MOSI, PN532_SS);

// Use this line for a breakout with a hardware SPI connection. Note that
// the PN532 SCK, MOSI, and MISO pins need to be connected to the Arduino's
// hardware SPI SCK, MOSI, and MISO pins. On an Arduino Uno these are
// SCK = 13, MOSI = 11, MISO = 12. The SS line can be any digital IO pin.
Adafruit_PN532 nfc(PN532_SS);

// Or use this line for a breakout or shield with an I2C connection:
//Adafruit_PN532 nfc(PN532_IRQ, PN532_RESET);

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");
}
}
4°)Moniteur Série:
Au début j'avais le message:
“Didn't find PN53x board
en fait, j'avais suivi un vidéo youtube (voir lien) qui préconisait de commenter la ligne
Adafruit_PN532 nfc(PN532_SCK, PN532_MISO, PN532_MOSI, PN532_SS);
et de décommenter la ligne
//Adafruit_PN532 nfc(PN532_IRQ, PN532_RESET);

Ce n'était pas une bonne solution.

[Edit 31/12/18] Sur du NANO : Voici ce que j'ai commenté et décommenté :

Commenté: //Adafruit_PN532 nfc(PN532_SCK, PN532_MISO, PN532_MOSI, PN532_SS);
Décommenté: Adafruit_PN532 nfc(PN532_SS);
Commenté car pas I2C ://Adafruit_PN532 nfc(PN532_IRQ, PN532_RESET);

En passant un badge NFC:
Hello!
Found chip PN532
Firmware ver. 1.6
Waiting for an ISO14443A card
Found a card!
UID Length: 4 bytes
UID Value:  0xDB 0xAD 0xC9 0x50


Et Voilà...

5°) Sources
https://www.youtube.com/watch?v=TvKOhcHH32M
http://lantaukwcounter.blogspot.com/2015/11/reading-octopus-card-ids-felica-with.html
https://forum.snootlab.com/viewtopic.php?f=45&t=1091
https://arduino.stackexchange.com/questions/49616/problem-didnt-find-pn53x-board-in-arduino-uno-e-card-reader




Aucun commentaire:

Enregistrer un commentaire