RFID with HC06 bluetooth and ESP wifi module
Dependencies: mbed MFRC522 ESP8266
main.cpp@5:6ee9d8e3d385, 2019-04-21 (annotated)
- Committer:
- rajas1812
- Date:
- Sun Apr 21 15:36:13 2019 +0000
- Revision:
- 5:6ee9d8e3d385
- Parent:
- 4:834deb7037ff
RFID with HC06 bluetooth and ESP8286 wifi module
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
ndgohil | 0:ebf3612d4985 | 1 | #include "mbed.h" |
ndgohil | 0:ebf3612d4985 | 2 | #include "MFRC522.h" |
sthiyag | 2:9a39ebd76683 | 3 | #include "ESP8266.h" // Include header file from Author: Antonio Quevedo |
sthiyag | 2:9a39ebd76683 | 4 | #include "math.h" |
sthiyag | 2:9a39ebd76683 | 5 | #include <string> |
sthiyag | 2:9a39ebd76683 | 6 | |
ndgohil | 0:ebf3612d4985 | 7 | // FRDM-K64F (Freescale) Pin for MFRC522 reset |
ndgohil | 0:ebf3612d4985 | 8 | #define MF_RESET PTD0 |
ndgohil | 0:ebf3612d4985 | 9 | #define SERIAL_1 |
ndgohil | 0:ebf3612d4985 | 10 | |
sthiyag | 2:9a39ebd76683 | 11 | #define APIKEY XE2W5EIXWN6LA234 //Put "Write key" of your channel in thingspeak.com |
sthiyag | 2:9a39ebd76683 | 12 | #define IP "184.106.153.149" // IP Address of "api.thingspeak.com\" |
bkhandag | 4:834deb7037ff | 13 | #define WIFI_SSID "RajasiPhone" |
bkhandag | 4:834deb7037ff | 14 | #define WIFI_PASS "youcantguessmypassword" |
sthiyag | 2:9a39ebd76683 | 15 | |
bkhandag | 3:11a68fb3886f | 16 | Serial pc(USBTX,USBRX); |
sthiyag | 2:9a39ebd76683 | 17 | |
sthiyag | 2:9a39ebd76683 | 18 | ESP8266 esp(PTC17, PTC16, 115200); // baud rate for wifi |
sthiyag | 2:9a39ebd76683 | 19 | |
sthiyag | 2:9a39ebd76683 | 20 | char snd[255],rcv[1000],snd_Data[255]; //snd= string used to send command to ESP 8266 wii and rcv = string used to receive response from ESP8266 wifi module |
sthiyag | 2:9a39ebd76683 | 21 | |
sthiyag | 2:9a39ebd76683 | 22 | void esp_initialize(void); // Function used to initialize ESP8266 wifi module |
sthiyag | 2:9a39ebd76683 | 23 | void esp_send(void); // Function used to connect with thingspeak.com and update channel using ESP8266 wifi module |
sthiyag | 2:9a39ebd76683 | 24 | |
sthiyag | 2:9a39ebd76683 | 25 | |
ndgohil | 0:ebf3612d4985 | 26 | DigitalOut LedGreen(LED2); |
ndgohil | 0:ebf3612d4985 | 27 | |
ndgohil | 0:ebf3612d4985 | 28 | //Serial connection to PC for output |
bkhandag | 3:11a68fb3886f | 29 | //Serial pc(PTC17, PTC16); // serial comm on the FRDM board |
ndgohil | 0:ebf3612d4985 | 30 | |
ndgohil | 0:ebf3612d4985 | 31 | //wireless bluetooth connection to Android Device for output |
ndgohil | 0:ebf3612d4985 | 32 | Serial blue(PTC15, PTC14); |
ndgohil | 0:ebf3612d4985 | 33 | |
ndgohil | 0:ebf3612d4985 | 34 | |
ndgohil | 0:ebf3612d4985 | 35 | //MFRC522 RfChip (SPI_MOSI, SPI_MISO, SPI_SCK, SPI_CS, MF_RESET); |
ndgohil | 0:ebf3612d4985 | 36 | MFRC522 RfChip (PTD2, PTD3, PTD1, PTE25, PTD0); |
ndgohil | 0:ebf3612d4985 | 37 | |
rajas1812 | 1:ad028c0c6c6c | 38 | int counter1 = 0; |
rajas1812 | 1:ad028c0c6c6c | 39 | int counter2 = 0; |
rajas1812 | 1:ad028c0c6c6c | 40 | |
ndgohil | 0:ebf3612d4985 | 41 | int main(void) { |
ndgohil | 0:ebf3612d4985 | 42 | |
bkhandag | 3:11a68fb3886f | 43 | blue.baud(9600); |
bkhandag | 4:834deb7037ff | 44 | pc.baud(115200); |
bkhandag | 4:834deb7037ff | 45 | pc.printf("Welcome to RFID Tag Reader Using FRDM K64F...\n\r"); |
ndgohil | 0:ebf3612d4985 | 46 | blue.printf("Welcome to RFID Tag Reader Uing FRDM K64F...\n\r"); |
bkhandag | 4:834deb7037ff | 47 | pc.printf("Bluetooth Enable\r\n"); |
ndgohil | 0:ebf3612d4985 | 48 | blue.printf("Bluetooth Enable\r\n"); |
sthiyag | 2:9a39ebd76683 | 49 | RfChip.PCD_Init(); |
sthiyag | 2:9a39ebd76683 | 50 | |
sthiyag | 2:9a39ebd76683 | 51 | |
bkhandag | 4:834deb7037ff | 52 | //pc.baud(115200); // Baud rate used for communicating with Tera-term on PC |
sthiyag | 2:9a39ebd76683 | 53 | |
sthiyag | 2:9a39ebd76683 | 54 | pc.printf("START\r\n"); // Starting point |
sthiyag | 2:9a39ebd76683 | 55 | |
sthiyag | 2:9a39ebd76683 | 56 | esp_initialize(); |
sthiyag | 2:9a39ebd76683 | 57 | |
sthiyag | 2:9a39ebd76683 | 58 | |
ndgohil | 0:ebf3612d4985 | 59 | |
ndgohil | 0:ebf3612d4985 | 60 | while (true) { |
ndgohil | 0:ebf3612d4985 | 61 | LedGreen = 1; |
ndgohil | 0:ebf3612d4985 | 62 | |
ndgohil | 0:ebf3612d4985 | 63 | // Look for new cards |
ndgohil | 0:ebf3612d4985 | 64 | if ( ! RfChip.PICC_IsNewCardPresent()) |
ndgohil | 0:ebf3612d4985 | 65 | { |
ndgohil | 0:ebf3612d4985 | 66 | wait_ms(200); |
ndgohil | 0:ebf3612d4985 | 67 | continue; |
ndgohil | 0:ebf3612d4985 | 68 | } |
ndgohil | 0:ebf3612d4985 | 69 | |
ndgohil | 0:ebf3612d4985 | 70 | // Select one of the cards |
ndgohil | 0:ebf3612d4985 | 71 | if ( ! RfChip.PICC_ReadCardSerial()) |
ndgohil | 0:ebf3612d4985 | 72 | { |
ndgohil | 0:ebf3612d4985 | 73 | wait_ms(200); |
ndgohil | 0:ebf3612d4985 | 74 | continue; |
ndgohil | 0:ebf3612d4985 | 75 | } |
ndgohil | 0:ebf3612d4985 | 76 | LedGreen = 0; |
ndgohil | 0:ebf3612d4985 | 77 | int a=0; |
ndgohil | 0:ebf3612d4985 | 78 | |
ndgohil | 0:ebf3612d4985 | 79 | // Print Card UID |
bkhandag | 4:834deb7037ff | 80 | pc.printf("Card UID:"); |
ndgohil | 0:ebf3612d4985 | 81 | blue.printf("Card UID:"); |
ndgohil | 0:ebf3612d4985 | 82 | for (uint8_t i = 0; i < RfChip.uid.size; i++) |
ndgohil | 0:ebf3612d4985 | 83 | { |
bkhandag | 4:834deb7037ff | 84 | pc.printf(" %X02 ", RfChip.uid.uidByte[i]); |
ndgohil | 0:ebf3612d4985 | 85 | blue.printf(" %X02 ", RfChip.uid.uidByte[i]); |
ndgohil | 0:ebf3612d4985 | 86 | a = RfChip.uid.uidByte[0]; |
ndgohil | 0:ebf3612d4985 | 87 | } |
rajas1812 | 1:ad028c0c6c6c | 88 | printf("%d",a); |
ndgohil | 0:ebf3612d4985 | 89 | |
ndgohil | 0:ebf3612d4985 | 90 | //Print Card Type |
rajas1812 | 1:ad028c0c6c6c | 91 | if(a==73) |
ndgohil | 0:ebf3612d4985 | 92 | { |
rajas1812 | 1:ad028c0c6c6c | 93 | if(counter1 == 0){ |
bkhandag | 4:834deb7037ff | 94 | pc.printf("\r\nMilk is in the fridge"); |
bkhandag | 3:11a68fb3886f | 95 | blue.printf("\r\nMilk is in the fridge"); |
rajas1812 | 1:ad028c0c6c6c | 96 | counter1 = 1; |
rajas1812 | 5:6ee9d8e3d385 | 97 | |
ndgohil | 0:ebf3612d4985 | 98 | } |
rajas1812 | 1:ad028c0c6c6c | 99 | else if(counter1 == 1){ |
bkhandag | 4:834deb7037ff | 100 | pc.printf("\r\nMilk is out of the fridge"); |
bkhandag | 3:11a68fb3886f | 101 | blue.printf("\r\nMilk is in out of the fridge"); |
rajas1812 | 1:ad028c0c6c6c | 102 | counter1 = 0; |
sthiyag | 2:9a39ebd76683 | 103 | esp_send(); |
rajas1812 | 1:ad028c0c6c6c | 104 | } |
rajas1812 | 1:ad028c0c6c6c | 105 | } |
rajas1812 | 1:ad028c0c6c6c | 106 | |
rajas1812 | 1:ad028c0c6c6c | 107 | if(a==207) |
ndgohil | 0:ebf3612d4985 | 108 | { |
rajas1812 | 1:ad028c0c6c6c | 109 | if (counter2 == 0) |
rajas1812 | 1:ad028c0c6c6c | 110 | { |
rajas1812 | 1:ad028c0c6c6c | 111 | printf("\r\nBeer is in the fridge"); |
bkhandag | 3:11a68fb3886f | 112 | blue.printf("\r\nBeer is in the fridge"); |
rajas1812 | 1:ad028c0c6c6c | 113 | counter2 = 1; |
rajas1812 | 1:ad028c0c6c6c | 114 | } |
rajas1812 | 1:ad028c0c6c6c | 115 | else if (counter2 == 1){ |
rajas1812 | 1:ad028c0c6c6c | 116 | printf("\r\nBeer is out of the fridge"); |
bkhandag | 3:11a68fb3886f | 117 | blue.printf("\r\nBeer is in out of the fridge"); |
rajas1812 | 1:ad028c0c6c6c | 118 | counter2 = 0; |
sthiyag | 2:9a39ebd76683 | 119 | esp_send(); |
rajas1812 | 1:ad028c0c6c6c | 120 | } |
rajas1812 | 1:ad028c0c6c6c | 121 | } |
rajas1812 | 1:ad028c0c6c6c | 122 | //wait(1); |
rajas1812 | 1:ad028c0c6c6c | 123 | // else |
rajas1812 | 1:ad028c0c6c6c | 124 | // { |
rajas1812 | 1:ad028c0c6c6c | 125 | // printf("\r\nUnrecognized"); |
rajas1812 | 1:ad028c0c6c6c | 126 | // blue.printf("\r\nUnrecognized"); |
rajas1812 | 1:ad028c0c6c6c | 127 | //} |
ndgohil | 0:ebf3612d4985 | 128 | printf("\n\r"); |
ndgohil | 0:ebf3612d4985 | 129 | blue.printf("\n\r"); |
ndgohil | 0:ebf3612d4985 | 130 | |
ndgohil | 0:ebf3612d4985 | 131 | // Print RFID Card type |
ndgohil | 0:ebf3612d4985 | 132 | uint8_t piccType = RfChip.PICC_GetType(RfChip.uid.sak); |
ndgohil | 0:ebf3612d4985 | 133 | printf("PICC Type: %s \n\n\r\r", RfChip.PICC_GetTypeName(piccType)); |
ndgohil | 0:ebf3612d4985 | 134 | blue.printf("PICC Type: %s \n\n\r\r", RfChip.PICC_GetTypeName(piccType)); |
ndgohil | 0:ebf3612d4985 | 135 | |
rajas1812 | 1:ad028c0c6c6c | 136 | wait(2); |
ndgohil | 0:ebf3612d4985 | 137 | |
ndgohil | 0:ebf3612d4985 | 138 | } |
ndgohil | 0:ebf3612d4985 | 139 | } |
sthiyag | 2:9a39ebd76683 | 140 | void esp_initialize(void) |
sthiyag | 2:9a39ebd76683 | 141 | { |
sthiyag | 2:9a39ebd76683 | 142 | pc.printf("Initializing ESP\r\n"); |
sthiyag | 2:9a39ebd76683 | 143 | |
sthiyag | 2:9a39ebd76683 | 144 | pc.printf("Reset ESP\r\n"); |
sthiyag | 2:9a39ebd76683 | 145 | esp.Reset(); //RESET ESP |
sthiyag | 2:9a39ebd76683 | 146 | esp.RcvReply(rcv, 400); //receive a response from ESP |
sthiyag | 2:9a39ebd76683 | 147 | //pc.printf(rcv); //Print the response onscreen |
sthiyag | 2:9a39ebd76683 | 148 | wait(2); |
sthiyag | 2:9a39ebd76683 | 149 | |
sthiyag | 2:9a39ebd76683 | 150 | strcpy(snd,"AT"); |
sthiyag | 2:9a39ebd76683 | 151 | esp.SendCMD(snd); |
sthiyag | 2:9a39ebd76683 | 152 | pc.printf(snd); |
sthiyag | 2:9a39ebd76683 | 153 | //wait(2); |
sthiyag | 2:9a39ebd76683 | 154 | esp.RcvReply(rcv, 400); |
sthiyag | 2:9a39ebd76683 | 155 | pc.printf(rcv); |
sthiyag | 2:9a39ebd76683 | 156 | wait(0.1); |
sthiyag | 2:9a39ebd76683 | 157 | |
sthiyag | 2:9a39ebd76683 | 158 | strcpy(snd,"AT+CWMODE=1"); |
sthiyag | 2:9a39ebd76683 | 159 | esp.SendCMD(snd); |
sthiyag | 2:9a39ebd76683 | 160 | pc.printf(snd); |
sthiyag | 2:9a39ebd76683 | 161 | wait(2); |
sthiyag | 2:9a39ebd76683 | 162 | |
sthiyag | 2:9a39ebd76683 | 163 | strcpy(snd,"AT+CWJAP=\""); |
sthiyag | 2:9a39ebd76683 | 164 | strcat(snd,WIFI_SSID); |
sthiyag | 2:9a39ebd76683 | 165 | strcat(snd,"\",\""); |
sthiyag | 2:9a39ebd76683 | 166 | strcat(snd,WIFI_PASS); |
sthiyag | 2:9a39ebd76683 | 167 | strcat(snd,"\""); |
sthiyag | 2:9a39ebd76683 | 168 | |
sthiyag | 2:9a39ebd76683 | 169 | esp.SendCMD(snd); |
sthiyag | 2:9a39ebd76683 | 170 | pc.printf(snd); |
sthiyag | 2:9a39ebd76683 | 171 | wait(5); |
sthiyag | 2:9a39ebd76683 | 172 | esp.RcvReply(rcv, 400); |
sthiyag | 2:9a39ebd76683 | 173 | pc.printf("\n %s \n", rcv); |
sthiyag | 2:9a39ebd76683 | 174 | |
sthiyag | 2:9a39ebd76683 | 175 | strcpy(snd,"AT+CIPMUX=0"); |
sthiyag | 2:9a39ebd76683 | 176 | esp.SendCMD(snd); |
sthiyag | 2:9a39ebd76683 | 177 | pc.printf(snd); |
sthiyag | 2:9a39ebd76683 | 178 | //wait(2); |
sthiyag | 2:9a39ebd76683 | 179 | esp.RcvReply(rcv, 400); |
sthiyag | 2:9a39ebd76683 | 180 | pc.printf("\n %s \n", rcv); |
sthiyag | 2:9a39ebd76683 | 181 | |
sthiyag | 2:9a39ebd76683 | 182 | } |
sthiyag | 2:9a39ebd76683 | 183 | |
sthiyag | 2:9a39ebd76683 | 184 | |
sthiyag | 2:9a39ebd76683 | 185 | void esp_send(void) |
sthiyag | 2:9a39ebd76683 | 186 | { |
rajas1812 | 5:6ee9d8e3d385 | 187 | int field1 = 0; |
rajas1812 | 5:6ee9d8e3d385 | 188 | int field2 = 0; |
sthiyag | 2:9a39ebd76683 | 189 | //ESP updates the Status of Thingspeak channel// |
sthiyag | 2:9a39ebd76683 | 190 | |
sthiyag | 2:9a39ebd76683 | 191 | strcpy(snd,"AT+CIPSTART="); |
sthiyag | 2:9a39ebd76683 | 192 | strcat(snd,"\"TCP\",\""); |
sthiyag | 2:9a39ebd76683 | 193 | strcat(snd,IP); |
sthiyag | 2:9a39ebd76683 | 194 | strcat(snd,"\",80"); |
sthiyag | 2:9a39ebd76683 | 195 | |
sthiyag | 2:9a39ebd76683 | 196 | esp.SendCMD(snd); |
sthiyag | 2:9a39ebd76683 | 197 | pc.printf("S\r\n%s",snd); |
sthiyag | 2:9a39ebd76683 | 198 | //wait(2); |
sthiyag | 2:9a39ebd76683 | 199 | esp.RcvReply(rcv, 1000); |
sthiyag | 2:9a39ebd76683 | 200 | pc.printf("R\r\n%s",rcv); |
sthiyag | 2:9a39ebd76683 | 201 | wait(1); |
sthiyag | 2:9a39ebd76683 | 202 | |
rajas1812 | 5:6ee9d8e3d385 | 203 | sprintf(snd,"GET https://api.thingspeak.com/update?key=XE2W5EIXWN6LA234&field1=%d&field2=%d\r\n",field1,field2); |
sthiyag | 2:9a39ebd76683 | 204 | |
sthiyag | 2:9a39ebd76683 | 205 | int i=0; |
sthiyag | 2:9a39ebd76683 | 206 | for(i=0;snd[i]!='\0';i++); |
sthiyag | 2:9a39ebd76683 | 207 | i++; |
sthiyag | 2:9a39ebd76683 | 208 | char cmd[255]; |
sthiyag | 2:9a39ebd76683 | 209 | |
sthiyag | 2:9a39ebd76683 | 210 | sprintf(cmd,"AT+CIPSEND=%d",i); //Send Number of open connection and Characters to send |
sthiyag | 2:9a39ebd76683 | 211 | esp.SendCMD(cmd); |
sthiyag | 2:9a39ebd76683 | 212 | pc.printf("S\r\n%s",cmd); |
sthiyag | 2:9a39ebd76683 | 213 | while(i<=20 || rcv == ">") |
sthiyag | 2:9a39ebd76683 | 214 | { |
sthiyag | 2:9a39ebd76683 | 215 | esp.RcvReply(rcv, 1000); |
sthiyag | 2:9a39ebd76683 | 216 | wait(100); |
sthiyag | 2:9a39ebd76683 | 217 | i++; |
sthiyag | 2:9a39ebd76683 | 218 | } |
sthiyag | 2:9a39ebd76683 | 219 | pc.printf("R\r\n%s",rcv); |
sthiyag | 2:9a39ebd76683 | 220 | |
sthiyag | 2:9a39ebd76683 | 221 | esp.SendCMD(snd); //Post value to thingspeak channel |
sthiyag | 2:9a39ebd76683 | 222 | pc.printf("S\r\n%s",snd); |
sthiyag | 2:9a39ebd76683 | 223 | |
sthiyag | 2:9a39ebd76683 | 224 | while(i<=20 || rcv == "OK") |
sthiyag | 2:9a39ebd76683 | 225 | { |
sthiyag | 2:9a39ebd76683 | 226 | esp.RcvReply(rcv, 1000); |
sthiyag | 2:9a39ebd76683 | 227 | wait(100); |
sthiyag | 2:9a39ebd76683 | 228 | i++; |
sthiyag | 2:9a39ebd76683 | 229 | } |
sthiyag | 2:9a39ebd76683 | 230 | pc.printf("R\r\n%s",rcv); |
sthiyag | 2:9a39ebd76683 | 231 | |
sthiyag | 2:9a39ebd76683 | 232 | } |
rajas1812 | 5:6ee9d8e3d385 | 233 |