PN532 - 在同一草图中与 Mifare 标签和 NFC phone 通信

PN532 - Communicate with Mifare tag and NFC phone in the same sketch

我正在尝试在同一草图中使用带有 mifare 标签的 PN532 和 android 应用程序。但问题似乎是 android phone 被 mifare 模块和 NFC 模块检测到。因此,在大约一半的情况下,它会尝试与 phone 进行通信,就好像它是一个 mifare 标签一样。仅此一项不会有问题,但在所有这些之后不再检测到 phone。

作为一个小型演示,如果我多次放置标签并在一段时间后将其移除,我会得到如下输出:

[5150] Mifare Placed
[7301] Mifare Removed
[10545] Mifare Placed
[11626] Mifare Removed

但是如果我放置和移除我的 phone,输出如下所示:

[1821] Mobile Placed
[2951] Mobile Removed
[3615] Mifare Placed
[3692] Mifare Removed

如您所见,我第二次尝试让它与 phone 通信时,mifare 模块识别它并显示错误消息。

这是我的代码:

#include <Adafruit_PN532.h>


#define PN532_IRQ   (2)
#define PN532_RESET (3)

Adafruit_PN532 nfc(PN532_IRQ, PN532_RESET);


static uint8_t lastType = 255;

#include "mifareclassic.h"
MifareClassic* mifare;
#include "nfc.h"
NFC* mobile;


void setup() {  
  Serial.begin(115200);

  nfc.begin();

  uint32_t versiondata;
  versiondata = nfc.getFirmwareVersion();
  if (! versiondata) {
    Serial.println("Error!");
  }

  nfc.setPassiveActivationRetries(3); //else, it will endlessly try to retrieve the tag
  nfc.SAMConfig(); //read tags
}


void loop() {
  if(lastType==255){
    //MIFARE PLACED?
    if(mifare->isPlaced()){
      lastType = 0;
      Serial.println("["+String(millis())+"] Mifare Placed");
    }
    //NFC PLACED?
    else if(mobile->isPlaced()){
      lastType = 1;
      Serial.println("["+String(millis())+"] Mobile Placed");
    }
  } else {
    //MIFARE REMOVED?
    if(lastType==0){
      if(!mifare->isPlaced()){
        Serial.println("["+String(millis())+"] Mifare Removed");
        lastType = 255;
      }
    }
    //NFC REMOVED?
    else if(lastType==1){
      if(!mobile->isPlaced()){
        Serial.println("["+String(millis())+"] Mobile Removed");
        lastType = 255;
      }
    }
  }  
}

main.ino

#define block 4

class MifareClassic {
  public:
    MifareClassic(){}
    bool isPlaced();
};

bool MifareClassic::isPlaced(){
  uint8_t len;
  uint8_t uid[8] = {0};
  if(nfc.readPassiveTargetID(PN532_MIFARE_ISO14443A, uid, &len)){
    return true;
  } else {
    return false;
  }
}

mifareclassic.h

#define block 4

class NFC {
  public:
    NFC(){}
    bool isPlaced();
};

bool NFC::isPlaced(){
  if(lastType == 255){
    if(nfc.inListPassiveTarget()) {
      //nfc.inListPassiveTarget();
      //authentificate via AID
      uint8_t selectApdu[] = { 0x00, /* CLA */
                                0xA4, /* INS */
                                0x04, /* P1  */
                                0x00, /* P2  */
                                0x07, /* Length of AID  */
                                0xF0, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, /* AID defined on Android App */
                                0x00  /* Le  */ };
      uint8_t uid[8];
      uint8_t len;

      if(nfc.inDataExchange(selectApdu, sizeof(selectApdu), uid, &len)) {
        return true;
      }
    }
  } else {
    uint8_t apdu[] = {0};
    uint8_t back[32];
    uint8_t len; 

    if(nfc.inDataExchange(apdu, sizeof(apdu), back, &len)) {
      return true;
    }
  }

  return false;
}

nfc.h

经过大量实验后,我找到了一个解决方案:我只是使用 inListPassiveTarget() 函数来查明是否放置了任何东西。如果是这种情况,我会通过 inDataExchange() 验证 AID 来检查放置的手机 phone,如果不起作用,则只通过 readPassiveTargetID() 检查 mifareclassic。

以防将来有人遇到同样的问题,这是我的(现在可以使用的)代码:

#include <Wire.h>
#include <SPI.h>
#include <Adafruit_PN532.h>



#define PN532_IRQ   (2)
#define PN532_RESET (3)

Adafruit_PN532 nfc(PN532_IRQ, PN532_RESET);


enum { NONE, MIFARECLASSIC, MOBILE };
int lastType = NONE;

#include "mifareclassic.h"
#include "mobile.h"


void setup() {  
  Serial.begin(115200);

  nfc.begin();

  uint32_t versiondata;
  versiondata = nfc.getFirmwareVersion();
  if (! versiondata) {
    Serial.println("Error!");
  }

  nfc.setPassiveActivationRetries(3); //else, it will endlessly try to retrieve the tag
  nfc.SAMConfig(); //read tags
}


void loop() {

  if(lastType==NONE){
    if(nfc.inListPassiveTarget()){
      //MOBILE PLACED?
      if(Mobile::isNewTagPut()){
        lastType = MOBILE;
        Serial.println("["+String(millis())+"] Mobile Placed");
      } 
      //MIFARECLASSIC PLACED?
      else if(MifareClassic::isNewTagPut()){
        lastType = MIFARECLASSIC;
        Serial.println("["+String(millis())+"] Mifare Placed");
      }
    }
  } else {
    //MOBILE REMOVED?
    if(lastType==MOBILE){
      if(!Mobile::isTagStillPlaced()){
        Serial.println("["+String(millis())+"] Mobile Removed");
        lastType = NONE;
      }
    }
    //MIFARECLASSIC REMOVED?
    else if(lastType==MIFARECLASSIC){
      if(!MifareClassic::isTagStillPlaced()){
        Serial.println("["+String(millis())+"] Mifare Removed");
        lastType = NONE;
      }
    }
  }
}

main.ino

#define block 4

class MifareClassic {
  public:
    static bool isNewTagPut();
    static bool isTagStillPlaced();
};

bool MifareClassic::isNewTagPut(){
  return isTagStillPlaced();
}

bool MifareClassic::isTagStillPlaced(){
  uint8_t len;
  uint8_t uid[8] = {0};
  return nfc.readPassiveTargetID(PN532_MIFARE_ISO14443A, uid, &len);
}

mifareclassic.h

class Mobile {
  public:
    static bool isNewTagPut();
    static bool isTagStillPlaced();
};

bool Mobile::isNewTagPut(){  
  //authentificate via AID
  uint8_t selectApdu[] = { 0x00, /* CLA */
                            0xA4, /* INS */
                            0x04, /* P1  */
                            0x00, /* P2  */
                            0x07, /* Length of AID  */
                            0xF0, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, /* AID defined on Android App */
                            0x00  /* Le  */ };
  uint8_t uid[8];
  uint8_t len;

  return nfc.inDataExchange(selectApdu, sizeof(selectApdu), uid, &len);
}

bool Mobile::isTagStillPlaced(){
  uint8_t apdu[] = {0};
  uint8_t back[32];
  uint8_t len; 

  return nfc.inDataExchange(apdu, sizeof(apdu), back, &len);
}

mobile.h