Smart Fridge code

Dependencies:   mbed MFRC522

Committer:
bkhandag
Date:
Sat Apr 20 15:00:03 2019 +0000
Revision:
3:11a68fb3886f
Parent:
2:9a39ebd76683
Child:
4:834deb7037ff
v1

Who changed what in which revision?

UserRevisionLine numberNew 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\"
sthiyag 2:9a39ebd76683 13 #define WIFI_SSID "Sheelu"
bkhandag 3:11a68fb3886f 14 #define WIFI_PASS "Barbie123"
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 3:11a68fb3886f 44 //pc.baud(9600);
ndgohil 0:ebf3612d4985 45 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");
ndgohil 0:ebf3612d4985 47 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
sthiyag 2:9a39ebd76683 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
ndgohil 0:ebf3612d4985 80 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 {
ndgohil 0:ebf3612d4985 84 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){
rajas1812 1:ad028c0c6c6c 94 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;
ndgohil 0:ebf3612d4985 97 }
rajas1812 1:ad028c0c6c6c 98 else if(counter1 == 1){
rajas1812 1:ad028c0c6c6c 99 printf("\r\nMilk is out of the fridge");
bkhandag 3:11a68fb3886f 100 blue.printf("\r\nMilk is in out of the fridge");
rajas1812 1:ad028c0c6c6c 101 counter1 = 0;
sthiyag 2:9a39ebd76683 102 esp_send();
rajas1812 1:ad028c0c6c6c 103 }
rajas1812 1:ad028c0c6c6c 104 }
rajas1812 1:ad028c0c6c6c 105
rajas1812 1:ad028c0c6c6c 106 if(a==207)
ndgohil 0:ebf3612d4985 107 {
rajas1812 1:ad028c0c6c6c 108 if (counter2 == 0)
rajas1812 1:ad028c0c6c6c 109 {
rajas1812 1:ad028c0c6c6c 110 printf("\r\nBeer is in the fridge");
bkhandag 3:11a68fb3886f 111 blue.printf("\r\nBeer is in the fridge");
rajas1812 1:ad028c0c6c6c 112 counter2 = 1;
rajas1812 1:ad028c0c6c6c 113 }
rajas1812 1:ad028c0c6c6c 114 else if (counter2 == 1){
rajas1812 1:ad028c0c6c6c 115 printf("\r\nBeer is out of the fridge");
bkhandag 3:11a68fb3886f 116 blue.printf("\r\nBeer is in out of the fridge");
rajas1812 1:ad028c0c6c6c 117 counter2 = 0;
sthiyag 2:9a39ebd76683 118 esp_send();
rajas1812 1:ad028c0c6c6c 119 }
rajas1812 1:ad028c0c6c6c 120 }
rajas1812 1:ad028c0c6c6c 121 //wait(1);
rajas1812 1:ad028c0c6c6c 122 // else
rajas1812 1:ad028c0c6c6c 123 // {
rajas1812 1:ad028c0c6c6c 124 // printf("\r\nUnrecognized");
rajas1812 1:ad028c0c6c6c 125 // blue.printf("\r\nUnrecognized");
rajas1812 1:ad028c0c6c6c 126 //}
ndgohil 0:ebf3612d4985 127 printf("\n\r");
ndgohil 0:ebf3612d4985 128 blue.printf("\n\r");
ndgohil 0:ebf3612d4985 129
ndgohil 0:ebf3612d4985 130 // Print RFID Card type
ndgohil 0:ebf3612d4985 131 uint8_t piccType = RfChip.PICC_GetType(RfChip.uid.sak);
ndgohil 0:ebf3612d4985 132 printf("PICC Type: %s \n\n\r\r", RfChip.PICC_GetTypeName(piccType));
ndgohil 0:ebf3612d4985 133 blue.printf("PICC Type: %s \n\n\r\r", RfChip.PICC_GetTypeName(piccType));
ndgohil 0:ebf3612d4985 134
rajas1812 1:ad028c0c6c6c 135 wait(2);
ndgohil 0:ebf3612d4985 136
ndgohil 0:ebf3612d4985 137 }
ndgohil 0:ebf3612d4985 138 }
sthiyag 2:9a39ebd76683 139 void esp_initialize(void)
sthiyag 2:9a39ebd76683 140 {
sthiyag 2:9a39ebd76683 141 pc.printf("Initializing ESP\r\n");
sthiyag 2:9a39ebd76683 142
sthiyag 2:9a39ebd76683 143 pc.printf("Reset ESP\r\n");
sthiyag 2:9a39ebd76683 144 esp.Reset(); //RESET ESP
sthiyag 2:9a39ebd76683 145 esp.RcvReply(rcv, 400); //receive a response from ESP
sthiyag 2:9a39ebd76683 146 //pc.printf(rcv); //Print the response onscreen
sthiyag 2:9a39ebd76683 147 wait(2);
sthiyag 2:9a39ebd76683 148
sthiyag 2:9a39ebd76683 149 strcpy(snd,"AT");
sthiyag 2:9a39ebd76683 150 esp.SendCMD(snd);
sthiyag 2:9a39ebd76683 151 pc.printf(snd);
sthiyag 2:9a39ebd76683 152 //wait(2);
sthiyag 2:9a39ebd76683 153 esp.RcvReply(rcv, 400);
sthiyag 2:9a39ebd76683 154 pc.printf(rcv);
sthiyag 2:9a39ebd76683 155 wait(0.1);
sthiyag 2:9a39ebd76683 156
sthiyag 2:9a39ebd76683 157 strcpy(snd,"AT+CWMODE=1");
sthiyag 2:9a39ebd76683 158 esp.SendCMD(snd);
sthiyag 2:9a39ebd76683 159 pc.printf(snd);
sthiyag 2:9a39ebd76683 160 wait(2);
sthiyag 2:9a39ebd76683 161
sthiyag 2:9a39ebd76683 162 strcpy(snd,"AT+CWJAP=\"");
sthiyag 2:9a39ebd76683 163 strcat(snd,WIFI_SSID);
sthiyag 2:9a39ebd76683 164 strcat(snd,"\",\"");
sthiyag 2:9a39ebd76683 165 strcat(snd,WIFI_PASS);
sthiyag 2:9a39ebd76683 166 strcat(snd,"\"");
sthiyag 2:9a39ebd76683 167
sthiyag 2:9a39ebd76683 168 esp.SendCMD(snd);
sthiyag 2:9a39ebd76683 169 pc.printf(snd);
sthiyag 2:9a39ebd76683 170 wait(5);
sthiyag 2:9a39ebd76683 171 esp.RcvReply(rcv, 400);
sthiyag 2:9a39ebd76683 172 pc.printf("\n %s \n", rcv);
sthiyag 2:9a39ebd76683 173
sthiyag 2:9a39ebd76683 174 strcpy(snd,"AT+CIPMUX=0");
sthiyag 2:9a39ebd76683 175 esp.SendCMD(snd);
sthiyag 2:9a39ebd76683 176 pc.printf(snd);
sthiyag 2:9a39ebd76683 177 //wait(2);
sthiyag 2:9a39ebd76683 178 esp.RcvReply(rcv, 400);
sthiyag 2:9a39ebd76683 179 pc.printf("\n %s \n", rcv);
sthiyag 2:9a39ebd76683 180
sthiyag 2:9a39ebd76683 181 }
sthiyag 2:9a39ebd76683 182
sthiyag 2:9a39ebd76683 183
sthiyag 2:9a39ebd76683 184 void esp_send(void)
sthiyag 2:9a39ebd76683 185 {
sthiyag 2:9a39ebd76683 186
sthiyag 2:9a39ebd76683 187 //ESP updates the Status of Thingspeak channel//
sthiyag 2:9a39ebd76683 188
sthiyag 2:9a39ebd76683 189 strcpy(snd,"AT+CIPSTART=");
sthiyag 2:9a39ebd76683 190 strcat(snd,"\"TCP\",\"");
sthiyag 2:9a39ebd76683 191 strcat(snd,IP);
sthiyag 2:9a39ebd76683 192 strcat(snd,"\",80");
sthiyag 2:9a39ebd76683 193
sthiyag 2:9a39ebd76683 194 esp.SendCMD(snd);
sthiyag 2:9a39ebd76683 195 pc.printf("S\r\n%s",snd);
sthiyag 2:9a39ebd76683 196 //wait(2);
sthiyag 2:9a39ebd76683 197 esp.RcvReply(rcv, 1000);
sthiyag 2:9a39ebd76683 198 pc.printf("R\r\n%s",rcv);
sthiyag 2:9a39ebd76683 199 wait(1);
sthiyag 2:9a39ebd76683 200
sthiyag 2:9a39ebd76683 201 sprintf(snd,"GET https://api.thingspeak.com/update?key=XE2W5EIXWN6LA234&field1=%d&field2=%d\r\n",counter1,counter2);
sthiyag 2:9a39ebd76683 202
sthiyag 2:9a39ebd76683 203 int i=0;
sthiyag 2:9a39ebd76683 204 for(i=0;snd[i]!='\0';i++);
sthiyag 2:9a39ebd76683 205 i++;
sthiyag 2:9a39ebd76683 206 char cmd[255];
sthiyag 2:9a39ebd76683 207
sthiyag 2:9a39ebd76683 208 sprintf(cmd,"AT+CIPSEND=%d",i); //Send Number of open connection and Characters to send
sthiyag 2:9a39ebd76683 209 esp.SendCMD(cmd);
sthiyag 2:9a39ebd76683 210 pc.printf("S\r\n%s",cmd);
sthiyag 2:9a39ebd76683 211 while(i<=20 || rcv == ">")
sthiyag 2:9a39ebd76683 212 {
sthiyag 2:9a39ebd76683 213 esp.RcvReply(rcv, 1000);
sthiyag 2:9a39ebd76683 214 wait(100);
sthiyag 2:9a39ebd76683 215 i++;
sthiyag 2:9a39ebd76683 216 }
sthiyag 2:9a39ebd76683 217 pc.printf("R\r\n%s",rcv);
sthiyag 2:9a39ebd76683 218
sthiyag 2:9a39ebd76683 219 esp.SendCMD(snd); //Post value to thingspeak channel
sthiyag 2:9a39ebd76683 220 pc.printf("S\r\n%s",snd);
sthiyag 2:9a39ebd76683 221
sthiyag 2:9a39ebd76683 222 while(i<=20 || rcv == "OK")
sthiyag 2:9a39ebd76683 223 {
sthiyag 2:9a39ebd76683 224 esp.RcvReply(rcv, 1000);
sthiyag 2:9a39ebd76683 225 wait(100);
sthiyag 2:9a39ebd76683 226 i++;
sthiyag 2:9a39ebd76683 227 }
sthiyag 2:9a39ebd76683 228 pc.printf("R\r\n%s",rcv);
sthiyag 2:9a39ebd76683 229
sthiyag 2:9a39ebd76683 230 }