ESP32 + NFC PN532
//====================================================
// esp32-wroom-32 devkit v1 default pins
// SCL D22
// SDA D21
//====================================================
#include <Wire.h>
#include <PN532_I2C.h>
#include <PN532.h>
#include <NfcAdapter.h>
PN532_I2C pn532_i2c(Wire);
NfcAdapter nfc = NfcAdapter(pn532_i2c);
/* Uno's A4 to SDA & A5 to SCL */
//=============================================
void setup(void) {
Serial.begin(115200);
Serial.println("NFC TAG READER"); // Header used when using the serial monitor
nfc.begin();
}
//=============================================
void loop(void) {
Serial.println("\nScan your NFC tag on the NFC Shield\n"); // Command so that you an others will know what to do
if (nfc.tagPresent())
{
NfcTag tag = nfc.read();
Serial.println(tag.getTagType());
Serial.print("UID: ");
String UID=tag.getUidString();
//Serial.println(tag.getUidString()); // Retrieves the Unique Identification from your tag
Serial.println(UID); // Retrieves the Unique Identification from your tag
}
delay(5000);
}
//====================================================
// esp32-wroom-32 devkit v1 default pins
// SCL D22
// SDA D21
//====================================================
#include <Wire.h>
#include <PN532_I2C.h>
#include <PN532.h>
#include <NfcAdapter.h>
PN532_I2C pn532_i2c(Wire);
NfcAdapter nfc = NfcAdapter(pn532_i2c);
/* Uno's A4 to SDA & A5 to SCL */
//=============================================
void setup(void) {
Serial.begin(115200);
Serial.println("NFC TAG READER"); // Header used when using the serial monitor
nfc.begin();
}
//=============================================
void loop(void) {
Serial.println("\nScan your NFC tag on the NFC Shield\n"); // Command so that you an others will know what to do
if (nfc.tagPresent())
{
NfcTag tag = nfc.read();
Serial.println(tag.getTagType());
Serial.print("UID: ");
String UID=tag.getUidString();
//Serial.println(tag.getUidString()); // Retrieves the Unique Identification from your tag
Serial.println(UID); // Retrieves the Unique Identification from your tag
if (tag.hasNdefMessage()) // If your tag has a message
{
NdefMessage message = tag.getNdefMessage();
Serial.print("\nThis Message in this Tag is ");
Serial.print(message.getRecordCount());
Serial.print(" NFC Tag Record");
if (message.getRecordCount() != 1) {
Serial.print("s");
}
Serial.println(".");
// If you have more than 1 Message then it wil cycle through them
int recordCount = message.getRecordCount();
for (int i = 0; i < recordCount; i++)
{
Serial.print("\nNDEF Record ");Serial.println(i+1);
NdefRecord record = message.getRecord(i);
int payloadLength = record.getPayloadLength();
byte payload[payloadLength];
record.getPayload(payload);
String payloadAsString = ""; // Processes the message as a string vs as a HEX value
for (int c = 0; c < payloadLength; c++) {
payloadAsString += (char)payload[c];
}
Serial.print(" Information (as String): ");
Serial.println(payloadAsString);
String uid = record.getId();
if (uid != "") {
Serial.print(" ID: ");Serial.println(uid); // Prints the Unique Identification of the NFC Tag
}
}
}
}
delay(5000);
}
沒有留言:
張貼留言