Reading a RFID tag (13.56MHz) and sending its data through bluetooth (HC-05 module) to an Android cellphone (using the BlueTerm APP).

Dependencies:   TextLCD beep mbed

Fork of RFID-RC522 by Thomas Kirchner

Committer:
Clovis
Date:
Tue Sep 23 07:08:59 2014 +0000
Revision:
3:bf44d10e4904
Parent:
2:a0c7513fb634
initial release: reads a RFID tag (13.56Mhz) and sends the data of the tag to a cellphone (through HC-05 bluetooth module). Tested for the FRDM-K64F (Freescale) platform.

Who changed what in which revision?

UserRevisionLine numberNew contents of line
kirchnet 2:a0c7513fb634 1 //Test of cheap 13.56 Mhz RFID-RC522 module from eBay
Clovis 3:bf44d10e4904 2 //This code is based on Martin Olejar's and Thomas Kirchner's MFRC522 libraries. Minimal changes
Clovis 3:bf44d10e4904 3 //Adapted for FRDM-K64F from Freescale, in 07/21/2014 by Clovis Fritzen.
kirchnet 2:a0c7513fb634 4
kirchnet 2:a0c7513fb634 5 #include "mbed.h"
kirchnet 2:a0c7513fb634 6 #include "MFRC522.h"
Clovis 3:bf44d10e4904 7 #include "beep.h"
Clovis 3:bf44d10e4904 8 #include "TextLCD.h"
kirchnet 2:a0c7513fb634 9
Clovis 3:bf44d10e4904 10 // FRDM-K64F (Freescale) Pin for MFRC522 reset
Clovis 3:bf44d10e4904 11 #define MF_RESET PTD0
kirchnet 2:a0c7513fb634 12
Clovis 3:bf44d10e4904 13 // Defining the serial ports for PC and Bluetooth
Clovis 3:bf44d10e4904 14 #define SERIAL_1
Clovis 3:bf44d10e4904 15 Serial pc(USBTX, USBRX);
Clovis 3:bf44d10e4904 16 #ifdef SERIAL_1
Clovis 3:bf44d10e4904 17 Serial blue(PTC15,PTC14); // HC05
Clovis 3:bf44d10e4904 18 #endif
Clovis 3:bf44d10e4904 19 #ifdef SERIAL_2
Clovis 3:bf44d10e4904 20 Serial blue(PTC17,PTC16); // TX = P14 RX = P13
Clovis 3:bf44d10e4904 21 #endif
Clovis 3:bf44d10e4904 22
kirchnet 2:a0c7513fb634 23
Clovis 3:bf44d10e4904 24 DigitalOut LedGreen(LED2), LedRed(LED1);
Clovis 3:bf44d10e4904 25 TextLCD lcd(PTB9, PTA1, PTB23, PTA2, PTC2, PTC3); // rs, e, d4-d7
Clovis 3:bf44d10e4904 26 Beep buzzer(PTA0);
Clovis 3:bf44d10e4904 27 Timer timeBetweenCards;
Clovis 3:bf44d10e4904 28 char tagname;
Clovis 3:bf44d10e4904 29 char * cardtypetest;
Clovis 3:bf44d10e4904 30 char cardid;
kirchnet 2:a0c7513fb634 31
Clovis 3:bf44d10e4904 32
Clovis 3:bf44d10e4904 33
Clovis 3:bf44d10e4904 34 //MFRC522 RfChip (SPI_MOSI, SPI_MISO, SPI_SCK, SPI_CS, MF_RESET);
Clovis 3:bf44d10e4904 35 MFRC522 RfChip (PTD2, PTD3, PTD1, PTE25, PTD0);
Clovis 3:bf44d10e4904 36
kirchnet 2:a0c7513fb634 37
kirchnet 2:a0c7513fb634 38 int main(void) {
Clovis 3:bf44d10e4904 39
Clovis 3:bf44d10e4904 40 blue.baud(9600);
Clovis 3:bf44d10e4904 41 pc.baud(9600);
Clovis 3:bf44d10e4904 42 LedRed= 0;
Clovis 3:bf44d10e4904 43 pc.printf("Bluetooth and RFID Started\r\n");
kirchnet 2:a0c7513fb634 44
kirchnet 2:a0c7513fb634 45 // Init. RC522 Chip
kirchnet 2:a0c7513fb634 46 RfChip.PCD_Init();
kirchnet 2:a0c7513fb634 47
kirchnet 2:a0c7513fb634 48 while (true) {
Clovis 3:bf44d10e4904 49
Clovis 3:bf44d10e4904 50 lcd.locate(0,0);
Clovis 3:bf44d10e4904 51 lcd.printf("%.2f seconds \n", timeBetweenCards.read());
Clovis 3:bf44d10e4904 52 tagname = '\0';
Clovis 3:bf44d10e4904 53 LedRed= 1;
kirchnet 2:a0c7513fb634 54 LedGreen = 1;
kirchnet 2:a0c7513fb634 55
kirchnet 2:a0c7513fb634 56 // Look for new cards
kirchnet 2:a0c7513fb634 57 if ( ! RfChip.PICC_IsNewCardPresent())
kirchnet 2:a0c7513fb634 58 {
Clovis 3:bf44d10e4904 59 //wait_ms(100);
kirchnet 2:a0c7513fb634 60 continue;
kirchnet 2:a0c7513fb634 61 }
kirchnet 2:a0c7513fb634 62
kirchnet 2:a0c7513fb634 63 // Select one of the cards
kirchnet 2:a0c7513fb634 64 if ( ! RfChip.PICC_ReadCardSerial())
kirchnet 2:a0c7513fb634 65 {
Clovis 3:bf44d10e4904 66 //wait_ms(100);
Clovis 3:bf44d10e4904 67
kirchnet 2:a0c7513fb634 68 continue;
Clovis 3:bf44d10e4904 69
kirchnet 2:a0c7513fb634 70 }
Clovis 3:bf44d10e4904 71
Clovis 3:bf44d10e4904 72 // When a card is detected, a Green Led flashes and a buzzers bips!
Clovis 3:bf44d10e4904 73 blue.printf("%f seconds \n", timeBetweenCards.read());
Clovis 3:bf44d10e4904 74 timeBetweenCards.start();
Clovis 3:bf44d10e4904 75 LedGreen = 0;
Clovis 3:bf44d10e4904 76 buzzer.beep(5000,0.3);
kirchnet 2:a0c7513fb634 77
kirchnet 2:a0c7513fb634 78 // Print Card UID
Clovis 3:bf44d10e4904 79 printf("\n\r");
Clovis 3:bf44d10e4904 80 printf("Card UID: ");
Clovis 3:bf44d10e4904 81
kirchnet 2:a0c7513fb634 82 for (uint8_t i = 0; i < RfChip.uid.size; i++)
kirchnet 2:a0c7513fb634 83 {
Clovis 3:bf44d10e4904 84 printf(" %X02", RfChip.uid.uidByte[i]);
Clovis 3:bf44d10e4904 85 cardid= RfChip.uid.uidByte[i];
Clovis 3:bf44d10e4904 86 blue.printf(" %X02 ", (char *) cardid);
Clovis 3:bf44d10e4904 87 tagname= RfChip.uid.uidByte[i]+ tagname;
Clovis 3:bf44d10e4904 88 blue.putc(tagname);
Clovis 3:bf44d10e4904 89 lcd.locate(0,1);
Clovis 3:bf44d10e4904 90 lcd.printf("Tag id: %.0d\n", tagname);
Clovis 3:bf44d10e4904 91
kirchnet 2:a0c7513fb634 92 }
Clovis 3:bf44d10e4904 93 printf("\n\r");
Clovis 3:bf44d10e4904 94 if (tagname == 226) {
Clovis 3:bf44d10e4904 95 printf ("BLUE Keychain");
Clovis 3:bf44d10e4904 96 printf("\n\r");
Clovis 3:bf44d10e4904 97 } else {
Clovis 3:bf44d10e4904 98 if (tagname == 92) {
Clovis 3:bf44d10e4904 99
Clovis 3:bf44d10e4904 100 printf ("White card");
Clovis 3:bf44d10e4904 101 printf("\n\r");
Clovis 3:bf44d10e4904 102 }
Clovis 3:bf44d10e4904 103 }
Clovis 3:bf44d10e4904 104
Clovis 3:bf44d10e4904 105
Clovis 3:bf44d10e4904 106
Clovis 3:bf44d10e4904 107 printf("Decimal tag ID: %d \n\r", tagname);
kirchnet 2:a0c7513fb634 108
kirchnet 2:a0c7513fb634 109 // Print Card type
kirchnet 2:a0c7513fb634 110 uint8_t piccType = RfChip.PICC_GetType(RfChip.uid.sak);
Clovis 3:bf44d10e4904 111 printf("PICC Type: %s \n\r", RfChip.PICC_GetTypeName(piccType));
Clovis 3:bf44d10e4904 112 cardtypetest= RfChip.PICC_GetTypeName(piccType);
Clovis 3:bf44d10e4904 113 blue.printf("PICC Type: %s \n\r",(char *) cardtypetest);
Clovis 3:bf44d10e4904 114 //wait_ms(100);
kirchnet 2:a0c7513fb634 115 }
kirchnet 2:a0c7513fb634 116 }