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();
}
}