NFC Shield acts as Arduino Shield via 1Sheeld

Buy 1Sheeld

NFC Shield brings you the power of NFC tags to your projects, so now with a small tag you can control your home door locks without carrying keys anymore, or  you can also control lights, physical objects or anything connected to Arduino through the NFC module in your smartphone. Learn more from the examples page.

This shield is one of the communication shields.

Communication shields allow you to use your smartphone as a gateway between Arduino and other phones, they make it easier to communicate with your Arduino board by sending and receiving data to or from another phone. Learn more from the communication shields tutorial.

NFC library brings you the power of NFC tags to your projects, so now with a small tag you can control your home door locks without carrying keys anymore, or  you can also control lights or any other physical objects easily once you get your tags near the smartphone.

Adding #define CUSTOM_SETTINGS along with #define INCLUDE_NFC_SHIELD is a key that will let you unlock the NFC shield only amongst all shields and prevent 1Sheeld library from eating your memory specially for UNO board.

New to NFC…! check this Link

 

Note:

– NFC tags cannot be read while the smartphone is in the sleep\lock mode so make sure that your smartphone is awake to get data from tags.

– NFC shield supports only NFC data exchange format “NDEF” Tags.

 

Helper Classes:

 

1- NFCTag class:

   Grabs basic info carried by the tag ex. ( tag ID, tag type, Number of records in tag, Max size of tag, Used size… etc ).

 

2- NFCRecord class:

– Queries data from records in tag and supports  three types of data (Record type category  ,Record raw Data , Record parsed data”RTD_URI & RTD_TEXT”).

 – NFC tag may contain multiple records each record is a part of the NFC memory which holds certain type and data that represents itself.

 

NFCTag & getLastTag(void)

Returns last Tag scanned.

SYNTAX

NFC.getLastTag( );

RETURNS

An object of type NFCTag which represents last tag scanned.

PARAMETERS

None.

USAGE

NFCTag &currentTag = NFC.getLastTag( );

bool isNewTagScanned(void)

Checks if a new Tag is scanned.

SYNTAX

NFC.isNewTagScanned( );

RETURNS

A Yes “1” or No “0” for a new tag scanned.

PARAMETERS

None.

USAGE

bool newTagScanned = NFC.isNewTagScanned();
if(newTagScanned)
{/*do my code*/};

void finishedReading(void)

Function invoked once finished dealing with Tag prevent losing data while scanning another tag instantly after the old Tag scanned.

Note :

Make sure to enable the Tag interrupts using enableTagInterrupts( ) before using finishedReading( ) function.

SYNTAX

NFC.finishedReading( );

RETURNS

None.

PARAMETERS

None.

USAGE

/* Call finished reading once you finished dealing with the Tag. */
NFC.finishedReading();

void enableTagInterrupts(void)

Enables Tags interruption to prevent losing data once a new tag is scanned instantly after the old Tag.

Note :

Call finishedReading function after making sure that you finished dealing with the Tag.

SYNTAX

NFC.enableTagInterrupts( );

RETURNS

None.

PARAMETERS

None.

USAGE

/* Enable Tags interrupts. */
NFC.enableTagInterrupts();

void disableTagInterrupts(void)

Disables Tags interruption.

SYNTAX

NFC.disableTagInterrupts( );

RETURNS

None.

PARAMETERS

None.

USAGE

/* Disable tag interrupts if no more needed. */
NFC.disableTagInterrupts( );

void setOnNewTagScanned(void (*userFunction)(NFCTag &))

Sets a certain individual function coded by the user in the sketch to be called and runs its functionality once a new tag is scanned.  

SYNTAX

NFC.setOnNewTagScanned(&userFunctionName);

RETURNS

None.

PARAMETERS

&userFunctionName: takes the physical address of the userFunction in the memory and jump to it once called to execute it’s functionality.

USAGE

/* Subscribe to the new tag scanned event. */
NFC.setOnNewTagScanned(&tagFunction);
/* tagFunction exists under the loop function. */
/* A function to be called once a new tag is scanned. */
void tagFunction(NFCTag &myTag)
{
  /* Create and object of type NFCRecord. */
  NFCRecord &myRecordNumber0 = myTag.getRecord(0);
  /* Subscribe to record parsed data response event. */
  myTag.setOnRecordParsedDataResponse(&responseFunction);
  /*
  /* Check if there's a record with number 0 in Tag. */
  if (!myRecordNumber0.isNull())
  {
    /* Parse and query the data in the first record. */
    myRecordNumber0.queryParsedData();
  }
  else
  {
    /* Print out no record found. */
    Terminal.println("No Record found");
  }
}

void setOnError(void (*userFunction)(byte))

Sets a certain individual function coded by the user in the sketch to be called and runs its functionality once an error occurs when tag is scanned.  

Literals:

INDEX_OUT_OF_BOUNDS

RECORD_CAN_NOT_BE_PARSED

TAG_NOT_SUPPORTED

NO_ENOUGH_BYTES

TAG_READING_ERROR

RECORD_NOT_FOUND

SYNTAX

NFC.setOnError(&userFunctionName);

RETURNS

None.

PARAMETERS

&userFunctionName: takes the physical address of the userFunction in the memory and jump to it once called to execute it’s functionality.

USAGE

/* Subscribe to setOnError. */
NFC.setOnError(&errorFunction);
/* errorFunction exists under the loop function. */
/* This function will be invoked if an error occurred in reading NFC Tag. */
void errorFunction(byte errorNumber)
{
  /* Switch on error number. */
  switch (errorNumber)
  {
    case INDEX_OUT_OF_BOUNDS: Terminal.println("INDEX_OUT_OF_BOUNDS"); break;
    case RECORD_CAN_NOT_BE_PARSED: Terminal.println("RECORD_CAN_NOT_BE_PARSED"); break;
    case TAG_NOT_SUPPORTED: Terminal.println("TAG_NOT_SUPPORTED"); break;
    case NO_ENOUGH_BYTES: Terminal.println("NO_ENOUGH_BYTES"); break;
    case TAG_READING_ERROR: Terminal.println("TAG_READING_ERROR"); break;
    case RECORD_NOT_FOUND: Terminal.println("RECORD_NOT_FOUND"); break;
  }
}

bool isEmpty(void)

Checks if the Tag has no records.

SYNTAX

NFCTag &myTag = NFC.getLastTag( );

myTag.isEmpty();

RETURNS

A Yes “1” or No “0” if there no records.

PARAMETERS

None.

USAGE

NFCTag &myTag = NFC.getLastTag();
/* Check first if Tag has records. */
if(!myTag.isEmpty())
{/* do my code*/}

byte * getId(void)

Returns back Tag unique ID.

SYNTAX

NFCTag &myTag = NFC.getLastTag( );

myTag.getId();

RETURNS

A byte array contains Tag ID.

PARAMETERS

None.

USAGE

byte * tagId;
/* Check first if Tag has records. */
if(!myTag.isEmpty())
{
  tagId = myTag.getId();
}

byte getIdLength(void)

Returns back Tag unique ID length.

SYNTAX

NFCTag &myTag = NFC.getLastTag( );

myTag.getIdLength();

RETURNS

Tag id length.

PARAMETERS

None.

USAGE

byte idlength = 0;
NFCTag &myTag = NFC.getLastTag();
/* Check first if Tag has records. */
if(!myTag.isEmpty())
{
   IdLegnth = myTag.getIdLength( ); 
}

byte getNumberOfRecords(void)

Returns number of records in Tag.

SYNTAX

NFCTag &myTag = NFC.getLastTag( );

myTag.getNumberOfRecords();

RETURNS

Tag records number.

PARAMETERS

None.

EXAMPLE

byte recordsNo = 0;
NFCTag &myTag = NFC.getLastTag();
/* Check first if Tag has records. */
if(!myTag.isEmpty())
{
   recordsNo= myTag.getNumberOfRecords();
}

NFCRecord & getRecord(int)

Returns certain record according to the given number.

Note:

Make sure that the Tag holds a record with a number you entered.

SYNTAX

NFCTag &myTag = NFC.getLastTag( );

NFCRecord &myRecord = myTag.getRecord(0);

RETURNS

A certain numbered record in Tag.

PARAMETERS

None.

USAGE

NFCTag &myTag = NFC.getLastTag();
/* Get Record number 0 in Tag.*/
NFCRecord &record0 = myTag.getRecord(0);

int getSize(void)

Returns size of memory used in Tag.

SYNTAX

NFCTag &myTag = NFC.getLastTag( );

int  usedSize = myTag.getSize();

RETURNS

An integer with size used.

PARAMETERS

None.

USAGE

int usedSize = 0;
NFCTag &myTag = NFC.getLastTag();
/* Check first if Tag has records. */
if(!myTag.isEmpty())
{
   usedSize = myTag.getSize();
}

int getMaxSize(void)

Returns maximum size of memory of Tag.

SYNTAX

NFCTag &myTag = NFC.getLastTag( );

int  maxSize = myTag.getMaxSize();

RETURNS

An integer with maximum size.

PARAMETERS

None.

USAGE

int maxSize = 0;
NFCTag &myTag = NFC.getLastTag();
/* Check first if Tag has records. */
if(!myTag.isEmpty())
{
   maxSize = myTag.getMaxSize();
}

void setOnRecordTypeResponse(void (*userFunction)(byte , byte [],byte ))

Sets a certain individual function coded by the user in the sketch to be called and runs its functionality once a record type is returned.

dependencies:

This callback works only when recordtype is queried check queryType.

SYNTAX

NFC.setOnRecordTypeResponse(&userFunctionName);

RETURNS

None.

PARAMETERS

&userFunctionName: takes the physical address of the userFunction in the memory and jump to it once called to execute it’s functionality.

USAGE

/* Subscribe to onRecordTypeResponse event. */
myTag..setOnRecordTypeResponse(&recordTypeFunction);
/* Query type from application. */ 
myTag.getRecord(0).queryType( );
/*recordTypeFunction is invoked once application responds with type.*/
void recordTypeFunction(byte recordNumber , byte type[] , byte typeLength)
{
  Terminal.println(recordNumber);
  for (int i = 0 ; i < typeLength ; i++)
  {
    Terminal.write(type[i]);
  }
}

void setOnRecordParsedDataResponse(void (*userFunction)(byte , char[]))

Sets a certain individual function coded by the user in the sketch to be called and runs its functionality once a parsed data  is returned.    

dependencies:

This callback works only when parsed data is queried check queryParsedData.

SYNTAX

NFC.setOnParsedDataResponse(&userFunctionName);

RETURNS

None.

PARAMETERS

&userFunctionName: takes the physical address of the userFunction in the memory and jump to it once called to execute it’s functionality.

USAGE

/* Subscribe to OnRecordParsedData event. */
myTag..setOnRecordParsedDataResponse(&recordParsedFunction);
/* Query parsed data from the application. */
myTag.getRecord(0).queryParsedData( );
/* recordParsedFunction will be called once parsed data sent from application. */
void recordParsedFunction(byte recordNumber ,char data)
{
   Terminal.println(recordNumber);
   Terminal.write(data);
}

void setOnRecordDataResponse(void (userFunction)(byte ,byte [],byte ))

Sets a certain individual function coded by the user in the sketch to be called and runs its functionality once raw data  is returned.

dependencies:

This callback works only when data is queried check queryData.

SYNTAX

NFC.setOnRecordDataResponse(&userFunctionName);

RETURNS

None.

PARAMETERS

&userFunctionName: takes the physical address of the userFunction in the memory and jump to it once called to execute it’s functionality.

USAGE

/* Subscribe to OnRecordDataResponse event. */
myTag..setOnRecordDataResponse(&recordDataFunction);
/* Query raw Data from the application. */
myTag.getRecord(0).queryData( );
/* recordDataFunction will be called once raw data is sent by application. */
void recordDataFunction(byte recordNumber , byte data[] , byte dataLength)
{
  Terminal.println(recordNumber);
  for (int i = 0 ; i < dataLength ; i++)
  {
    Terminal.write(data[i]);
  }

}

bool isNull(void)

Checks if tag maximum size i not zero.

SYNTAX

NFCTag &myTag =NFC.getTag( );

myTag.isNull( );

RETURNS

A Yes “1” or No “0” for tag is null.

PARAMETERS

None.

USAGE

NFCTag &myTag = NFC.getLastTag();
if(myTag.isNull)
{
   Terminal.println(“Tag has no size”);
}

byte getTypeCategory(void)

Returns back type of record.  

Categories “Literals”:

TNF_UNKNOWN :

TNF_EMPTY

TNF_EXTERNAL_TYPE

TNF_MIME_MEDIA

TNF_UNCHANGED

TNF_ABSOLUTE_URI

RTD_TEXT

RTD_URI

RTD_UNSUPPORTED

SYNTAX

NFCRecord  &myRecord0 = myTag.getRecord(0);

myRecord0.getTypeCategoryl( );

RETURNS

A certain number among several categories.

PARAMETERS

None.

USAGE

NFCTag &myTag = NFC.getLastTag();
switch(myTag.getRecord(0).getTypeCategory())
{
  case TNF_UNKNOWN : Terminal.println("TNF_UNKNOWN");break;
  case TNF_EMPTY : Terminal.println("TNF_EMPTY");break;
  case TNF_EXTERNAL_TYPE : Terminal.println("TNF_EXTERNAL_TYPE");break;
  case TNF_MIME_MEDIA : Terminal.println("TNF_MIME_MEDIA");break;
  case TNF_UNCHANGED : Terminal.println("TNF_UNCHANGED");break;
  case TNF_ABSOLUTE_URI : Terminal.println("TNF_ABSOLUTE_URI");break;
  case RTD_TEXT : Terminal.println("RTD_TEXT");break;
  case RTD_URI : Terminal.println("RTD_URI");break;
  case RTD_UNSUPPORTED : Terminal.println("RTD_UNSUPPORTED");break;
}

int getTypeLength(void)

Returns back record type length.     

SYNTAX

NFCRecord  &myRecord0 = myTag.getRecord(0);

myRecord0.getTypeLength( );

RETURNS

Length to loop on type data .

PARAMETERS

None.

USAGE

int recordTypeLength = 0;
NFCTag &myTag = NFC.getLastTag();
recordTypeLength = myTag.getRecord(0).getTypeLength()

int getDataLength(void)

Returns back record raw data length.

SYNTAX

NFCRecord  &myRecord0 = myTag.getRecord(0);

myRecord0.getdataLength( );

RETURNS

A length number.

PARAMETERS

None.

USAGE

int recordDataLength = 0;
NFCTag &myTag = NFC.getLastTag();
recordDataLength = myTag.getRecord(0).getDataLength()

void queryData(int=0 ,byte=64)

Queries data from record with it’s encapsulated information with a default start 0 to default size 64.

SYNTAX

NFCRecord  &myRecord0 = myTag.getRecord(0);

myRecord0.queryData( );

RETURNS

None.

PARAMETERS

A start by default 0 and size 64 which both can be changed unless the need of specific data from record.

USAGE

NFCRecord &myRecordNumber0 = myTag.getRecord(0);
/* Subscribe to record parsed data response event. */
myTag.setOnRecordDataResponse(&responseFunction);
/* Check if there's a record with number 0 in Tag. */
if(!myRecordNumber0.isNull())
{
  /* Parse and query the data in the first record. */
  myRecordNumber0.queryData();  
}

void queryType(int=0 ,byte=64)

Queries type of record with it’s encapsulated information with a default start 0 to default size 64.

SYNTAX

NFCRecord  &myRecord0 = myTag.getRecord(0);

myRecord0.queryType( );

RETURNS

None.

PARAMETERS

A start by default 0 and size 64 which both can be changed unless the need of specific type data from record.

USAGE

NFCRecord &myRecordNumber0 = myTag.getRecord(0);
/* Subscribe to record parsed data response event. */
myTag.setOnRecordTypeDataResponse(&responseFunction);
/* Check if there's a record with number 0 in Tag. */
if(!myRecordNumber0.isNull())
{
  /* Parse and query the data in the first record. */
  myRecordNumber0.queryTypeData();  
}

void queryParsedData(void)

Queries parsed data from record “encapsulated data is removed”.

SYNTAX

NFCRecord  &myRecord0 = myTag.getRecord(0);

myRecord0.queryParsedData( );

RETURNS

None.

PARAMETERS

None.

USAGE

/* Create and object of type NFCRecord. */
NFCRecord &myRecordNumber0 = myTag.getRecord(0);
/* Subscribe to record parsed data response event. */
myTag.setOnRecordParsedDataResponse(&responseFunction);
/* 
/* Check if there's a record with number 0 in Tag. */
if(!myRecordNumber0.isNull())
{
  /* Parse and query the data in the first record. */
  myRecordNumber0.queryParsedData();  
}

bool isNull(void)

Check if the record doesn’t exist in Tag.    

SYNTAX

NFCRecord  &myRecord0 = myTag.getRecord(0);

myRecord0.isNull( );

RETURNS

A Yes”1” or No”0” for null record.

PARAMETERS

None.

USAGE

/* Check if there's a record with number 0 in Tag. */
if(!myRecordNumber0.isNull())
{
  /* Parse and query the data in the first record. */
  myRecordNumber0.queryParsedData();  
}