What else can we do with NFC? We've laid the groundwork for keycard access systems, initiated purchases on amazon, how about realtime configuration changes?
In this write up we'll learn how to use NDEF tag formatting to store text records containing configuration information. In this case, colors! Picture a light switch that scans RFID tags, and sets the lights in the room to the color tag you just tapped! To demonstrate this we'll display some text on the LCD screen, each time you scan an encoded tag, the color will change! If the encoding isn't recognized, anduino chooses a random color.
Getting StartedTo start with this demo you'll need an Arduino Zero or Due paired with an anduinoWiFi shield, and some RFID tags. We chose to use these Mifare Classic RFID keycards although any ISO 14443-A NTAG-203 compatible tags should work!
If you haven't yet used NFC on the anduinoWiFi swing by our getting started guide to make sure everything is setup and working properly. Much like we do in the guide, let's run the File>>examples>>AnduinoNFC>>readRFIDTag to grab our tags UIDs.
NDEF it!We're going to use the NDEF format to encode our RFID tags with a color so the first order of business will be navigating to File>>examples>>AnduinoNFC>>formateToNdef and formatting any RFID tags you'd like to use.
Flash the sketch, once the serial terminal displays:
Mifare Classic/Ultralight --> NDEF
Found chip PN532
Firmware ver. 1.6
Place an unformatted Mifare Classic tag on the reader.
You're ready to format your tags!
Color Encode TagsOnce you've prepared the tag to receive NDEF formatted data you're ready to save some configuration parameters! If you take a peek at the nfcColorSelect sketch and scroll all the way to the bottom you'll find the colorPicker function.
/* Returns the 16-bit val for some pre-set
* color definitions or a random number
#define ST7735_BLACK 0x0000
#define ST7735_BLUE 0x001F
#define ST7735_RED 0xF800
#define ST7735_GREEN 0x07E0
#define ST7735_CYAN 0x07FF
#define ST7735_MAGENTA 0xF81F
#define ST7735_YELLOW 0xFFE0
#define ST7735_WHITE 0xFFFF
*/
uint16_t colorPicker(String rfidColor)
{
if(rfidColor == "white")
{
return ST7735_WHITE;
}
else if(rfidColor == "red")
{
return ST7735_RED;
}
else if(rfidColor == "blue")
{
return ST7735_BLUE;
}
else if(rfidColor == "black")
{
return ST7735_BLACK;
}
else if(rfidColor == "green")
{
return ST7735_GREEN;
}
else if(rfidColor == "cyan")
{
return ST7735_CYAN;
}
else if(rfidColor == "magenta")
{
return ST7735_MAGENTA;
}
else if(rfidColor == "yellow")
{
return ST7735_YELLOW;
}
else
{
return random(65535);
}
}
Any of these rfidColor types are valid to burn onto your RFID Tag. If you happen to misspell or encode an unavailable color a random color will be chosen. Open up File>>Exampes>>AnduinoNFC>>writeNDEFRecord and let's burn one of the available colors to an NDEF formatted RFID tag.
Before you run the sketch you'll want to edit this line of code:
message.addTextRecord("I'm a generic text record in english");
And change the text to whichever color you'd like the card to represent.
message.addTextRecord("yellow");
Be sure to comment out any other lines that might be adding additional NDEF records as we only want this one and count on it residing in record[0].
Run the sketch, scan your RFID tag and ensure your tag contains data similar to this...
NFC Tag - Mifare Classic
UID BA 61 20 00
NDEF Message 1 record, 13 bytes
NDEF Record
TNF 0x1 Well Known
Type Length 0x1 1
Payload Length 0x9 9
Type 54 T
Payload 02 65 6E 79 65 6C 6C 6F 77 .enyellow
Record is 13 bytes
*Don't worry about the .en appended to your color, that's just depicting the mime/media language encoding of our text (the default is en - english). We strip the encoding here prior to parsing the color.
/* Change the text display color sent over in the text record
* w/ 16-bit default color values
*/
//strip the encoding
for(int i=3; i<payloadLength; i++)
{
temp[i-3] = payloadBytes[i];
}
//cast to string
rfidColor = (const char*)temp;
The colors Duke, the colors!It works!
sEach time you scan a different RFID tag the colors will change! Spice things up and give this a go with an RGB led or an NeoPixel strip! Be sure to post your write ups to the anduino project hub and tell us all about how you're using NFC to configure you're environment in realtime!
What's next?Still yearning for some more NFC projects? Follow our project hub to stay in the loop on new projects and creative ideas on how to utilize NFC and other IoT tech in your projects!
Comments