ESP32 + PN532 NFC Reader I2C Mode
參考來源
http://www.elechouse.com/elechouse/images/product/PN532_module_V3/PN532_%20Manual_V3.pdf
https://www.taiwaniot.com.tw/product/pn532-nfc-rfid-v3-%E6%A8%A1%E7%B5%84-%E8%BF%91%E5%A0%B4%E9%80%9A%E8%A8%8A-%E6%94%AF%E6%8C%81-android-%E6%89%8B%E6%A9%9F%E9%80%9A%E8%A8%8A/
使用Library https://github.com/elechouse/PN532
PN532 NFC RFID V3 模組 近場通訊 支持 Android 手機通訊
NFC又稱近距離無線通信,是一種短距離的高頻無線通信技術,允許電子設備之間進行非接觸式點對點數據傳輸(在十厘米內)交換數據。這個技術由免接觸式射頻識別(RFID)演變而來,並向下兼容RFID,最早由Sony和Philips各自開發成功,主要用於手機等手持設備中提供M2M(Machine to Machine)的通信。由於近場通訊具有天然的安全性,因此,NFC技術被認為在手機支付等領域具有很大的應用前景。
利用NXP532的幾乎所有管腳都,因此用戶可以輕鬆的調試和把玩。該進場通訊模組默認才用I2C作為數據接埠
- 支援I2C、SPI、HSU(高速UART),可以很容易在這些通訊方式之間進行切換,通過板子左下角的撥動開關即可輕鬆實現通訊方式的切換
- Work in NFC Mode or RFID reader/writer Mode(可工作在NFC狀態、RFID讀或者寫狀態)
- RFID reader/writer supports: (RDID讀寫支援)
Mifare 1k, 4k, Ultralight, 和DesFire 卡
ISO/IEC 14443-4 cards,如CD97BX, CD light, Desfire, P5CN072 (SMX)
Innovision Jewel cards,如IRT5001 card
FeliCa cards ,如RCS_860 和RCS_854 - 即插即用,相容Arduino
- 板內天線, 支援 1cm-5ccm 通訊距離
- On-board level shifter, 標準5V TTL for I2C and UART, 3.3V TTL for SPI
- 可工作在 RFID 讀/寫模式
- 可工作在 1443-A card 或者 虛擬card模式
- 可與其他NFC設備交換數據,如Android手機
- Interface 數據埠 I2C 是默認數據埠. 用戶也可根據自己需要利用引出的管腳改變數據傳輸方式,如串埠,SPI等
Connecting an I2C device to an ESP32 is normally as simple as connecting GND to GND, SDA to SDA, SCL to SCL and a positive power supply to a peripheral, usually 3.3V (but it depends on the module you’re using).
I2C Device | ESP32 |
SDA | SDA (default is GPIO 21) |
SCL | SCL (default is GPIO 22) |
GND | GND |
VCC | usually 3.3V or 5V |
使用Library https://github.com/elechouse/PN532
解壓縮後存入 目錄 C:\Users\alex\Documents\Arduino\libraries
將其他MODE 刪除掉 保留I2C Mode
支持I2C .SPI 和HSU(高速UART)
I2C --> Wire,h
SPI --> SPI.h
HSU (High Speed UART) -->PN532_HSU.h
// SDA SDA (default is GPIO 21) ESP32
// SCL SCL (default is GPIO 22)
/* it will be switch to I2C Mode*/
#include <Wire.h>
#include <PN532_I2C.h>
#include <PN532.h>
#include <NfcAdapter.h>
PN532_I2C pn532i2c(Wire);
PN532 nfc(pn532i2c);
void setup(void) {
Serial.begin(115200);
Serial.println("Hello!");
Serial.println("SDA (default is GPIO 21) SCL (default is GPIO 22)");
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");
}
}
沒有留言:
張貼留言