25 Oct 2014

Raspberry Pi Phidgets RFID

Submitted by Max

In this tutorial, we will use an RFID tag reader writer with Raspberry.

The abbreviation RFID stands for Radio Frequency IDentification, as the name suggests is a system for the identification of objects, people or animals by means of radio frequency.

The basic system requires a reader and a tag or transponder, previously programmed to contain the information.

The tags can be supplied (Tag Active) or not (Passive Tag), in this guide we will use passive tags.

When the reader and the tag are near, the tag is activated and returns his information.

The circuit can be represented as the below picture

Raspberry Pi Phidgets RFID schema

The reader generates an electromagnetic field which induces a small current in the circuit of the tag. This current is sufficient to activate the chip contained in this tag that returns the data stored on it.

The passive tag then, is able to transmit data without having any power, so it is becoming more and more widespread its use in various fields.

Some examples of the use of RFID systems have anti-theft, animal trackers or access control.

The use of RFID technology can seem very complicated, but fortunately with the Phidgets reader is so much easier.

To demonstrate its use, we will create an application that turn on a green led when you read a tag stored in Raspberry and turn on a red led when the tag is unknown.

You’ll need:

RASPBERRY-PI - SBC, MODEL B+, 512M

or

RASPBERRY-PI - SBC, MODEL B, 512MB

1024_0 - PhidgetRFID Read-Write

some tags

Prepare Raspberry for the Phidgets or you can use our Raspberry-Pi - SBC, MODEL B+, 512M MicroSD 8GB Phidgets ready

The PhidgetRFID has 2 digital outputs, we connect two led, green to output 0 and red output 1. Connect PhidgetRFID to a usb port of Raspberry

Raspberry Pi Phidgets RFID connection

Create a new folder 

mkdir /home/pi/emmeshop

and inside a file

sudo nano /home/pi/emmeshop/pi-rfid.c

with the following content

// - RFID simple -
// This program simply displays the data that is generated by an RFID phidget in a very simple case and outputs it to the console.
// This simple example covers the basics of connecting and using an RFID phidget.
//
// Copyright 2008 Phidgets Inc.  All rights reserved.
// This work is licensed under the Creative Commons Attribution 2.5 Canada License.
// view a copy of this license, visit http://creativecommons.org/licenses/by/2.5/ca/
 
#include <stdio.h>
#include <phidget21.h> 
 
 
int CCONV AttachHandler(CPhidgetHandle RFID, void *userptr)
{
    int serialNo;
    const char *name; 
 
    CPhidget_getDeviceName (RFID, &name);
    CPhidget_getSerialNumber(RFID, &serialNo);
    printf("%s %10d attached!\n", name, serialNo); 
 
    return 0;

 
int CCONV DetachHandler(CPhidgetHandle RFID, void *userptr)
{
    int serialNo;
    const char *name; 
 
    CPhidget_getDeviceName (RFID, &name);
    CPhidget_getSerialNumber(RFID, &serialNo);
    printf("%s %10d detached!\n", name, serialNo); 
 
    return 0;

 
int CCONV ErrorHandler(CPhidgetHandle RFID, void *userptr, int ErrorCode, const char *unknown)
{
    printf("Error handled. %d - %s\n", ErrorCode, unknown);
    return 0;

 
int CCONV OutputChangeHandler(CPhidgetRFIDHandle RFID, void *usrptr, int Index, int State)
{
    if(Index == 0 || Index == 1)
    {
        //printf("Output: %d > State: %d\n", Index, State);
    }
    return 0;

 
int CCONV TagHandler(CPhidgetRFIDHandle RFID, void *usrptr, char *TagVal, CPhidgetRFID_Protocol proto)
{
    //turn on the Onboard LED
    CPhidgetRFID_setLEDOn(RFID, 1);
    if (strcmp(TagVal, "4300f9c136")==0 || strcmp(TagVal,"4d004a9f79")==0)
    {
        CPhidgetRFID_setOutputState(RFID, 0, 1);
        CPhidgetRFID_setOutputState(RFID, 1, 0);
        printf("Tag Read: %s is ok\n", TagVal);
    }
    else
    {
        CPhidgetRFID_setOutputState(RFID, 0, 0);
        CPhidgetRFID_setOutputState(RFID, 1, 1);
        printf("Tag Read: %s is unknown\n", TagVal);
    }
    
    return 0;

 
int CCONV TagLostHandler(CPhidgetRFIDHandle RFID, void *usrptr, char *TagVal, CPhidgetRFID_Protocol proto)
{
    //turn off the Onboard LED
    CPhidgetRFID_setLEDOn(RFID, 0);
    CPhidgetRFID_setOutputState(RFID, 0, 0);
    CPhidgetRFID_setOutputState(RFID, 1, 0);
    printf("Tag Lost: %s\n", TagVal);
    return 0;

 
//Display the properties of the attached phidget to the screen.  We will be displaying the name, serial number and version of the attached device.
//We will also display the nu,mber of available digital outputs
int display_properties(CPhidgetRFIDHandle phid)
{
    int serialNo, version, numOutputs, antennaOn, LEDOn;
    const char* ptr; 
 
    CPhidget_getDeviceType((CPhidgetHandle)phid, &ptr);
    CPhidget_getSerialNumber((CPhidgetHandle)phid, &serialNo);
    CPhidget_getDeviceVersion((CPhidgetHandle)phid, &version); 
 
    CPhidgetRFID_getOutputCount (phid, &numOutputs);
    CPhidgetRFID_getAntennaOn (phid, &antennaOn);
    CPhidgetRFID_getLEDOn (phid, &LEDOn); 
 
    printf("%s\n", ptr);
    printf("Serial Number: %10d\nVersion: %8d\n", serialNo, version);
    printf("# Outputs: %d\n\n", numOutputs);
    printf("Antenna Status: %d\nOnboard LED Status: %d\n", antennaOn, LEDOn); 
 
    return 0;

 
int rfid_simple()
{
    int result;
    const char *err; 
 
 
    //Declare an RFID handle
    CPhidgetRFIDHandle rfid = 0; 
 
    //create the RFID object
    CPhidgetRFID_create(&rfid); 
 
    //Set the handlers to be run when the device is plugged in or opened from software, unplugged or closed from software, or generates an error.
    CPhidget_set_OnAttach_Handler((CPhidgetHandle)rfid, AttachHandler, NULL);
    CPhidget_set_OnDetach_Handler((CPhidgetHandle)rfid, DetachHandler, NULL);
    CPhidget_set_OnError_Handler((CPhidgetHandle)rfid, ErrorHandler, NULL); 
 
    //Registers a callback that will run if an output changes.
    //Requires the handle for the Phidget, the function that will be called, and an arbitrary pointer that will be supplied to the callback function (may be NULL).
    CPhidgetRFID_set_OnOutputChange_Handler(rfid, OutputChangeHandler, NULL); 
 
    //Registers a callback that will run when a Tag is read.
    //Requires the handle for the PhidgetRFID, the function that will be called, and an arbitrary pointer that will be supplied to the callback function (may be NULL).
    CPhidgetRFID_set_OnTag2_Handler(rfid, TagHandler, NULL); 
 
    //Registers a callback that will run when a Tag is lost (removed from antenna read range).
    //Requires the handle for the PhidgetRFID, the function that will be called, and an arbitrary pointer that will be supplied to the callback function (may be NULL).
    CPhidgetRFID_set_OnTagLost2_Handler(rfid, TagLostHandler, NULL); 
 
    //open the RFID for device connections
    CPhidget_open((CPhidgetHandle)rfid, -1); 
 
    //get the program to wait for an RFID device to be attached
    printf("Waiting for RFID to be attached....");
    if((result = CPhidget_waitForAttachment((CPhidgetHandle)rfid, 10000)))
    {
        CPhidget_getErrorDescription(result, &err);
        printf("Problem waiting for attachment: %s\n", err);
        return 0;
    } 
 
    //Display the properties of the attached RFID device
    display_properties(rfid); 
 
    CPhidgetRFID_setAntennaOn(rfid, 1); 
 
    //read RFID event data
    printf("Reading.....\n"); 
 
    //keep displaying RFID event data until user input is read
    printf("Press any key to continue\n");
    getchar(); 
 
    //keep displaying RFID event data until user input is read
    printf("Press any key to continue\n");
    getchar(); 
 
    
    CPhidgetRFID_setOutputState(rfid, 0, 0);
    CPhidgetRFID_setOutputState(rfid, 1, 0); 
 
    printf("Press any key to end\n");
    getchar(); 
 
    //since user input has been read, this is a signal to terminate the program so we will close the phidget and delete the object we created
    printf("Closing...\n");
    CPhidget_close((CPhidgetHandle)rfid);
    CPhidget_delete((CPhidgetHandle)rfid); 
 
    //all done, exit
    return 0;

 
int main(int argc, char* argv[])
{
    rfid_simple();
    return 0;
}

This is a modified copy of file RFID-simple in Phidgets examples.

In the code in red the value of the tag is compared with two stored values​​, if the tag has a value corresponding to one of two, the green led turn on otherwise, turn on the red led.

When the tag is removed both led are turned off

Compile it with gcc, remembering to link it to the Phidget libraries

gcc pi-rfid.c -o pi-rfid -lphidget21

run with

sudo ./pi-rfid

Raspberry Pi Phidgets RFID wait

approaching a tag

Raspberry Pi Phidgets RFID read

In this simple example we turned on the led, but we could activate a relay to open a door or attach any other action at the correct reading of the code.

For example,we can store in a file a set of tags associated with some people and make log with date and time in which each person has opened the door.

From this example and with your imagination the possible applications are endless.

In this tutorial we only saw some of the properties of PhidgetRFID, in the next tutorials we will discover other interesting features of this board.

Follow us on social to stay informed.