![](/media/cache/img/default_profile.jpg.50x50_q85.jpg)
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
main.cpp@4:6ba6c4be9dde, 2015-05-02 (annotated)
- Committer:
- abhishekgoyanka
- Date:
- Sat May 02 19:29:57 2015 +0000
- Revision:
- 4:6ba6c4be9dde
- Parent:
- 3:ade6c26d1b90
- Child:
- 5:9f7e1200059e
2 Kam work but no LCD and no effic
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
Kojto | 0:a83db87be46c | 1 | #include "mbed.h" |
Kojto | 0:a83db87be46c | 2 | #include "SDFileSystem.h" |
abhishekgoyanka | 3:ade6c26d1b90 | 3 | #include "MFRC522.h" |
abhishekgoyanka | 3:ade6c26d1b90 | 4 | #include <stdio.h> |
abhishekgoyanka | 3:ade6c26d1b90 | 5 | #include "N5110.h" |
abhishekgoyanka | 3:ade6c26d1b90 | 6 | // FRDM-K64F (Freescale) Pin for MFRC522 reset |
abhishekgoyanka | 3:ade6c26d1b90 | 7 | #define MF_RESET PTD0 |
abhishekgoyanka | 3:ade6c26d1b90 | 8 | FILE *fp; |
abhishekgoyanka | 3:ade6c26d1b90 | 9 | |
abhishekgoyanka | 3:ade6c26d1b90 | 10 | DigitalOut LedGreen(LED2); |
abhishekgoyanka | 3:ade6c26d1b90 | 11 | |
abhishekgoyanka | 3:ade6c26d1b90 | 12 | //Serial connection to PC for output |
abhishekgoyanka | 3:ade6c26d1b90 | 13 | Serial pc(USBTX, USBRX); // serial comm on the FRDM board |
abhishekgoyanka | 3:ade6c26d1b90 | 14 | //Serial uart(PTC17, PTC16); |
abhishekgoyanka | 3:ade6c26d1b90 | 15 | |
abhishekgoyanka | 3:ade6c26d1b90 | 16 | // VCC,SCE,RST,D/C,MOSI,SCLK,LED |
abhishekgoyanka | 3:ade6c26d1b90 | 17 | //N5110 lcd(PTB2, PTC12, PTD0, PTB3, PTD6, PTD5, PTC12); |
abhishekgoyanka | 3:ade6c26d1b90 | 18 | N5110 lcd(PTB2, PTC12, PTD0, PTB3, PTD6, PTD5, PTA0); |
abhishekgoyanka | 3:ade6c26d1b90 | 19 | |
abhishekgoyanka | 3:ade6c26d1b90 | 20 | //MFRC522 RfChip (SPI_MOSI, SPI_MISO, SPI_SCK, SPI_CS, MF_RESET); |
abhishekgoyanka | 3:ade6c26d1b90 | 21 | MFRC522 RfChip (PTD2, PTD3, PTD1, PTE25, PTD0); |
Kojto | 0:a83db87be46c | 22 | |
Kojto | 0:a83db87be46c | 23 | SDFileSystem sd(PTE3, PTE1, PTE2, PTE4, "sd"); // MOSI, MISO, SCK, CS |
abhishekgoyanka | 3:ade6c26d1b90 | 24 | char buffer[1024]; //UNKNOWN foe now |
abhishekgoyanka | 3:ade6c26d1b90 | 25 | //uint8_t line[100]; |
abhishekgoyanka | 3:ade6c26d1b90 | 26 | uint8_t user[10]; |
abhishekgoyanka | 3:ade6c26d1b90 | 27 | uint8_t userlist[2][10]; |
abhishekgoyanka | 3:ade6c26d1b90 | 28 | char sam[16]="BC02CD022B02702"; |
abhishekgoyanka | 3:ade6c26d1b90 | 29 | char tempsam[16]; |
abhishekgoyanka | 3:ade6c26d1b90 | 30 | char abhishek[17]="6902BC029C021A02"; |
abhishekgoyanka | 3:ade6c26d1b90 | 31 | char abhishektemp[17]; |
Kojto | 0:a83db87be46c | 32 | |
abhishekgoyanka | 3:ade6c26d1b90 | 33 | int usercount=0; |
abhishekgoyanka | 3:ade6c26d1b90 | 34 | int found=0; |
abhishekgoyanka | 3:ade6c26d1b90 | 35 | int flag=0; //checks if the card scanner is run for the first time |
abhishekgoyanka | 3:ade6c26d1b90 | 36 | int m=0; |
abhishekgoyanka | 3:ade6c26d1b90 | 37 | |
abhishekgoyanka | 3:ade6c26d1b90 | 38 | int main() |
abhishekgoyanka | 3:ade6c26d1b90 | 39 | |
abhishekgoyanka | 3:ade6c26d1b90 | 40 | { |
Kojto | 0:a83db87be46c | 41 | pc.printf("Initializing \n"); |
abhishekgoyanka | 3:ade6c26d1b90 | 42 | //wait(2); |
abhishekgoyanka | 3:ade6c26d1b90 | 43 | |
abhishekgoyanka | 3:ade6c26d1b90 | 44 | // initialise display |
abhishekgoyanka | 3:ade6c26d1b90 | 45 | lcd.init(); |
abhishekgoyanka | 3:ade6c26d1b90 | 46 | |
abhishekgoyanka | 3:ade6c26d1b90 | 47 | // Init. RC522 Chip |
abhishekgoyanka | 3:ade6c26d1b90 | 48 | RfChip.PCD_Init(); |
Kojto | 0:a83db87be46c | 49 | |
abhishekgoyanka | 3:ade6c26d1b90 | 50 | fp = fopen("/sd/attendance.txt", "r"); |
abhishekgoyanka | 3:ade6c26d1b90 | 51 | if (fp != NULL) |
abhishekgoyanka | 3:ade6c26d1b90 | 52 | { |
Kojto | 0:a83db87be46c | 53 | fclose(fp); |
abhishekgoyanka | 3:ade6c26d1b90 | 54 | remove("/sd/attendance.txt"); |
Kojto | 0:a83db87be46c | 55 | pc.printf("Remove an existing file with the same name \n"); |
Kojto | 0:a83db87be46c | 56 | } |
abhishekgoyanka | 3:ade6c26d1b90 | 57 | |
Kojto | 0:a83db87be46c | 58 | |
abhishekgoyanka | 3:ade6c26d1b90 | 59 | while (true) |
abhishekgoyanka | 3:ade6c26d1b90 | 60 | { |
abhishekgoyanka | 3:ade6c26d1b90 | 61 | LedGreen = 1; |
abhishekgoyanka | 3:ade6c26d1b90 | 62 | |
abhishekgoyanka | 3:ade6c26d1b90 | 63 | // Look for new cards |
abhishekgoyanka | 3:ade6c26d1b90 | 64 | if ( ! RfChip.PICC_IsNewCardPresent()) |
abhishekgoyanka | 3:ade6c26d1b90 | 65 | { |
abhishekgoyanka | 3:ade6c26d1b90 | 66 | wait_ms(200); |
abhishekgoyanka | 3:ade6c26d1b90 | 67 | continue; |
abhishekgoyanka | 3:ade6c26d1b90 | 68 | } |
abhishekgoyanka | 3:ade6c26d1b90 | 69 | |
abhishekgoyanka | 3:ade6c26d1b90 | 70 | // Select one of the cards |
abhishekgoyanka | 3:ade6c26d1b90 | 71 | if ( ! RfChip.PICC_ReadCardSerial()) |
abhishekgoyanka | 3:ade6c26d1b90 | 72 | { |
abhishekgoyanka | 3:ade6c26d1b90 | 73 | wait_ms(200); |
abhishekgoyanka | 3:ade6c26d1b90 | 74 | continue; |
abhishekgoyanka | 3:ade6c26d1b90 | 75 | } |
abhishekgoyanka | 3:ade6c26d1b90 | 76 | |
abhishekgoyanka | 3:ade6c26d1b90 | 77 | LedGreen = 0; |
abhishekgoyanka | 3:ade6c26d1b90 | 78 | |
abhishekgoyanka | 3:ade6c26d1b90 | 79 | |
abhishekgoyanka | 3:ade6c26d1b90 | 80 | |
abhishekgoyanka | 3:ade6c26d1b90 | 81 | // Print Card UID |
abhishekgoyanka | 3:ade6c26d1b90 | 82 | printf("Card UID: "); |
abhishekgoyanka | 3:ade6c26d1b90 | 83 | for (uint8_t i = 0; i < RfChip.uid.size; i++) |
abhishekgoyanka | 3:ade6c26d1b90 | 84 | { |
abhishekgoyanka | 4:6ba6c4be9dde | 85 | printf("%X02", RfChip.uid.uidByte[i]); |
abhishekgoyanka | 3:ade6c26d1b90 | 86 | user[i]=RfChip.uid.uidByte[i]; |
abhishekgoyanka | 3:ade6c26d1b90 | 87 | } |
abhishekgoyanka | 4:6ba6c4be9dde | 88 | printf("\n"); |
abhishekgoyanka | 3:ade6c26d1b90 | 89 | |
abhishekgoyanka | 3:ade6c26d1b90 | 90 | if(flag) |
abhishekgoyanka | 3:ade6c26d1b90 | 91 | { found=0; |
abhishekgoyanka | 3:ade6c26d1b90 | 92 | for(uint8_t i = 0; i < usercount; i++) |
abhishekgoyanka | 3:ade6c26d1b90 | 93 | { |
abhishekgoyanka | 3:ade6c26d1b90 | 94 | for (uint8_t j = 0; j < RfChip.uid.size; j++) |
abhishekgoyanka | 3:ade6c26d1b90 | 95 | { |
abhishekgoyanka | 4:6ba6c4be9dde | 96 | if(userlist[i][j]==user[j]) |
abhishekgoyanka | 3:ade6c26d1b90 | 97 | { |
abhishekgoyanka | 3:ade6c26d1b90 | 98 | found++; |
abhishekgoyanka | 3:ade6c26d1b90 | 99 | printf("Found=%d\n",found); |
abhishekgoyanka | 3:ade6c26d1b90 | 100 | } |
abhishekgoyanka | 3:ade6c26d1b90 | 101 | //set some flag to not write name to sd card or array |
abhishekgoyanka | 3:ade6c26d1b90 | 102 | } |
abhishekgoyanka | 3:ade6c26d1b90 | 103 | } |
abhishekgoyanka | 3:ade6c26d1b90 | 104 | if(found==9) |
abhishekgoyanka | 3:ade6c26d1b90 | 105 | { |
abhishekgoyanka | 3:ade6c26d1b90 | 106 | printf("user already present"); |
abhishekgoyanka | 3:ade6c26d1b90 | 107 | |
abhishekgoyanka | 3:ade6c26d1b90 | 108 | } |
abhishekgoyanka | 3:ade6c26d1b90 | 109 | else |
abhishekgoyanka | 3:ade6c26d1b90 | 110 | { |
abhishekgoyanka | 3:ade6c26d1b90 | 111 | //userlist[i][j]==user[i]; |
abhishekgoyanka | 3:ade6c26d1b90 | 112 | for (uint8_t j = 0; j < RfChip.uid.size; j++) |
abhishekgoyanka | 3:ade6c26d1b90 | 113 | { |
abhishekgoyanka | 4:6ba6c4be9dde | 114 | userlist[usercount][j]=user[j]; |
abhishekgoyanka | 3:ade6c26d1b90 | 115 | } |
abhishekgoyanka | 4:6ba6c4be9dde | 116 | |
abhishekgoyanka | 4:6ba6c4be9dde | 117 | usercount++; |
abhishekgoyanka | 4:6ba6c4be9dde | 118 | printf("\n user added"); |
abhishekgoyanka | 3:ade6c26d1b90 | 119 | } |
abhishekgoyanka | 3:ade6c26d1b90 | 120 | } |
abhishekgoyanka | 3:ade6c26d1b90 | 121 | else |
abhishekgoyanka | 3:ade6c26d1b90 | 122 | { |
abhishekgoyanka | 4:6ba6c4be9dde | 123 | printf("inside the else\n"); |
abhishekgoyanka | 3:ade6c26d1b90 | 124 | for (uint8_t i = 0; i < RfChip.uid.size; i++) //make it size of user |
abhishekgoyanka | 3:ade6c26d1b90 | 125 | { |
abhishekgoyanka | 4:6ba6c4be9dde | 126 | userlist[0][i]=user[i]; |
abhishekgoyanka | 4:6ba6c4be9dde | 127 | printf("%i %X02 \n",i, userlist[0][i]); |
abhishekgoyanka | 3:ade6c26d1b90 | 128 | //set some flag to not write name to sd card or array |
abhishekgoyanka | 3:ade6c26d1b90 | 129 | } |
abhishekgoyanka | 4:6ba6c4be9dde | 130 | usercount++; |
abhishekgoyanka | 4:6ba6c4be9dde | 131 | printf("First user added\n"); |
abhishekgoyanka | 4:6ba6c4be9dde | 132 | flag=1; |
abhishekgoyanka | 3:ade6c26d1b90 | 133 | } |
abhishekgoyanka | 4:6ba6c4be9dde | 134 | |
abhishekgoyanka | 4:6ba6c4be9dde | 135 | |
abhishekgoyanka | 4:6ba6c4be9dde | 136 | |
abhishekgoyanka | 4:6ba6c4be9dde | 137 | |
abhishekgoyanka | 4:6ba6c4be9dde | 138 | sprintf(abhishektemp,"%X02%X02%X02%X02",user[0],user[1],user[2],user[3]); |
abhishekgoyanka | 3:ade6c26d1b90 | 139 | |
abhishekgoyanka | 4:6ba6c4be9dde | 140 | printf("Abitemp is %s",abhishektemp); |
abhishekgoyanka | 4:6ba6c4be9dde | 141 | |
abhishekgoyanka | 3:ade6c26d1b90 | 142 | for(uint8_t i = 0; i < RfChip.uid.size; i++) |
abhishekgoyanka | 3:ade6c26d1b90 | 143 | { |
abhishekgoyanka | 3:ade6c26d1b90 | 144 | abhishektemp[i]=user[i]; |
abhishekgoyanka | 3:ade6c26d1b90 | 145 | |
abhishekgoyanka | 4:6ba6c4be9dde | 146 | } |
abhishekgoyanka | 4:6ba6c4be9dde | 147 | if(strcmp(abhishektemp, abhishek)==0) |
abhishekgoyanka | 3:ade6c26d1b90 | 148 | { |
abhishekgoyanka | 3:ade6c26d1b90 | 149 | printf("\n found abhishek"); |
abhishekgoyanka | 3:ade6c26d1b90 | 150 | lcd.printString("ABHISHEK",0,0); |
abhishekgoyanka | 3:ade6c26d1b90 | 151 | continue; |
abhishekgoyanka | 3:ade6c26d1b90 | 152 | } |
abhishekgoyanka | 4:6ba6c4be9dde | 153 | else if(strcmp(abhishektemp,sam)==0) |
abhishekgoyanka | 3:ade6c26d1b90 | 154 | { |
abhishekgoyanka | 3:ade6c26d1b90 | 155 | printf("\n found sam"); |
abhishekgoyanka | 3:ade6c26d1b90 | 156 | lcd.printString("SAM",0,0); |
abhishekgoyanka | 3:ade6c26d1b90 | 157 | continue; |
abhishekgoyanka | 3:ade6c26d1b90 | 158 | } |
abhishekgoyanka | 3:ade6c26d1b90 | 159 | else |
abhishekgoyanka | 3:ade6c26d1b90 | 160 | { |
abhishekgoyanka | 3:ade6c26d1b90 | 161 | continue; |
abhishekgoyanka | 3:ade6c26d1b90 | 162 | } |
abhishekgoyanka | 3:ade6c26d1b90 | 163 | } |
abhishekgoyanka | 3:ade6c26d1b90 | 164 | |
abhishekgoyanka | 3:ade6c26d1b90 | 165 | |
abhishekgoyanka | 3:ade6c26d1b90 | 166 | // Print Card type |
abhishekgoyanka | 3:ade6c26d1b90 | 167 | uint8_t piccType = RfChip.PICC_GetType(RfChip.uid.sak); |
abhishekgoyanka | 3:ade6c26d1b90 | 168 | printf("\n PICC Type: %s \n\r", RfChip.PICC_GetTypeName(piccType)); |
abhishekgoyanka | 3:ade6c26d1b90 | 169 | wait_ms(100); |
abhishekgoyanka | 3:ade6c26d1b90 | 170 | |
abhishekgoyanka | 4:6ba6c4be9dde | 171 | |
Kojto | 0:a83db87be46c | 172 | printf("\nWriting data to the sd card \n"); |
abhishekgoyanka | 3:ade6c26d1b90 | 173 | fp = fopen("/sd/attendance.txt", "w"); |
abhishekgoyanka | 3:ade6c26d1b90 | 174 | if (fp == NULL) |
abhishekgoyanka | 3:ade6c26d1b90 | 175 | { |
Kojto | 0:a83db87be46c | 176 | pc.printf("Unable to write the file \n"); |
abhishekgoyanka | 3:ade6c26d1b90 | 177 | } |
abhishekgoyanka | 3:ade6c26d1b90 | 178 | else |
abhishekgoyanka | 3:ade6c26d1b90 | 179 | { |
abhishekgoyanka | 3:ade6c26d1b90 | 180 | fprintf(fp, "\n ATTENDANCE RECORD \n ABHISHEK PRESENT \n"); |
abhishekgoyanka | 3:ade6c26d1b90 | 181 | for(uint8_t i = 0; i < usercount; i++) |
abhishekgoyanka | 3:ade6c26d1b90 | 182 | { |
abhishekgoyanka | 3:ade6c26d1b90 | 183 | for (uint8_t j = 0; j < RfChip.uid.size; j++) |
abhishekgoyanka | 3:ade6c26d1b90 | 184 | { |
abhishekgoyanka | 3:ade6c26d1b90 | 185 | /* if(userlist[i][j]==sam[j]) |
abhishekgoyanka | 3:ade6c26d1b90 | 186 | { |
abhishekgoyanka | 3:ade6c26d1b90 | 187 | |
abhishekgoyanka | 3:ade6c26d1b90 | 188 | fprintf(fp, "\n SAM PRESENT \n"); |
abhishekgoyanka | 3:ade6c26d1b90 | 189 | |
abhishekgoyanka | 3:ade6c26d1b90 | 190 | } |
abhishekgoyanka | 3:ade6c26d1b90 | 191 | else if(userlist[i][j]==abhishek[j]) |
abhishekgoyanka | 3:ade6c26d1b90 | 192 | { |
abhishekgoyanka | 3:ade6c26d1b90 | 193 | fprintf(fp, "\n ABHISHEK PRESENT \n"); |
abhishekgoyanka | 3:ade6c26d1b90 | 194 | |
abhishekgoyanka | 3:ade6c26d1b90 | 195 | } */ |
abhishekgoyanka | 3:ade6c26d1b90 | 196 | |
abhishekgoyanka | 3:ade6c26d1b90 | 197 | // std::fputc(userlist[i][j],fp); |
abhishekgoyanka | 3:ade6c26d1b90 | 198 | fprintf(fp,"%s",userlist[i][j]); |
abhishekgoyanka | 3:ade6c26d1b90 | 199 | // uint8_t fwrite(userlist[i][j],100,sizeof(userlist[i][j]),fp); |
abhishekgoyanka | 3:ade6c26d1b90 | 200 | } |
abhishekgoyanka | 3:ade6c26d1b90 | 201 | } |
Kojto | 0:a83db87be46c | 202 | fclose(fp); |
Kojto | 0:a83db87be46c | 203 | pc.printf("File successfully written! \n"); |
Kojto | 0:a83db87be46c | 204 | } |
Kojto | 0:a83db87be46c | 205 | |
Kojto | 0:a83db87be46c | 206 | printf("\nReading data from the SD card. \n"); |
abhishekgoyanka | 3:ade6c26d1b90 | 207 | fp = fopen("/sd/attendance.txt", "r"); |
abhishekgoyanka | 3:ade6c26d1b90 | 208 | if (fp != NULL) |
abhishekgoyanka | 3:ade6c26d1b90 | 209 | { |
Kojto | 0:a83db87be46c | 210 | int size = fread(buffer, sizeof(char), 1024, fp); |
abhishekgoyanka | 3:ade6c26d1b90 | 211 | printf("Number of data read: %d, text from attendance.txt file: %s \n", size, buffer); |
Kojto | 0:a83db87be46c | 212 | fclose(fp); |
Kojto | 0:a83db87be46c | 213 | } |
Kojto | 0:a83db87be46c | 214 | } |
abhishekgoyanka | 3:ade6c26d1b90 | 215 |