The Electroniccats_PN7150
class enables Arduino library for I2C access to the PN7150 RFID/Near Field Communication chip.
Include and instantiate the Electroniccats_PN7150 class. Creates a global NFC device interface object, attached to pins 7 (IRQ) and 8 (VEN) and using the default I2C address 0x28
#include <Electroniccats_PN7150.h>
Electroniccats_PN7150 nfc(PN7150_IRQ, PN7150_VEN, PN7150_ADDR);
uint8_t PN7150_IRQ
: IRQ pin for data interrupt.uint8_t PN7150_VEN
: "Reset" or "Enable" pin for device.uint8_t PN7150_ADDR
: Hexadecimal address for device, default0x28
.
#include "Electroniccats_PN7150.h"
#define PN7150_IRQ (8)
#define PN7150_VEN (7)
#define PN7150_ADDR (0x28)
Electroniccats_PN7150 nfc(PN7150_IRQ, PN7150_VEN, PN7150_ADDR); // Creates a global NFC device interface object, attached to pins 7 (IRQ) and 8 (VEN) and using the default I2C address 0x28
void setup(){
Serial.begin(115200);
while(!Serial);
Serial.println("Detect NFC readers with PN7150");
uint8_t statusNFC = setupNFC();
if (!statusNFC)
Serial.println("Set up is ok");
else
Serial.println("Error while setting up mode, check connections!");
}
int setupNFC(){
Serial.println("Initializing...");
int setupOK = nfc.connectNCI(); // Wake up the board
if (!setupOK){
setupOK = nfc.configMode(); // Set up the configuration mode
if (!setupOK) setupOK = nfc.startDiscovery(); // NCI Discovery mode
}
return setupOK;
}
Get the firmware version of the NXP-NCI controller.
int getFirmwareVersion();
int version = nfc.getFirmwareVersion();
Initialize the connection with the NXP-NCI controller and updates the firmware version that can be obtained with the getFirmwareVersion
method.
uint8_t connectNCI();
Returns 0
if the connection is established correctly, otherwise returns 1
.
uint8_t statusNFC = nfc.connectNCI();
if (!statusNFC) {
Serial.println("Set up is ok");
} else {
Serial.println("Error while setting up mode, check connections!");
}
Configure the device mode. Reader/Writer as default.
uint8_t configMode();
Returns 0
if the mode is configured correctly, otherwise returns 1
.
uint8_t status = nfc.configMode();
if (!status) {
Serial.println("Set up is ok");
} else {
Serial.println("Error while setting up mode, check connections!");
}
Configure the device mode to Reader/Writer.
bool setReaderWriterMode();
Returns true
if the mode is configured correctly, otherwise returns false
.
bool status = nfc.setReaderWriterMode();
if (status) {
Serial.println("Set up is ok");
} else {
Serial.println("Error while setting up mode, check connections!");
}
Configure the device mode to Card Emulation.
bool setEmulationMode();
Returns true
if the mode is configured correctly, otherwise returns false
.
bool status = nfc.setEmulationMode();
if (status) {
Serial.println("Set up is ok");
} else {
Serial.println("Error while setting up mode, check connections!");
}
Configure the device mode to Peer to Peer.
bool setP2PMode();
Returns true
if the mode is configured correctly, otherwise returns false
.
bool status = nfc.setP2PMode();
if (status) {
Serial.println("Set up is ok");
} else {
Serial.println("Error while setting up mode, check connections!");
}
Get the current device mode.
int getMode();
switch (nfc.getMode()) {
case nfc.mode.READER_WRITER:
Serial.println("Reader/Writer mode");
break;
case nfc.mode.EMULATION:
Serial.println("Card Emulation mode");
break;
case nfc.mode.P2P:
Serial.println("Peer to Peer mode");
break;
default:
Serial.println("Unknown mode");
break;
}
Configure some aspects of the NFC controller, such as the hardware configuration, RF (radio frequency) configuration, and other settings.
bool configureSettings();
A custom NFC UID can be configured by passing the UID and its length as parameters.
bool configureSettings(uint8_t *nfcuid, uint8_t uidlen);
Returns 0
if the parameters are configured correctly, otherwise returns 1
.
bool status = nfc.configureSettings();
if (!status) {
Serial.println("Set up is ok");
} else {
Serial.println("Error while setting up mode, check connections!");
}
uint8_t nfcuid[] = {0x08, 0x00, 0x00, 0x00, 0x00, 0x00};
uint8_t uidlen = 6;
bool status = nfc.configureSettings(nfcuid, uidlen);
if (!status) {
Serial.println("Set up is ok");
} else {
Serial.println("Error while setting up mode, check connections!");
}
Start the discovery mode for the device.
uint8_t startDiscovery();
Returns 0
if the mode is configured correctly, otherwise returns 1
.
uint8_t status = nfc.startDiscovery();
if (!status) {
Serial.println("Set up is ok");
} else {
Serial.println("Error while setting up mode, check connections!");
}
Stop the discovery process of the device.
bool stopDiscovery();
Returns 0
. Never returns 1
.
nfc.stopDiscovery();
Returns true
if a tag is detected, otherwise returns false
. The timeout is set to 500ms by default and can be changed by passing a parameter.
bool isTagDetected(uint16_t tout = 500);
if (nfc.isTagDetected()) {
Serial.println("Tag detected!");
// Do something
}
Send a data packet in card mode.
bool cardModeSend(unsigned char *pData, unsigned char DataSize);
Receive a data packet from a card.
bool cardModeReceive(unsigned char *pData, unsigned char *pDataSize);
Resets the card emulation state and processes the card mode for card emulation.
void handleCardEmulation();
Waits for the tag to be removed.
void waitForTagRemoval();
if (nfc.isTagDetected()) {
Serial.println("Remove the Card");
nfc.waitForTagRemoval();
Serial.println("Card removed!");
}
Sends a command to the reader.
bool readerTagCmd(unsigned char *pCommand, unsigned char CommandSize, unsigned char *pAnswer, unsigned char *pAnswerSize);
Reactivates a target after it has been deactivated.
bool readerReActivate();
Activates the next tag discovered.
bool activateNextTagDiscovery();
if (nfc.isTagDetected()) {
// It can detect multiple cards at the same time if they use the same protocol
if (nfc.remoteDevice.hasMoreTags()) {
Serial.println("Multiple cards are detected!");
nfc.activateNextTagDiscovery();
}
}
Reads the NDEF message from the tag.
void readNdefMessage();
if (nfc.isTagDetected()) {
nfc.readNdefMessage();
}
Writes the NDEF message to the tag.
void writeNdefMessage();
Performs a factory test for the NCI controller.
bool nciFactoryTestPrbs(NxpNci_TechType_t type, NxpNci_Bitrate_t bitrate);
Performs a factory test for the NCI controller.
bool nciFactoryTestRfOn();
Stops the discovery process, configures the device mode if it is necessary and starts the discovery process again.
bool reset();
nfc.reset();
Registers a callback function to be called when an NDEF message is received.
void setReadMsgCallback(CustomCallback_t function);
void messageReceived() {
Serial.println("Message received!");
}
void setup() {
nfc.setReadMsgCallback(messageReceived);
}
Returns true
if a reader is detected, otherwise returns false
.
bool isReaderDetected();
if (nfc.isReaderDetected()) {
Serial.println("Reader detected!");
// Do something
}
Send a command to the reader to close the communication.
void closeCommunication();
if (nfc.isReaderDetected()) {
Serial.println("Reader detected!");
nfc.handleCardEmulation();
nfc.closeCommunication();
}
Send an NDEF message to the reader.
void sendMessage();
if (nfc.isReaderDetected()) {
Serial.println("Reader detected!");
Serial.println("Sending NDEF message...");
nfc.sendMessage();
}
Constant for the undetermined interface.
UNDETERMINED = 0x0
nfc.interface.UNDETERMINED;
Constant for the frame interface.
FRAME = 0x1
nfc.interface.FRAME;
Constant for the ISO-DEP interface.
ISODEP = 0x2
nfc.interface.ISODEP;
Constant for the NFC-DEP interface.
NFCDEP = 0x3
nfc.interface.NFCDEP;
Constant for the tag interface.
TAGCMD = 0x80
nfc.interface.TAGCMD;
Constant for the Reader/Writer mode.
READER_WRITER = 1
nfc.mode.READER_WRITER;
Constant for the Card Emulation mode.
EMULATION = 2
nfc.mode.EMULATION;
Constant for the Peer to Peer mode.
P2P = 3
nfc.mode.P2P;
Constant for the Poll mode.
POLL = 0x00
nfc.modeTech.POLL;
Constant for the Listen mode.
LISTEN = 0x80
nfc.modeTech.LISTEN;
Constant for the Mask mode.
MASK = 0xF0
nfc.modeTech.MASK;
Constant for the undetermined protocol.
UNDETERMINED = 0x0
nfc.protocol.UNDETERMINED;
Constant for the T1T protocol.
T1T = 0x1
nfc.protocol.T1T;
Constant for the T2T protocol.
T2T = 0x2
nfc.protocol.T2T;
Constant for the T3T protocol.
T3T = 0x3
nfc.protocol.T3T;
Constant for the ISO-DEP protocol.
ISODEP = 0x4
nfc.protocol.ISODEP;
Constant for the NFC-DEP protocol.
NFCDEP = 0x5
nfc.protocol.NFCDEP;
Constant for the ISO15693 protocol.
ISO15693 = 0x6
nfc.protocol.ISO15693;
Constant for the MIFARE protocol.
MIFARE = 0x80
nfc.protocol.MIFARE;
Constant for the Passive NFC-A technology.
PASSIVE_NFCA = 0
nfc.tech.PASSIVE_NFCA;
Constant for the Passive NFC-B technology.
PASSIVE_NFCB = 1
nfc.tech.PASSIVE_NFCB;
Constant for the Passive NFC-F technology.
PASSIVE_NFCF = 2
nfc.tech.PASSIVE_NFCF;
Constant for the Active NFC-A technology.
ACTIVE_NFCA = 3
nfc.tech.ACTIVE_NFCA;
Constant for the Active NFC-F technology.
ACTIVE_NFCF = 5
nfc.tech.ACTIVE_NFCF;
Constant for the Passive 15693 technology.
PASSIVE_15693 = 6
nfc.tech.PASSIVE_15693;
Constant for the Passive NFC-V technology (ISO 15693).
PASSIVE_NFCV = 6
nfc.tech.PASSIVE_NFCV;
A RemoteDevice
object represents a remote NFC device such as a tag or a reader.
Get the interface of the device.
unsigned char getInterface() const;
if (nfc.isTagDetected()) {
Serial.print("Interface: ");
switch (nfc.remoteDevice.getInterface()) {
case nfc.interface.ISODEP:
Serial.println("ISO-DEP");
break;
case nfc.interface.NFCDEP:
Serial.println("NFC-DEP");
break;
case nfc.interface.TAGCMD:
Serial.println("TAG");
break;
case nfc.interface.FRAME:
Serial.println("FRAME");
break;
case nfc.interface.UNDETERMINED:
Serial.println("UNDETERMINED");
break;
default:
Serial.println("UNKNOWN");
break;
}
}
Get the protocol of the device.
unsigned char getProtocol() const;
if (nfc.isTagDetected()) {
Serial.print("Protocol: ");
switch (nfc.remoteDevice.getProtocol()) {
case nfc.protocol.T1T:
Serial.println("T1T");
break;
case nfc.protocol.T2T:
Serial.println("T2T");
break;
case nfc.protocol.T3T:
Serial.println("T3T");
break;
case nfc.protocol.ISODEP:
Serial.println("ISO-DEP");
break;
case nfc.protocol.NFCDEP:
Serial.println("NFC-DEP");
break;
case nfc.protocol.ISO15693:
Serial.println("ISO15693");
break;
case nfc.protocol.MIFARE:
Serial.println("MIFARE");
break;
case nfc.protocol.UNDETERMINED:
Serial.println("UNDETERMINED");
break;
default:
Serial.println("UNKNOWN");
break;
}
}
Get the mode tech of the device.
unsigned char getModeTech() const;
if (nfc.isTagDetected()) {
Serial.print("Technology: ");
switch (nfc.remoteDevice.getModeTech()) {
case nfc.tech.PASSIVE_NFCA:
Serial.println("PASSIVE NFC A");
break;
case nfc.tech.PASSIVE_NFCB:
Serial.println("PASSIVE NFC B");
break;
case nfc.tech.PASSIVE_NFCF:
Serial.println("PASSIVE NFC F");
break;
case nfc.tech.PASSIVE_15693:
Serial.println("PASSIVE 15693");
break;
}
}
Returns true
if there are more tags to be discovered, otherwise returns false
.
bool hasMoreTags() const;
if (nfc.isTagDetected()) {
if (nfc.remoteDevice.hasMoreTags()) {
Serial.println("Multiple cards are detected!");
nfc.activateNextTagDiscovery();
}
}
Get the SENS RES length of the device.
unsigned char getSensResLen() const;
Get the SENS RES (ATQA) of the device. Only available for tags that use passive communication and the NFC-A, NFC-B or NFC-F technologies, otherwise returns NULL
.
const unsigned char* getSensRes() const;
if (nfc.isTagDetected()) {
Serial.print("ATQA: ");
for (int i = 0; i < nfc.remoteDevice.getSensResLen(); i++) {
Serial.print(nfc.remoteDevice.getSensRes()[i], HEX);
Serial.print(" ");
}
Serial.println();
}
Get the SEL RES length of the device.
unsigned char getSelResLen() const;
Get the SEL RES (SAK) of the device. Only available for tags that use passive communication and the NFC-A technology, otherwise returns NULL
.
const unsigned char* getSelRes() const;
if (nfc.isTagDetected()) {
Serial.print("SAK: ");
for (int i = 0; i < nfc.remoteDevice.getSelResLen(); i++) {
Serial.print(nfc.remoteDevice.getSelRes()[i], HEX);
Serial.print(" ");
}
Serial.println();
}
Get the NFCID length of the device.
unsigned char getNFCIDLen() const;
Get the NFCID of the device. Only available for tags that use passive communication and the NFC-A technology, otherwise returns NULL
.
const unsigned char* getNFCID() const;
if (nfc.isTagDetected()) {
Serial.print("NFCID: ");
for (int i = 0; i < nfc.remoteDevice.getNFCIDLen(); i++) {
Serial.print(nfc.remoteDevice.getNFCID()[i], HEX);
Serial.print(" ");
}
Serial.println();
}
Get the RATS length of the device.
unsigned char getRatsLen() const;
Get the RATS of the device. Only available for tags that use passive communication and the NFC-A technology, otherwise returns NULL
.
const unsigned char* getRats() const;
if (nfc.isTagDetected()) {
Serial.print("RATS: ");
for (int i = 0; i < nfc.remoteDevice.getRatsLen(); i++) {
Serial.print(nfc.remoteDevice.getRats()[i], HEX);
Serial.print(" ");
}
Serial.println();
}
Get the ATTRIB RES length of the device.
unsigned char getAttribResLen() const;
Get the ATTRIB RES of the device. Only available for tags that use passive communication and the NFC-B technology, otherwise returns NULL
.
const unsigned char* getAttribRes() const;
if (nfc.isTagDetected()) {
Serial.print("ATTRIB RES: ");
for (int i = 0; i < nfc.remoteDevice.getAttribResLen(); i++) {
Serial.print(nfc.remoteDevice.getAttribRes()[i], HEX);
Serial.print(" ");
}
Serial.println();
}
Get the bit rate of the device. Only available for tags that use passive communication and the NFC-F technology, otherwise returns NULL
.
unsigned char getBitRate() const;
Serial.print("Bitrate = ");
Serial.println((nfc.remoteDevice.getBitRate() == 1) ? "212" : "424");
Get the AFI of the device. Only available for tags that use passive communication and the NFC-V technology, otherwise returns NULL
.
unsigned char getAFI() const;
if (nfc.isTagDetected()) {
Serial.print("AFI = ");
Serial.println(nfc.remoteDevice.getAFI());
}
Get the DSF ID of the device. Only available for tags that use passive communication and the NFC-V technology, otherwise returns NULL
.
unsigned char getDSFID() const;
if (nfc.isTagDetected()) {
Serial.print("DSF ID = ");
Serial.println(nfc.remoteDevice.getDSFID(), HEX);
}
Get the ID of the device. Only available for tags that use passive communication and the NFC-V technology, otherwise returns NULL
.
const unsigned char* getID() const;
if (nfc.isTagDetected()) {
Serial.print("ID: ");
for (int i = 0; i < sizeof(nfc.remoteDevice.getID()); i++) {
Serial.print(nfc.remoteDevice.getID()[i], HEX);
Serial.print(" ");
}
Serial.println();
}
A NdefMessage
object represents an NDEF message. An NDEF message is a container for one or more NDEF records.
Registers a callback function to be called when an NDEF message is received wich updates the message content.
void begin();
NdefMessage message;
void setup() {
message.begin();
}
Get the content size of the message.
static unsigned short getContentSize();
Get the content of the message.
static unsigned char *getContent();
Set the content of the message.
static void setContent(unsigned char *content, unsigned short contentSize);
Get the record of the message.
NdefRecord_t getRecord();
The record is a structure that contains the following properties:
typedef struct {
NdefRecordType_e recordType;
unsigned char *recordPayload;
unsigned int recordPayloadSize;
} NdefRecord_t;
message.getRecord();
Returns true
if the message is empty, otherwise returns false
.
bool isEmpty();
if (message.isEmpty()) {
Serial.println("The message is empty!");
}
Returns true
if the message is not empty, otherwise returns false
.
bool isNotEmpty();
if (message.isNotEmpty()) {
Serial.println("The message is not empty!");
}
Returns true
if the message has a record, otherwise returns false
.
bool hasRecord();
if (message.hasRecord()) {
Serial.println("The message has a record!");
}
A NdefRecord
object represents an NDEF record. An NDEF record is a data structure that contains data that is stored or transported in an NDEF message.
NdefRecord record;
Creates a new NDEF record.
void create(NdefRecord_t record);
record.create(message.getRecord());
Returns true
if the record is empty, otherwise returns false
.
bool isEmpty();
if (record.isEmpty()) {
Serial.println("The record is empty!");
}
Returns true
if the record is not empty, otherwise returns false
.
bool isNotEmpty();
if (record.isNotEmpty()) {
Serial.println("The record is not empty!");
}
Get the type of the record.
NdefRecordType_e getType();
The type is a structure that contains the following properties:
typedef enum {
WELL_KNOWN_SIMPLE_TEXT,
WELL_KNOWN_SIMPLE_URI,
WELL_KNOWN_SMART_POSTER,
WELL_KNOWN_HANDOVER_SELECT,
WELL_KNOWN_HANDOVER_REQUEST,
WELL_KNOWN_ALTERNATIVE_CARRIER,
WELL_KNOWN_COLLISION_RESOLUTION,
MEDIA_VCARD,
MEDIA_HANDOVER_WIFI,
MEDIA_HANDOVER_BT,
MEDIA_HANDOVER_BLE,
MEDIA_HANDOVER_BLE_SECURE,
ABSOLUTE_URI,
UNSUPPORTED_NDEF_RECORD = 0xFF
} NdefRecordType_e;
Get the payload size of the record.
unsigned short getPayloadSize();
Get the payload of the record.
unsigned char *getPayload();
Serial.print("Payload: ");
for (int i = 0; i < record.getPayloadSize(); i++) {
Serial.print(record.getPayload()[i], HEX);
Serial.print(" ");
}
Serial.println();
Get the text of the record if the type is WELL_KNOWN_SIMPLE_TEXT
, otherwise returns "null"
.
String getText();
Serial.print("Text: ");
Serial.println(record.getText());
Get the Bluetooth name of the record if the type is MEDIA_HANDOVER_BT
, otherwise returns "null"
.
String getBluetoothName();
Serial.print("Bluetooth name: ");
Serial.println(record.getBluetoothName());
Get the Bluetooth address of the record if the type is MEDIA_HANDOVER_BT
, otherwise returns "null"
.
String getBluetoothAddress();
Serial.print("Bluetooth address: ");
Serial.println(record.getBluetoothAddress());
Get the WiFi SSID of the record if the type is MEDIA_HANDOVER_WIFI
, otherwise returns "null"
.
String getWifiSSID();
Serial.print("WiFi SSID: ");
Serial.println(record.getWifiSSID());
Get the WiFi password of the record if the type is MEDIA_HANDOVER_WIFI
, otherwise returns "null"
.
String getWiFiPassword();
Serial.print("WiFi password: ");
Serial.println(record.getWiFiPassword());
Get the WiFi authentication type of the record if the type is MEDIA_HANDOVER_WIFI
, otherwise returns "null"
.
String getWiFiAuthenticationType();
Serial.print("WiFi authentication type: ");
Serial.println(record.getWiFiAuthenticationType());
Get the WiFi encryption type of the record if the type is MEDIA_HANDOVER_WIFI
, otherwise returns "null"
.
String getWiFiEncryptionType();
Serial.print("WiFi encryption type: ");
Serial.println(record.getWiFiEncryptionType());
Get the vCard content of the record if the type is MEDIA_VCARD
, otherwise returns "null"
.
String getVCardContent();
Serial.print("vCard content: ");
Serial.println(record.getVCardContent());
Get the URI of the record if the type is WELL_KNOWN_SIMPLE_URI
, otherwise returns "null"
.
String getUri();
Serial.print("URI: ");
Serial.println(record.getUri());