DFRobot NFC chip - UART communication

I am trying to set up my Spark core to communicate properly with a DFRobot brand NFC module. This module uses the common PN532 NFC chip and interfaces via UART.
Module found here
The module comes with a sample arduino script that I have been unsuccessful in porting to the spark core.
Arduino example code
The first problem that I am having is that I am not getting back the correct return from the module after I send the wake up command over UART. The example code indicates I should be receiving “00 00 FF 00 FF 00 00 00 FF 02 FE D5 15 16 00” as a reply, but I never get that. I instead get a seemingly random reply that differs every time the code is run.

I was hoping someone has developed a uart nfc library for the PN532 chip that might work for me on the spark with this particular module, but I haven’t had any luck finding such a library.

My code is as follows, any help will be greatly appreciated!

const unsigned char wake[24]={
  0x55, 0x55, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, \
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xff, 0x03, 0xfd, 0xd4, 0x14, 0x01, 0x17, 0x00};//wake up NFC module
const unsigned char firmware[9]={
  0x00, 0x00, 0xFF, 0x02, 0xFE, 0xD4, 0x02, 0x2A, 0x00};//
const unsigned char tag[11]={
  0x00, 0x00, 0xFF, 0x04, 0xFC, 0xD4, 0x4A, 0x01, 0x00, 0xE1, 0x00};//detecting tag command
const unsigned char std_nfc[25] = {
  0x00, 0x00, 0xFF, 0x00, 0xFF, 0x00, 0x00, 0x00, 0xFF, 0x0C, \
0xF4, 0xD5, 0x4B, 0x01, 0x01, 0x00, 0x04, 0x08, 0x04, 0x00, 0x00, 0x00, 0x00, 0x4b, 0x00};

unsigned char old_id[5];
unsigned char receive_nfc[25];//Command receiving buffer

void setup()
{
    pinMode(D7, OUTPUT);
    
    Serial.begin(9600);
    
    while(!Serial.available()){
        digitalWrite(D7, HIGH);
        kindDelay(150);
    }
    digitalWrite(D7, LOW);
    
    Serial.println("Welcome to the NFC Prototype!");
    Serial.print("Waiting for WiFi... ");
    
    while(!WiFi.ready()){
        digitalWrite(D7, HIGH);
        delay(150);
        digitalWrite(D7, LOW);
        kindDelay(150);
    }
    
    Serial.println("done!");
    Serial.print("Opening comunication with NFC... ");
    
    Serial1.begin(115200);
    
    Serial.println("done!");
    Serial.print("Waking NFC card... ");
    
    wake_nfc();
    delay(100);
    
    Serial.println("done!");
    Serial.print("Reading from NFC card... ");
    
    read_nfc(15);
    delay(100);
    
    Serial.println("done!");
    Serial.print("NFC card data: ");
    
    display(15);
    
    Serial.print("Waiting for nfc data... ");
}

void loop() 
{
    send_tag();
    read_nfc(25);
    delay(100);
    
    if (!cmp_id()){
        if(test_nfc()){
            Serial.println("done!");
            Serial.print("NFC tag data: ");
            display(25);
            delay(100);
            Serial.print("Waiting for nfc data... ");
        }
    }
    copy_id();
}

void wake_nfc(void)
{
    unsigned char i;
    for(i=0; i<24; i++){
        Serial1.write(wake[i]);
        Serial1.flush();
    }
}

void read_nfc(unsigned int size)
{
    unsigned int i;
    for(i=0;i<size;i++){
        receive_nfc[i] = Serial1.read();
    }
}

void display(unsigned int size)
{
    unsigned int i;
    for(i=0;i<size;i++){
        Serial.print(receive_nfc[i], HEX);
        Serial.print(" ");
    }
    Serial.println();
}

void send_tag(void)
{
    unsigned char i;
    for(i=0;i<11;i++){
        Serial1.write(tag[i]);
        Serial1.flush();
    }
}

char cmp_id (void)
{
    int ai, oi;
    for(oi=0,ai=19; oi<5; oi++,ai++){
        if(old_id[oi] != receive_nfc[ai]){
            return 0;
        }
    }
    return 1;
}

int test_nfc(void)
{
    int i;
    for(i=0; i<19; i++){
        if(receive_nfc[i] != std_nfc[i]){
            return 0;
        }
    }
    
    return 1;
}

void copy_id(void)
{
    int ai, oi;
    for (oi=0,ai=19; oi<5; oi++,ai++){
        old_id[oi] = receive_nfc[ai];
    }
}

void kindDelay(unsigned long fullDelay)
{
    if(fullDelay > 100){
        long leftovers = fullDelay % 100;
        long loops = (fullDelay - leftovers) / 100;
        
        for(long i = 0; i<loops; i++){
            delay(100);
            SPARK_WLAN_Loop();
        }
        delay(leftovers);
    }else{
        delay(fullDelay);
        SPARK_WLAN_Loop();
    }
}

Just to be sure, how did you wire up the board to the spark core? :wink:

1 Like

I can’t take a photo of the actual setup until I get home from work later tonight, but this is how I have it setup:

I will post the photo of the actual setup later tonight for you guys to verify.

Looks like it’s flipped. :wink:

Core Tx —> Shield Rx

Core Rx --> Shield Tx :wink:

1 Like

@dcrawford, I would power it off the 3.3v line instead of the filtered line since the board has it own regulator. Also, I believe you need to switch the RX and TX lines on the Core so:

Core    PN532
TX       RX
RX       TX

Give that a shot and see how it works :smile:

LOL!!! @kennethlimcp beat me to it!!! :stuck_out_tongue:

@kennethlimcp, you rock dude!

2 Likes

Wow… can you tell I’m new to this lol :stuck_out_tongue:

If that’s all the problem is, that’s such a stupid mistake…
Just sucks I had to waist a night of trying to debug my script to learn that lesson…

But thank you very much for such a quick response!

Wow, its really awesome when a product like the Spark core has such fantastic people supporting it!
Keep up the good work guys!

2 Likes

I’ve tried with same NFC module from DFRobot. But in this project I tried with raspberry pi 3 and wiringpi library from Gordon project to Acces GPIO and UART.
But terminal still blank, can’t show the result.

My code is as follows, Big thanks for who can helps me…

#define wake_result 15
#define firmware_result 19
#define tag_result 25

#include <stdio.h>
#include <string.h>
#include <errno.h>

#include <wiringPi.h>
#include <wiringSerial.h>

const unsigned char wake[24]={
  0x55, 0x55, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xff, 0x03, 0xfd, 0xd4, 0x14, 0x01, 0x17, 0x00};//wake up NFC module
const unsigned char firmware[9]={
  0x00, 0x00, 0xFF, 0x02, 0xFE, 0xD4, 0x02, 0x2A, 0x00};//
const unsigned char tag[11]={
  0x00, 0x00, 0xFF, 0x04, 0xFC, 0xD4, 0x4A, 0x01, 0x00, 0xE1, 0x00};//detecting tag command
const unsigned char std_ACK[25] = {
  0x00, 0x00, 0xFF, 0x00, 0xFF, 0x00, 0x00, 0x00, 0xFF, 0x0C, 0xF4, 0xD5, 0x4B, 0x01, 0x01, 0x00, 0x04, 0x08, 0x04, 0x00, 0x00, 0x00, 0x00, 0x4b, 0x00};
unsigned char old_id[5];
unsigned char cur_id[5];
int fd;

unsigned char receive_ACK[25];//Command receiving buffer

void display(unsigned char tem);
void read_ACK(unsigned char temp);
void read_id (void);
void print_id();
void UART_Send_Byte(unsigned char command_data);
void firmware_version(void);
void wake_card(void);
void copy_id (void);
void send_tag (void);

char no_detection(void);
char same_id (void);


void setup(void)
{
	wiringPiSetup () ;
	printf ("bismillah");
    fd = serialOpen ("/dev/ttyS0", 115200);
  
  //delay(100);
  printf("wake card");
  delay(100);  // give delay before sending command bytes
  wake_card();
  delay(100);
  read_ACK(wake_result);
  delay(100);
  display(wake_result);

  /*firmware_version();
  delay(100);
  read_ACK(firmware_result);
  delay(100);
  display(firmware_result);*/
}


// scan NFC tag every second
int main(void)
{
  printf("TESSSS");
  setup();
  while(1){
  send_tag();
  read_ACK(tag_result);
  read_id();
  if (!no_detection()) {
    print_id();
  }
  delay(1000);}
  return 0;
}

void send_tag(void)
{//send tag[] to device
  unsigned char i;
  for(i=0;i<11;i++) //send command
    UART_Send_Byte(tag[i]);
}

void display(unsigned char tem)
{
	int i;
  for (i=0;i<tem;i++)
  {
    if (receive_ACK[i] < 16) // to make 2 digits
      printf("0");
      printf("%X",receive_ACK[i]);
    if (i<(tem-1)) printf(" ");
  }
 
}

void read_ACK(unsigned char temp)
{//read ACK into reveive_ACK[]
  //Serial.println("read the result");
 // printf("3");
  unsigned char i;
  for(i=0;i<temp;i++) {
    receive_ACK[i]= read(fd);
    delay(100);
  }
}

// read tag id from tag values
void read_id (void) {
	//printf("5");
  int ai, ci;
  for (ci=0, ai=19; ci<5; ci++,ai++) {
    cur_id[ci] = receive_ACK[ai];
  }
}

char no_detection(void) {
  int i;
  //printf("6");
  for (i=0 ; i<sizeof(cur_id) ; i++) {
    if (cur_id[i] != 255)  // FF
      return 0;
  }
  return 1;
}


void print_id() {
	int i;
	//printf("7");
  for (i=0 ; i<sizeof(cur_id) ; i++) {
    if (cur_id[i] < 16) // to make 2 digits
      printf("0");
    printf("%X",cur_id[i]);
    if (i<(sizeof(cur_id)-1)) printf(" ");
  }
 
}


void UART_Send_Byte(unsigned char command_data)
{//send byte to device
	//printf("8");
  char buf[10];
  sprintf(buf,"%X",command_data);
  write(fd,buf);
  serialFlush(fd);// complete the transmission of outgoing serial data
}

void firmware_version(void)
{//send firmware[] to device
  unsigned char i;
  //printf("9");
  for(i=0;i<9;i++) //send command
    UART_Send_Byte(firmware[i]);
}

void wake_card(void)
{//send wake[] to device
  unsigned char i;
  //printf("10");
  for(i=0;i<24;i++) //send command
    UART_Send_Byte(wake[i]);
}



void copy_id (void)
{//save old id
  int i;
  for (i=0 ; i<5 ; i++) {
    old_id[i] = cur_id[i];
  }
}



//return true if find id is old
char same_id (void)
{
  int ai, oi;
  for (oi=0,ai=19; oi<5; oi++,ai++) {
    if (old_id[oi] != receive_ACK[ai])
      return 0;
  }
  return 1;
}

// true if tag is FF FF FF FF FF