Pages: [1]   Go Down
Author Topic: Android ADK interfacing with NFC shield  (Read 841 times)
0 Members and 1 Guest are viewing this topic.
Offline Offline
Newbie
*
Karma: 0
Posts: 4
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Hi,
We are trying to integrate the PN 532 NFC shield with the Android ADK.. Both the ADK and shield are fine. I was able to load the basic sketches and read ids, etc..
Now, I am trying to make a sketch that can read the Mifare cards and send the id to the ADK and then to the Android device. I am using the following code:


Code:
//Alfatek Systems NFC Shield Read using Niels Brouwers' MicroBridge library.
//Connect a tactile switch between D12 and reset

#include <SPI.h>
#include <Adb.h>
#include <max3421e.h>
#include <PN532.h>
#include <EEPROM.h>
#define PN532_CS 10

//CS of NFC is pin 10
PN532 nfc(PN532_CS);
// Adb connection.
Connection * connection;
// Elapsed time for ADC sampling. The rate at which ADC value is sent to Android device.
long lastTime;
// ID of the MiFare Card  
uint32_t id;
uint8_t idbk[5];


// Event handler for the shell connection.
void adbEventHandler(Connection * connection, adb_eventType event, uint16_t length, uint8_t * data)
{
  // Data packet contains one byte from Android to determine when to start reading NFC
  if (event == ADB_CONNECTION_RECEIVE)
  {
  
    {
      digitalWrite(53, HIGH);
      digitalWrite(6, HIGH);
  //  ADB::closeAll();
      //NFC Begins
      {
        // look for MiFare type cards
        nfc.begin();
        //Read the NFC Shield version
        uint32_t versiondata = nfc.getFirmwareVersion();
        //Warning for non-compatible NFC shields
        if (!versiondata) {
          Serial.print("Didn't find PN53x board");
          while (1); // halt
        }
        // Got ok data, print it out!
        Serial.print("Found chip PN5");
        Serial.println((versiondata>>24) & 0xFF, HEX);
        Serial.print("Firmware ver. ");
        Serial.print((versiondata>>16) & 0xFF, DEC);
        Serial.print('.');
        Serial.println((versiondata>>8) & 0xFF, DEC);
        Serial.print("Supports ");
        Serial.println(versiondata & 0xFF, HEX);
        // configure board to read RFID tags and cards
        nfc.SAMConfig();  
        id =0;
        while (id==0)
        {
          id = nfc.readPassiveTargetID(PN532_MIFARE_ISO14443A);
          if (id != 0) {
            Serial.println();
            Serial.print("Read card #");
            Serial.println(id);
           }
        }
  //      nfc.end();
        // Store the NFC ID read to the EEPROM
        EEPROM.write(10,id>>24);
        EEPROM.write(11,id>>16);
        EEPROM.write(12,id>>8);
        EEPROM.write(13,id);
      }
     digitalWrite(6, LOW);   // Change the state of LED to indicate NFC ready to read
     delay(500);
     digitalWrite(6,HIGH);
      
  
    }
  }
}


void setup()
{
  //NFC Read Next Init
  pinMode(12, OUTPUT);
  digitalWrite(12, LOW);
  //Read the ID stored from EEPROM
  id = ((uint32_t) EEPROM.read(10)<<24) | ((uint32_t) EEPROM.read(11)<<16) | ((uint32_t) EEPROM.read(12)<<8)|((uint32_t) EEPROM.read(13));
  //Serial port debug purpose
  Serial.begin(19200);
  // Set Digital pin 6 (LED is connected) as output to indicate that NFC ready to be read.
  pinMode(6,OUTPUT);
  digitalWrite(6,LOW);
  // Note start time
  lastTime = millis();
  // Initialise the ADB subsystem.  
  ADB::init();
  // Open an ADB stream to the phone's shell. Auto-reconnect. Use any unused port number eg:4568
  connection = ADB::addConnection("tcp:4568", true, adbEventHandler);  
}

void loop()
{
  uint32_t lgval=(uint32_t)10000000000.0;
  //Display the last ID read
  if ((millis() - lastTime) > 1000)
  {

    Serial.print("Last ID Read: ");
    Serial.println(id);

    idbk[0]=id%100;
    idbk[1]=(id%10000)/100;
    idbk[2]=(id%1000000)/10000;
    idbk[3]=(id%100000000)/1000000;
    idbk[4]=(id%lgval)/100000000;

/*
    Serial.println(idbk[0]);
    Serial.println(idbk[1]);
    Serial.println(idbk[2]);
    Serial.println(idbk[3]);
    Serial.println(idbk[4]);
*/
    //Write the ID to Android
    connection->write(5,(uint8_t*)&idbk);  
    lastTime = millis();
  }
  // Poll the ADB subsystem.
  ADB::poll();
}




The logic works like this.. Use the adb event handler when it gets a response from the Android device, initialize the NFC shield, read the id, and store in EEPROM.. To send the data to the Android device, basically read the last ID stored in the EEPROM and send it to the Android device.. The code is all good - but with one problem - after the RFID tag is read (the board detects the PN532 and reads the card id), it seems to hang - i have to do a reset to make it work. Please help on how to make it work without reset..

Thanks for your help.
Alfatek
Logged

Longmont
Offline Offline
Newbie
*
Karma: 0
Posts: 28
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Hi,

I am using

 http://linksprite.com/wiki/index.php5?title=NFC_PN532_Shield  http://linksprite.com/wiki/index.php5?title=NFC_PN532_Shield

It looks like the NFC shield is using the same set of SPI pins as the USB host chip on ADK. Do you have problem here?

Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 4
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Here's my email id: sb @ alfateksystems.com
Logged

Pages: [1]   Go Up
Jump to: