Nivedita Mukka / Mbed 2 deprecated Personal Alert System using RFID with FRDM K64F Featured

Dependencies:   MFRC522 mbed

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers main.cpp Source File

main.cpp

00001 #include "mbed.h"
00002 #include "MFRC522.h"
00003 
00004 // FRDM-K64F (Freescale) Pin for MFRC522 reset
00005 #define MF_RESET    PTD0
00006 
00007 // Defining the serial ports for PC and Bluetooth
00008 #define SERIAL_1  
00009 Serial pc(USBTX, USBRX);
00010 #ifdef SERIAL_1
00011 Serial blue(PTC15,PTC14);         
00012 #endif
00013 #ifdef SERIAL_2
00014 Serial blue(PTC17,PTC16);         // TX = P14  RX = P13
00015 #endif
00016 
00017 DigitalOut LedGreen(LED2), LedRed(LED1);
00018 
00019 Timer timeBetweenCards;
00020 char tagname;
00021 char * cardtypetest;
00022 char cardid;
00023 
00024 //MFRC522    RfChip   (SPI_MOSI, SPI_MISO, SPI_SCK, SPI_CS, MF_RESET);
00025 MFRC522    RfChip   (PTD2, PTD3, PTD1, PTE25, PTD0);
00026 
00027 int main(void) {
00028   
00029   blue.baud(9600);
00030   pc.baud(9600);
00031   LedRed= 0;
00032   pc.printf("RFID based Alert System Started\r\n");
00033   printf("-------------------------------------------------------------\r\n");
00034 
00035   // Init. RC522 Chip
00036   RfChip.PCD_Init();
00037 
00038   while (true) {
00039 
00040     tagname = '\0';
00041     LedRed= 1;
00042     LedGreen = 1;
00043 
00044     // Look for new cards
00045     if ( ! RfChip.PICC_IsNewCardPresent())
00046     {
00047       wait_ms(100);
00048       continue;
00049     }
00050 
00051     // Select one of the cards
00052     if ( ! RfChip.PICC_ReadCardSerial())
00053     {
00054       wait_ms(100);
00055       
00056       continue;
00057       
00058     }
00059     
00060     // When a card is detected, a Green Led flashes!
00061     blue.printf("%f seconds \n", timeBetweenCards.read());
00062     timeBetweenCards.start();
00063     LedGreen = 0; 
00064     
00065     for (uint8_t i = 0; i < RfChip.uid.size; i++)
00066     {
00067   //    printf(" %X02", RfChip.uid.uidByte[i]);
00068         cardid= RfChip.uid.uidByte[i];
00069         blue.printf(" %X02 ", (char *) cardid);
00070         tagname= RfChip.uid.uidByte[i]+ tagname;
00071         blue.putc(tagname);
00072      }
00073     printf("\n\r");
00074     if (tagname == 206) {
00075         
00076         printf ("Blue Keychain detected: Welcome, Nivedita!");
00077         printf("\n\r");
00078         printf("------------------------------------------------");
00079         
00080         }
00081     else if (tagname == 124) {
00082         printf ("White Card detected: Welcome, Apoorva!");
00083         printf("\n\r");
00084         printf("------------------------------------------------");        
00085         }
00086     else if (tagname == 76) {
00087         printf ("Unknown person (White card): sending alert!! ");
00088         printf("\n\r");
00089         printf("------------------------------------------------");        
00090         }
00091       else if (tagname == 58) {
00092         printf ("Unknown person (Blue keychain): sending alert!! ");
00093         printf("\n\r");
00094         printf("------------------------------------------------");        
00095         }
00096         else {
00097         printf("--------------------!!!!!!!!!---------------------\n\r");    
00098         printf("Unknown card/keychain detected; sending alert!!!");
00099         printf("\n\r");
00100         printf("------------------------------------------------");        
00101         }
00102                
00103     // Print Card UID
00104     printf("\n\r");
00105     printf("Card UID: ");
00106     
00107    for (uint8_t i = 0; i < RfChip.uid.size; i++)
00108     {
00109         printf(" %X02", RfChip.uid.uidByte[i]);
00110         cardid= RfChip.uid.uidByte[i];
00111         blue.printf(" %X02 ", (char *) cardid);
00112         tagname= RfChip.uid.uidByte[i]+ tagname;
00113         blue.putc(tagname);
00114     }
00115     printf("\n\r");
00116               
00117     printf("Decimal tagname:  %d \n\r", tagname);   
00118     uint8_t piccType = RfChip.PICC_GetType(RfChip.uid.sak);
00119     printf("PICC Type: %s \n\r", RfChip.PICC_GetTypeName(piccType));
00120     cardtypetest= RfChip.PICC_GetTypeName(piccType);
00121     blue.printf("PICC Type: %s \n\r",(char *) cardtypetest);
00122     printf("________________________________________________");
00123     //wait_ms(100);
00124   }
00125 }