Smart Fridge code
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 "RajasiPhone" 00014 #define WIFI_PASS "youcantguessmypassword" 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(9600); 00044 pc.baud(115200); 00045 pc.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 pc.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 pc.printf("Card UID:"); 00081 blue.printf("Card UID:"); 00082 for (uint8_t i = 0; i < RfChip.uid.size; i++) 00083 { 00084 pc.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 pc.printf("\r\nMilk is in the fridge"); 00095 blue.printf("\r\nMilk is in the fridge"); 00096 counter1 = 1; 00097 } 00098 else if(counter1 == 1){ 00099 pc.printf("\r\nMilk is out of the fridge"); 00100 blue.printf("\r\nMilk is in out of the fridge"); 00101 counter1 = 0; 00102 esp_send(); 00103 } 00104 } 00105 00106 if(a==207) 00107 { 00108 if (counter2 == 0) 00109 { 00110 printf("\r\nBeer is in the fridge"); 00111 blue.printf("\r\nBeer is in the fridge"); 00112 counter2 = 1; 00113 } 00114 else if (counter2 == 1){ 00115 printf("\r\nBeer is out of the fridge"); 00116 blue.printf("\r\nBeer is in out of the fridge"); 00117 counter2 = 0; 00118 esp_send(); 00119 } 00120 } 00121 //wait(1); 00122 // else 00123 // { 00124 // printf("\r\nUnrecognized"); 00125 // blue.printf("\r\nUnrecognized"); 00126 //} 00127 printf("\n\r"); 00128 blue.printf("\n\r"); 00129 00130 // Print RFID Card type 00131 uint8_t piccType = RfChip.PICC_GetType(RfChip.uid.sak); 00132 printf("PICC Type: %s \n\n\r\r", RfChip.PICC_GetTypeName(piccType)); 00133 blue.printf("PICC Type: %s \n\n\r\r", RfChip.PICC_GetTypeName(piccType)); 00134 00135 wait(2); 00136 00137 } 00138 } 00139 void esp_initialize(void) 00140 { 00141 pc.printf("Initializing ESP\r\n"); 00142 00143 pc.printf("Reset ESP\r\n"); 00144 esp.Reset(); //RESET ESP 00145 esp.RcvReply(rcv, 400); //receive a response from ESP 00146 //pc.printf(rcv); //Print the response onscreen 00147 wait(2); 00148 00149 strcpy(snd,"AT"); 00150 esp.SendCMD(snd); 00151 pc.printf(snd); 00152 //wait(2); 00153 esp.RcvReply(rcv, 400); 00154 pc.printf(rcv); 00155 wait(0.1); 00156 00157 strcpy(snd,"AT+CWMODE=1"); 00158 esp.SendCMD(snd); 00159 pc.printf(snd); 00160 wait(2); 00161 00162 strcpy(snd,"AT+CWJAP=\""); 00163 strcat(snd,WIFI_SSID); 00164 strcat(snd,"\",\""); 00165 strcat(snd,WIFI_PASS); 00166 strcat(snd,"\""); 00167 00168 esp.SendCMD(snd); 00169 pc.printf(snd); 00170 wait(5); 00171 esp.RcvReply(rcv, 400); 00172 pc.printf("\n %s \n", rcv); 00173 00174 strcpy(snd,"AT+CIPMUX=0"); 00175 esp.SendCMD(snd); 00176 pc.printf(snd); 00177 //wait(2); 00178 esp.RcvReply(rcv, 400); 00179 pc.printf("\n %s \n", rcv); 00180 00181 } 00182 00183 00184 void esp_send(void) 00185 { 00186 00187 //ESP updates the Status of Thingspeak channel// 00188 00189 strcpy(snd,"AT+CIPSTART="); 00190 strcat(snd,"\"TCP\",\""); 00191 strcat(snd,IP); 00192 strcat(snd,"\",80"); 00193 00194 esp.SendCMD(snd); 00195 pc.printf("S\r\n%s",snd); 00196 //wait(2); 00197 esp.RcvReply(rcv, 1000); 00198 pc.printf("R\r\n%s",rcv); 00199 wait(1); 00200 00201 sprintf(snd,"GET https://api.thingspeak.com/update?key=XE2W5EIXWN6LA234&field1=%d&field2=%d\r\n",counter1,counter2); 00202 00203 int i=0; 00204 for(i=0;snd[i]!='\0';i++); 00205 i++; 00206 char cmd[255]; 00207 00208 sprintf(cmd,"AT+CIPSEND=%d",i); //Send Number of open connection and Characters to send 00209 esp.SendCMD(cmd); 00210 pc.printf("S\r\n%s",cmd); 00211 while(i<=20 || rcv == ">") 00212 { 00213 esp.RcvReply(rcv, 1000); 00214 wait(100); 00215 i++; 00216 } 00217 pc.printf("R\r\n%s",rcv); 00218 00219 esp.SendCMD(snd); //Post value to thingspeak channel 00220 pc.printf("S\r\n%s",snd); 00221 00222 while(i<=20 || rcv == "OK") 00223 { 00224 esp.RcvReply(rcv, 1000); 00225 wait(100); 00226 i++; 00227 } 00228 pc.printf("R\r\n%s",rcv); 00229 00230 }
Generated on Wed Aug 17 2022 15:27:54 by
1.7.2