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