A cost-effective alert system using FRDM-K64F based on RFID that can: 1. Differentiate between registered and unregistered users. 2. Detect and send alerts when cards are flashed 3. Display alerts on LCD screen 4. Log and store all the alerts in SD card (in desired file format) 5. Easily add new registered users

Dependencies:   MFRC522 NOKIA_5110 SDFileSystem mbed

main.cpp

Committer:
nivmukka
Date:
2016-04-30
Revision:
0:7a46e9e77d32

File content as of revision 0:7a46e9e77d32:

#include "mbed.h"
#include "MFRC522.h"
#include "SDFileSystem.h"
#include "NOKIA_5110.h"
#include <stdio.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

//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]; 
uint8_t user[10];
char Nivedita[17]="7A023C0293028502";
char Apoorva[17]="76029E02C302A502";
char temp[17];
int flag1=0;
int flag2=0; //checks if the card scanner is run for the first time

int main() 

{
    pc.printf("Starting Alert System 2.0 by nivmukka... \n");
    
    LcdPins myPins;
    myPins.sce  = PTC12;
    myPins.rst  = PTC4;
    myPins.dc   = PTB3;
    myPins.mosi = PTD6;
    myPins.miso = NC;
    myPins.sclk = PTD5;
    
    NokiaLcd myLcd( myPins );
    
    // Start the LCD
    myLcd.InitLcd();
    myLcd.DrawString("Alert System    by nivmukka");
    wait( 3 );
    myLcd.ShutdownLcd();
    
    // Init. RC522 Chip
    RfChip.PCD_Init();

   fp = fopen("/sd/niv_alert_log.txt", "r");
    if (fp != NULL) 
    {
        fclose(fp);
        remove("/sd/niv_alert_log.txt");
        pc.printf("Removing an existing file with the same name \n");
    }
   
while (true) 
{
    LedGreen = 1;

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

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

    LedGreen = 0;

    // Print Card UID
    printf("\n\r");
    printf("-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-READY FOR SCAN-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-\n");
    printf("\nCard 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");

    sprintf(temp,"%X02%X02%X02%X02",user[0],user[1],user[2],user[3]);
    //printf("temp is %s",temp);
    
    if(strcmp(temp, Apoorva)==0)
    { 
        printf("\nWelcome! Apoorva\n");
        if (flag2==1)
        {
            printf("Already logged in!\n");
                              
            myLcd.InitLcd();
            myLcd.DrawString("Welcome!        Apoorva");
            wait( 3 );
            myLcd.ShutdownLcd();
                
        }
        else if (flag2==0)
        {   
            printf("\nWriting data to the SD card log...\n");
            printf("Data successfully logged.\n");
            fp = fopen("/sd/niv_alert_log.txt", "a+");
            if (fp == NULL) 
            {
                pc.printf("Unable to write the file \n");
            } 
            else 
            {   
                fprintf(fp, "\nApoorva logged in. \n");
                fclose(fp); 
            }        
            flag2=1;
                
            myLcd.InitLcd();
            myLcd.DrawString("Welcome!        Apoorva");
            wait( 3 );
            myLcd.ShutdownLcd();
        }
    }        
    else if(strcmp(temp,Nivedita)==0)
    { 
        printf("\nWelcome! Nivedita\n");
        if (flag1==1)
        {
            printf("Already logged in!\n");
                          
            myLcd.InitLcd();
            myLcd.DrawString("Welcome!        Nivedita");
            wait( 3 );
            myLcd.ShutdownLcd();
                
        }
        else if (flag1==0)
        {
            printf("\nWriting data to the SD card log...\n");
            printf("Data successfully logged.\n");
            fp = fopen("/sd/niv_alert_log.txt", "a+");
            if (fp == NULL) 
            {
                pc.printf("Unable to write the file \n");
            }
            else 
            {    
                fprintf(fp, "\nNivedita logged in. \n");
                fclose(fp); 
            }                       
            flag1=1;
            //printf("flag1=%i\n",flag1);
        
            myLcd.InitLcd();
            myLcd.DrawString("Welcome!        Nivedita");
            wait( 3 );
            myLcd.ShutdownLcd();
            
        }
        
        //printf("Just after continue\n");    
    }      
    else if (strcmp(temp,Nivedita) || strcmp(temp,Apoorva) != 0)
    {
        printf("\nUnregistered User Detected!!!\n");
        
        myLcd.InitLcd();
        myLcd.DrawString("Unregistered   User! Alert!!");
        wait(3);
        myLcd.ShutdownLcd();
        
        printf("\nWriting data to the sd card log...\n");
        printf("Data successfully logged.\n");
        fp = fopen("/sd/niv_alert_log.txt", "a+");
        if (fp == NULL)
        {
            pc.printf("Unable to write the file \n");
        }
        else
        {
            fprintf(fp, "\nUnknown user found. Alert!! \n");
            fclose(fp);
        }
    }
    else
    {
        continue;
    }      
    printf("\n\r");
    printf("------------------------------------------------");
    printf("\nReading data from the SD card log... \n");
    fp = fopen("/sd/niv_alert_log.txt", "r");
    if (fp != NULL) 
    {
        int size = fread(buffer, sizeof(char), 1024, fp);
        printf("Size of data: %d, Displaying text from log file: %s\n", size, buffer);
        fclose(fp);
    } 
}
}