smart fridge using RFID and Bluetooth
Embed:
(wiki syntax)
Show/hide line numbers
main.cpp
00001 #include "mbed.h" 00002 #include "MFRC522.h" 00003 #include "ESP8266.h" // Include header file from Author: Antonio Quevedo 00004 #include "math.h" 00005 #include <string> 00006 00007 // FRDM-K64F (Freescale) Pin for MFRC522 reset 00008 #define MF_RESET PTD0 00009 #define SERIAL_1 00010 00011 #define APIKEY XE2W5EIXWN6LA234 //Put "Write key" of your channel in thingspeak.com 00012 #define IP "184.106.153.149" // IP Address of "api.thingspeak.com\" 00013 #define WIFI_SSID "Sheelu" 00014 #define WIFI_PASS "Barbie" 00015 00016 //Serial pc(USBTX,USBRX); 00017 00018 ESP8266 esp(PTC17, PTC16, 115200); // baud rate for wifi 00019 00020 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 00021 00022 void esp_initialize(void); // Function used to initialize ESP8266 wifi module 00023 void esp_send(void); // Function used to connect with thingspeak.com and update channel using ESP8266 wifi module 00024 00025 00026 DigitalOut LedGreen(LED2); 00027 00028 //Serial connection to PC for output 00029 Serial pc(PTC17, PTC16); // serial comm on the FRDM board 00030 00031 //wireless bluetooth connection to Android Device for output 00032 Serial blue(PTC15, PTC14); 00033 00034 00035 //MFRC522 RfChip (SPI_MOSI, SPI_MISO, SPI_SCK, SPI_CS, MF_RESET); 00036 MFRC522 RfChip (PTD2, PTD3, PTD1, PTE25, PTD0); 00037 00038 int counter1 = 0; 00039 int counter2 = 0; 00040 00041 int main(void) { 00042 00043 blue.baud(115200); 00044 pc.baud(115200); 00045 printf("Welcome to RFID Tag Reader Using FRDM K64F...\n\r"); 00046 blue.printf("Welcome to RFID Tag Reader Uing FRDM K64F...\n\r"); 00047 printf("Bluetooth Enable\r\n"); 00048 blue.printf("Bluetooth Enable\r\n"); 00049 RfChip.PCD_Init(); 00050 00051 00052 pc.baud(115200); // Baud rate used for communicating with Tera-term on PC 00053 00054 pc.printf("START\r\n"); // Starting point 00055 00056 esp_initialize(); 00057 00058 00059 00060 while (true) { 00061 LedGreen = 1; 00062 00063 // Look for new cards 00064 if ( ! RfChip.PICC_IsNewCardPresent()) 00065 { 00066 wait_ms(200); 00067 continue; 00068 } 00069 00070 // Select one of the cards 00071 if ( ! RfChip.PICC_ReadCardSerial()) 00072 { 00073 wait_ms(200); 00074 continue; 00075 } 00076 LedGreen = 0; 00077 int a=0; 00078 00079 // Print Card UID 00080 printf("Card UID:"); 00081 blue.printf("Card UID:"); 00082 for (uint8_t i = 0; i < RfChip.uid.size; i++) 00083 { 00084 printf(" %X02 ", RfChip.uid.uidByte[i]); 00085 blue.printf(" %X02 ", RfChip.uid.uidByte[i]); 00086 a = RfChip.uid.uidByte[0]; 00087 } 00088 printf("%d",a); 00089 00090 //Print Card Type 00091 if(a==73) 00092 { 00093 if(counter1 == 0){ 00094 printf("\r\nMilk is in the fridge"); 00095 blue.printf("\r\nWhite Card"); 00096 counter1 = 1; 00097 } 00098 else if(counter1 == 1){ 00099 printf("\r\nMilk is out of the fridge"); 00100 counter1 = 0; 00101 esp_send(); 00102 } 00103 } 00104 00105 if(a==207) 00106 { 00107 if (counter2 == 0) 00108 { 00109 printf("\r\nBeer is in the fridge"); 00110 blue.printf("\r\nBlue Keychain"); 00111 counter2 = 1; 00112 } 00113 else if (counter2 == 1){ 00114 printf("\r\nBeer is out of the fridge"); 00115 counter2 = 0; 00116 esp_send(); 00117 } 00118 } 00119 //wait(1); 00120 // else 00121 // { 00122 // printf("\r\nUnrecognized"); 00123 // blue.printf("\r\nUnrecognized"); 00124 //} 00125 printf("\n\r"); 00126 blue.printf("\n\r"); 00127 00128 // Print RFID Card type 00129 uint8_t piccType = RfChip.PICC_GetType(RfChip.uid.sak); 00130 printf("PICC Type: %s \n\n\r\r", RfChip.PICC_GetTypeName(piccType)); 00131 blue.printf("PICC Type: %s \n\n\r\r", RfChip.PICC_GetTypeName(piccType)); 00132 00133 wait(2); 00134 00135 } 00136 } 00137 void esp_initialize(void) 00138 { 00139 pc.printf("Initializing ESP\r\n"); 00140 00141 pc.printf("Reset ESP\r\n"); 00142 esp.Reset(); //RESET ESP 00143 esp.RcvReply(rcv, 400); //receive a response from ESP 00144 //pc.printf(rcv); //Print the response onscreen 00145 wait(2); 00146 00147 strcpy(snd,"AT"); 00148 esp.SendCMD(snd); 00149 pc.printf(snd); 00150 //wait(2); 00151 esp.RcvReply(rcv, 400); 00152 pc.printf(rcv); 00153 wait(0.1); 00154 00155 strcpy(snd,"AT+CWMODE=1"); 00156 esp.SendCMD(snd); 00157 pc.printf(snd); 00158 wait(2); 00159 00160 strcpy(snd,"AT+CWJAP=\""); 00161 strcat(snd,WIFI_SSID); 00162 strcat(snd,"\",\""); 00163 strcat(snd,WIFI_PASS); 00164 strcat(snd,"\""); 00165 00166 esp.SendCMD(snd); 00167 pc.printf(snd); 00168 wait(5); 00169 esp.RcvReply(rcv, 400); 00170 pc.printf("\n %s \n", rcv); 00171 00172 strcpy(snd,"AT+CIPMUX=0"); 00173 esp.SendCMD(snd); 00174 pc.printf(snd); 00175 //wait(2); 00176 esp.RcvReply(rcv, 400); 00177 pc.printf("\n %s \n", rcv); 00178 00179 } 00180 00181 00182 void esp_send(void) 00183 { 00184 00185 //ESP updates the Status of Thingspeak channel// 00186 00187 strcpy(snd,"AT+CIPSTART="); 00188 strcat(snd,"\"TCP\",\""); 00189 strcat(snd,IP); 00190 strcat(snd,"\",80"); 00191 00192 esp.SendCMD(snd); 00193 pc.printf("S\r\n%s",snd); 00194 //wait(2); 00195 esp.RcvReply(rcv, 1000); 00196 pc.printf("R\r\n%s",rcv); 00197 wait(1); 00198 00199 sprintf(snd,"GET https://api.thingspeak.com/update?key=XE2W5EIXWN6LA234&field1=%d&field2=%d\r\n",counter1,counter2); 00200 00201 int i=0; 00202 for(i=0;snd[i]!='\0';i++); 00203 i++; 00204 char cmd[255]; 00205 00206 sprintf(cmd,"AT+CIPSEND=%d",i); //Send Number of open connection and Characters to send 00207 esp.SendCMD(cmd); 00208 pc.printf("S\r\n%s",cmd); 00209 while(i<=20 || rcv == ">") 00210 { 00211 esp.RcvReply(rcv, 1000); 00212 wait(100); 00213 i++; 00214 } 00215 pc.printf("R\r\n%s",rcv); 00216 00217 esp.SendCMD(snd); //Post value to thingspeak channel 00218 pc.printf("S\r\n%s",snd); 00219 00220 while(i<=20 || rcv == "OK") 00221 { 00222 esp.RcvReply(rcv, 1000); 00223 wait(100); 00224 i++; 00225 } 00226 pc.printf("R\r\n%s",rcv); 00227 00228 }
Generated on Fri Jul 15 2022 14:47:13 by
1.7.2