This is a RFID based attendance system. The current program is only for two users but can be easily scaled for more users. Apart from FRDM-K64F board, we have MFRC522, Nokia 5110 LCD and SD card as external hardware. Each time a legitimate card is flashed the name of the person will be displayed on the LCD and his information will be stored in SD card if it's already not there. I have used existing libraries for all the harware. Here is the list. MFRC522 - By Martin Olejar SDFileSystem - By Andrew Lindsay NOKIA_5110 - By Chris Yan.

Dependencies:   MFRC522 NOKIA_5110 SDFileSystem mbed

Fork of FTF2014_lab4 by Freescale

main.cpp

Committer:
abhishekgoyanka
Date:
2015-05-02
Revision:
4:6ba6c4be9dde
Parent:
3:ade6c26d1b90
Child:
5:9f7e1200059e

File content as of revision 4:6ba6c4be9dde:

#include "mbed.h"
#include "SDFileSystem.h"
#include "MFRC522.h"
#include <stdio.h>
#include "N5110.h"
// FRDM-K64F (Freescale) Pin for MFRC522 reset
#define MF_RESET    PTD0
FILE *fp;

DigitalOut LedGreen(LED2);

//Serial connection to PC for output
Serial pc(USBTX, USBRX); // serial comm on the FRDM board
//Serial uart(PTC17, PTC16);

//    VCC,SCE,RST,D/C,MOSI,SCLK,LED
//N5110 lcd(PTB2, PTC12, PTD0, PTB3, PTD6, PTD5, PTC12);
N5110 lcd(PTB2, PTC12, PTD0, PTB3, PTD6, PTD5, PTA0);

//MFRC522    RfChip   (SPI_MOSI, SPI_MISO, SPI_SCK, SPI_CS, MF_RESET);
MFRC522    RfChip   (PTD2, PTD3, PTD1, PTE25, PTD0);

SDFileSystem sd(PTE3, PTE1, PTE2, PTE4, "sd"); // MOSI, MISO, SCK, CS
char buffer[1024];   //UNKNOWN foe now
//uint8_t line[100];
uint8_t user[10];
uint8_t userlist[2][10];
char sam[16]="BC02CD022B02702";
char tempsam[16];
char abhishek[17]="6902BC029C021A02";
char abhishektemp[17];

int usercount=0;
int found=0;
int flag=0; //checks if the card scanner is run for the first time
int m=0;

int main() 

{
    pc.printf("Initializing \n");
    //wait(2);
    
     // initialise display 
    lcd.init();
    
    // Init. RC522 Chip
    RfChip.PCD_Init();

   fp = fopen("/sd/attendance.txt", "r");
    if (fp != NULL) 
    {
        fclose(fp);
        remove("/sd/attendance.txt");
        pc.printf("Remove an existing file with the same name \n");
    }
   

while (true) 
{
    LedGreen = 1;

    // Look for new cards
    if ( ! RfChip.PICC_IsNewCardPresent())
    {
      wait_ms(200);
      continue;
    }

    // Select one of the cards
    if ( ! RfChip.PICC_ReadCardSerial())
    {
      wait_ms(200);
      continue;
    }

    LedGreen = 0;


    
    // Print Card UID
    printf("Card UID: ");
    for (uint8_t i = 0; i < RfChip.uid.size; i++)
    {
      printf("%X02", RfChip.uid.uidByte[i]);
     user[i]=RfChip.uid.uidByte[i];
    }
    printf("\n");
    
    if(flag)
    {   found=0;
        for(uint8_t i = 0; i < usercount; i++)
        {
            for (uint8_t j = 0; j < RfChip.uid.size; j++)
            {
               if(userlist[i][j]==user[j])
               {
               found++;
               printf("Found=%d\n",found);
               }
              //set some flag to not write name to sd card or array
            }
        }
        if(found==9)
        {
                       printf("user already present");

        }
        else
        {
               //userlist[i][j]==user[i];
               for (uint8_t j = 0; j < RfChip.uid.size; j++)
                {
                userlist[usercount][j]=user[j];
                }
                
            usercount++;        
            printf("\n user added");
        }
        }
    else
    {
        printf("inside the else\n");
    for (uint8_t i = 0; i < RfChip.uid.size; i++)     //make it size of user
            {
              userlist[0][i]=user[i];
              printf("%i %X02 \n",i, userlist[0][i]);
              //set some flag to not write name to sd card or array
            }
            usercount++;
         printf("First user added\n");
         flag=1;
    }
    
    
    
    
    sprintf(abhishektemp,"%X02%X02%X02%X02",user[0],user[1],user[2],user[3]);

    printf("Abitemp is %s",abhishektemp);
    
    for(uint8_t i = 0; i < RfChip.uid.size; i++)
    {
        abhishektemp[i]=user[i];
        
      }   
if(strcmp(abhishektemp, abhishek)==0)
{ 
 printf("\n found abhishek");
lcd.printString("ABHISHEK",0,0);
continue;
}
else if(strcmp(abhishektemp,sam)==0)
{ 
printf("\n found sam");
lcd.printString("SAM",0,0);
continue;
}
else
{
    continue;
}
}

        
       // Print Card type
    uint8_t piccType = RfChip.PICC_GetType(RfChip.uid.sak);
    printf("\n PICC Type: %s \n\r", RfChip.PICC_GetTypeName(piccType));
    wait_ms(100);
    
    
    printf("\nWriting data to the sd card \n");
    fp = fopen("/sd/attendance.txt", "w");
    if (fp == NULL) 
    {
        pc.printf("Unable to write the file \n");
    } 
    else 
    {
        fprintf(fp, "\n ATTENDANCE RECORD \n ABHISHEK PRESENT \n");
        for(uint8_t i = 0; i < usercount; i++)
        {
            for (uint8_t j = 0; j < RfChip.uid.size; j++)
            {
            /* if(userlist[i][j]==sam[j])
               {
            
            fprintf(fp, "\n SAM PRESENT \n");

               }
               else if(userlist[i][j]==abhishek[j])
               {
                   fprintf(fp, "\n ABHISHEK PRESENT \n");

               } */
                    
              //  std::fputc(userlist[i][j],fp);
            fprintf(fp,"%s",userlist[i][j]);
        // uint8_t fwrite(userlist[i][j],100,sizeof(userlist[i][j]),fp);
            }
         }
        fclose(fp);
        pc.printf("File successfully written! \n");
    }

    printf("\nReading data from the SD card. \n");
    fp = fopen("/sd/attendance.txt", "r");
    if (fp != NULL) 
    {
        int size = fread(buffer, sizeof(char), 1024, fp);
        printf("Number of data read: %d, text from attendance.txt file: %s \n", size, buffer);
        fclose(fp);
    }
}