mardi 8 janvier 2019

Arduino Episode 9 : NFC (PN532 + LCD)





Projet sympa et presque complétement abouti.

Ici, un lecteur de tag NFC connecté sur un Arduino Nano + écran LCD pour afficher l'UID du tag (en Hexa, en Décimal et en mode MSB (reverse HEXA ou reverse Decimal).

Branchements:

NANO - LCD ou PN532
GND -- (LCD) GND

5V -- (LCD) VCC
A5-- (LCD) SCL
A4-- (LCD) SDA

3V3-- (PN532) 5V

GND --(PN532) GND
D9 -- (PN532) (non connecté)
D10 -- (PN532) SCL
D11 -- (PN532) MOSI
D12 -- (PN532) MISO
D13 -- (PN532) SCK

La boite :
-Une boite en carton
-une ouverture pour l'écran (tient tout seul)
-une ouverture pour le cable d'alimentation
-le lecteur NFC fixé à la paroi avant de la boite
et le reste (cables, arduino, rallonge de cable USB) jeté en vrac


Code: 
#include <Wire.h>
#include <SPI.h>
#include <Adafruit_PN532.h>
#include <LiquidCrystal_I2C.h>
#include <RCSwitch.h>

RCSwitch mySwitch = RCSwitch();
LiquidCrystal_I2C lcd(0x27, 20, 4);

// 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!");

lcd.init();

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");
lcd.backlight();
}

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)

lcd.clear();
lcd.setCursor(5,1);
lcd.print("DISPONIBLE");
lcd.setCursor(5,2);
lcd.print("==========");



// 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");
String cardIDH = "";
String cardIDRH = "";
uint32_t cardIDD;
uint32_t cardIDRD;


for (uint8_t i=0; i < uidLength; i++)
{
//Serial.print(" 0x");Serial.print(uid[i], HEX);
cardIDH += String(uid[i], HEX);
cardIDRH = String(uid[i], HEX) + cardIDRH;

cardIDRD += (uint32_t) uid[i] << (i*8);
cardIDD += (uint32_t) uid[int(uidLength)-i-1] << (i*8);

}

//cardIDD = (uint32_t) uid[3] << 0 | (uint32_t) uid[2] << 8 | (uint32_t) uid[1] << 16 | (uint32_t) uid[0] << 24;
//cardIDRD = (uint32_t) uid[0] << 0 | (uint32_t) uid[1] << 8 | (uint32_t) uid[2] << 16 | (uint32_t) uid[3] << 24;

Serial.println("");
Serial.print("UID HexaDecimal : ");Serial.println(cardIDH);
Serial.print("UID Reverse HexaDecimal : ");Serial.println(cardIDRH);
Serial.print("UID Decimal : ");Serial.println(cardIDD);
Serial.print("UID Reverse Decimal : ");Serial.println(cardIDRD);
Serial.println("");Serial.println("");

lcd.clear();

lcd.setCursor(0,0);
lcd.print("HEX :"+cardIDH);

lcd.setCursor(0,1);
lcd.print("DEC :");
lcd.setCursor(5,1);
lcd.print(cardIDD);

lcd.setCursor(0,2);
lcd.print("RHEX:"+cardIDRH);

lcd.setCursor(0,3);
lcd.print("RDEC:");
lcd.setCursor(5,3);
lcd.print(cardIDRD);

// Wait 1 second before continuing
delay(10000);
lcd.clear();
}
else
{
// PN532 probably timed out waiting for a card
Serial.println("Timed out waiting for a card");
}
}


.

Aucun commentaire:

Enregistrer un commentaire