RFID Reader and Image Display

This program reads ID Innovations ID-12 RFID readers and matches the tags against a list of known tags. It’s an illustration of how to use an RFID reader to associate data with a set of tags.

  RFID Reader and image display
  Language: Processing
  This program reads ID Innovations ID-12 RFID readers rom two serial ports.
  It matches the tags against a list of known tags, and uses the match to 
  associate a name with the tag.  It also scans the sketch's data directory
  to load an image associated with the name.
  Created 12 Mar 2008
  by Tom Igoe and the interaction design class at AHO, Spring 2008
  directory reading and image scanning based on an example from Marius Watts


import processing.serial.*;

Serial portOne;    // the first serial port
Serial portTwo;    // the second serial port

boolean showImage;  // whether or not to show the image
// the list of names
String[] tags = {

// the list of people
String[] people = {
  "Christer", "Marianne", "Martin","Bo", "Gunnar","Knut","Fan_fan","Kyrre","Silje",
PImage thisImage;

void setup() {
  size(640, 480);
  showImage = false;
  // list the serial ports
  // open the serial ports:
  portOne = new Serial(this, Serial.list()[0], 9600);
  portTwo = new Serial(this, Serial.list()[2], 9600);
  // set both ports to buffer information until you get 0x03:

void draw() {
  // if there's an image to show, show it:
  if (showImage) {
  image(thisImage, 0, 0);

void serialEvent(Serial thisPort) {
  // read the incoming serial data:
  String inString = thisPort.readStringUntil(0x03);

  // if the string is not empty, do stuff with it:
  if (inString != null) {
    // if the string came from serial port one:
    if (thisPort == portOne) {
      print ("Data from port one: ");
    // if the string came from serial port two:
    if (thisPort == portTwo) {
      print ("Data from port two: ");
    // print the string:

    // the tag ID is only bytes 1 through 13. Get it:
    String payload = inString.substring(1, 13);
    // match the tag against the list of tags:

  This method matches the RFID tag number to a tag in the tagList.  
  Then it takes the corresponding name from the people list, and 
  gets the image from the data directory.
void matchTag(String thisTag) {
  // iterate over the list of all known tags:
  for (int whichTag = 0; whichTag < tags.length; whichTag++) {
    // if the tag you got matches this tag in the list:
    if (thisTag.equals(tags[whichTag])) {
      // get the name from the people list
      // that corresponds to this tag's position
      String thisName = people[whichTag];
      // print it:
      println("Here's " + thisName);
      // get the image and display it:

// Get random image from the folder filelist array
void getImage(String personName) {
  String filenames[];

    // open the directory:
    File directory = new File(sketchPath("data"));
    // list the files in the directory:
    filenames = directory.list();

    for(int thisFile = 0; thisFile<filenames.length; thisFile++){
      // if the file is a .jpg:
      if (filenames[thisFile].endsWith(".jpg")) {
        // get the filename without the .jpg extension:
        String thisName = filenames[thisFile].substring(0, filenames[thisFile].length() -4);
        //if the filename matches the name of the person:
        if( thisName.equals(personName)) {
          // load the image from the file:
          thisImage = loadImage("data/"+filenames[thisFile]);
          // show it:
          showImage = true;
  catch(Exception f){
    //  if there was a problem reading the directory:
    println("Image import error - missing thisFile or folder");