Can anybody post an examplecode that shows a nonblockng read from an PN532 RFID/NFC chip?
I have the PN532 NFC/RFID controller breakout board from adafruit and need a interrupt driven callback when a rfid card has been read.
The shield uses I2C by default and according to [1] it says:
"Digital #2 is used for "interrupt" notification. This means you don't have to sit there and 'poll' the chip to ask if a target tag has been found, the pin will pull low when a card, phone, etc is within range. You can adjust which pin is used if you need to keep digital #2 for something else"
And that's exactly what I need but with Adafruit library [2] there is no example included; Just using attachInterrupt() doesn't work.
It seems like it ought to work, so if it doesn't I guess that either the conditions that trigger the interrupt have not been met, or your code to detect the interrupt is faulty.
Use of the interrupt is optional and you should be able to write a 'dumb' sketch that just polls the device so I suggest you try that to show that the thing is basically working and actually detecting things - then check that you identified the correct pin at the Arduino and that the pin is receiving the signal you think it should, and then check that the way you register your interrupt handler is correct for the expected signal.
I've spent a couple hours not finding the answer to this, so I thought I should leave this here for posterity:
#include <Wire.h> // Serial for compass and or NFC
#include <Adafruit_NFCShield_I2C.h>
#define IRQ (2)
#define RESET (3) // Not connected by default on the NFC Shield
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)
String lastNfc = "";
Adafruit_NFCShield_I2C nfc(IRQ, RESET);
void onInterrupt(){
// This is basically the adafruit print hex function
String s = "";
for(int i = 0; i < uidLength; i++){
if(uid[i] <= 0xF){
s += "0";
}
s += String(uid[i]&0xff, HEX);
}
if(s != lastNfc){ // prevent reading the same card twice in a row
Serial.println(s);
lastNfc = s;
}
}
void setup() {
Serial.begin(115200);
delay(50);
nfc.begin();
nfc.setPassiveActivationRetries(0x01);
nfc.SAMConfig();
attachInterrupt(0, onInterrupt, RISING);
}
void loop() {
nfc.readPassiveTargetID(PN532_MIFARE_ISO14443A, uid, &uidLength, 100);
}
It's very possible I've gotten some bits wrong, but this works.