![](/media/cache/profiles/d68e30601ca27cf28d9e143af9fdc7e2.jpg.50x50_q85.jpg)
The project is about embedding temperature sensor, a PIR sensor and a thermocouple with RFID as a security measure and sending the data to ThingSpeak Cloud with a WiFi module ESP8266
Dependencies: DHT ESP8266 MFRC522
main.cpp@0:94f21066fc43, 2017-03-28 (annotated)
- Committer:
- architmuchhal
- Date:
- Tue Mar 28 17:19:25 2017 +0000
- Revision:
- 0:94f21066fc43
Internet I/O Bus powered by Hexiwear Brain using FRDM - K64F
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
architmuchhal | 0:94f21066fc43 | 1 | #include "mbed.h" |
architmuchhal | 0:94f21066fc43 | 2 | #include "MFRC522.h" |
architmuchhal | 0:94f21066fc43 | 3 | #include "DHT.h" |
architmuchhal | 0:94f21066fc43 | 4 | #include "ESP8266.h" // Include header file |
architmuchhal | 0:94f21066fc43 | 5 | #include "math.h" |
architmuchhal | 0:94f21066fc43 | 6 | #include <string> |
architmuchhal | 0:94f21066fc43 | 7 | |
architmuchhal | 0:94f21066fc43 | 8 | #define APIKEY D9BGPWTNAGUAZPMB //Put "Write key" of your channel in thingspeak.com |
architmuchhal | 0:94f21066fc43 | 9 | #define IP "184.106.153.149" // IP Address of "api.thingspeak.com\" |
architmuchhal | 0:94f21066fc43 | 10 | #define WIFI_SSID "Archit" |
architmuchhal | 0:94f21066fc43 | 11 | #define WIFI_PASS "archit12" |
architmuchhal | 0:94f21066fc43 | 12 | |
architmuchhal | 0:94f21066fc43 | 13 | ESP8266 esp(PTC17, PTC16, 115200); // baud rate for wifi |
architmuchhal | 0:94f21066fc43 | 14 | |
architmuchhal | 0:94f21066fc43 | 15 | // FRDM-K64F (Freescale) Pin for MFRC522 reset |
architmuchhal | 0:94f21066fc43 | 16 | #define MF_RESET PTB19 |
architmuchhal | 0:94f21066fc43 | 17 | |
architmuchhal | 0:94f21066fc43 | 18 | // Defining the serial ports for PC and Bluetooth |
architmuchhal | 0:94f21066fc43 | 19 | #define SERIAL_1 |
architmuchhal | 0:94f21066fc43 | 20 | AnalogIn check(PTB3); //thermocouple Analog input |
architmuchhal | 0:94f21066fc43 | 21 | |
architmuchhal | 0:94f21066fc43 | 22 | Serial pc(USBTX, USBRX); |
architmuchhal | 0:94f21066fc43 | 23 | DHT sensor(PTC2,DHT22); //DHT Sensor |
architmuchhal | 0:94f21066fc43 | 24 | DigitalOut LedGreen(LED2), LedRed(LED1); |
architmuchhal | 0:94f21066fc43 | 25 | DigitalIn inputPin(PTB13); // pir senor input |
architmuchhal | 0:94f21066fc43 | 26 | |
architmuchhal | 0:94f21066fc43 | 27 | int val=1; |
architmuchhal | 0:94f21066fc43 | 28 | float tmp_f, tmp_c, hum; |
architmuchhal | 0:94f21066fc43 | 29 | Timer timeBetweenCards; |
architmuchhal | 0:94f21066fc43 | 30 | char tagname; |
architmuchhal | 0:94f21066fc43 | 31 | |
architmuchhal | 0:94f21066fc43 | 32 | uint8_t user[10]; |
architmuchhal | 0:94f21066fc43 | 33 | char Richard[17]="A6027B026802A102"; |
architmuchhal | 0:94f21066fc43 | 34 | char tempo[17]; |
architmuchhal | 0:94f21066fc43 | 35 | char snd[255],rcv[1000],snd_Data[255]; |
architmuchhal | 0:94f21066fc43 | 36 | float x=0; |
architmuchhal | 0:94f21066fc43 | 37 | |
architmuchhal | 0:94f21066fc43 | 38 | //MFRC522 RfChip (SPI_MOSI, SPI_MISO, SPI_SCK, SPI_CS, MF_RESET); |
architmuchhal | 0:94f21066fc43 | 39 | MFRC522 RfChip (PTC6, PTC7, PTC5, PTC3, PTB19); |
architmuchhal | 0:94f21066fc43 | 40 | |
architmuchhal | 0:94f21066fc43 | 41 | void esp_initialize(void); |
architmuchhal | 0:94f21066fc43 | 42 | void esp_send(void); |
architmuchhal | 0:94f21066fc43 | 43 | |
architmuchhal | 0:94f21066fc43 | 44 | int main(void) |
architmuchhal | 0:94f21066fc43 | 45 | { |
architmuchhal | 0:94f21066fc43 | 46 | pc.baud(115200); |
architmuchhal | 0:94f21066fc43 | 47 | esp_initialize(); |
architmuchhal | 0:94f21066fc43 | 48 | LedRed= 0; |
architmuchhal | 0:94f21066fc43 | 49 | |
architmuchhal | 0:94f21066fc43 | 50 | // Initialising RC522 Chip |
architmuchhal | 0:94f21066fc43 | 51 | RfChip.PCD_Init(); |
architmuchhal | 0:94f21066fc43 | 52 | |
architmuchhal | 0:94f21066fc43 | 53 | while (true) |
architmuchhal | 0:94f21066fc43 | 54 | |
architmuchhal | 0:94f21066fc43 | 55 | { |
architmuchhal | 0:94f21066fc43 | 56 | if(inputPin==1) |
architmuchhal | 0:94f21066fc43 | 57 | |
architmuchhal | 0:94f21066fc43 | 58 | { |
architmuchhal | 0:94f21066fc43 | 59 | printf("Motion Detected!\r\n"); |
architmuchhal | 0:94f21066fc43 | 60 | printf("************************************************\r\n"); |
architmuchhal | 0:94f21066fc43 | 61 | printf("You have approached Omnisite Monitioring Solutions!\r\n"); |
architmuchhal | 0:94f21066fc43 | 62 | printf("------------------------------------------------------------\r\n"); |
architmuchhal | 0:94f21066fc43 | 63 | printf("Place Your ID to get access:\r\n"); |
architmuchhal | 0:94f21066fc43 | 64 | printf("------------------------------------------------------------\r\n"); |
architmuchhal | 0:94f21066fc43 | 65 | wait(2); |
architmuchhal | 0:94f21066fc43 | 66 | tagname = '\0'; |
architmuchhal | 0:94f21066fc43 | 67 | LedRed= 1; |
architmuchhal | 0:94f21066fc43 | 68 | LedGreen = 1; |
architmuchhal | 0:94f21066fc43 | 69 | |
architmuchhal | 0:94f21066fc43 | 70 | // checking for new cards |
architmuchhal | 0:94f21066fc43 | 71 | if ( ! RfChip.PICC_IsNewCardPresent()) { |
architmuchhal | 0:94f21066fc43 | 72 | wait_ms(100); |
architmuchhal | 0:94f21066fc43 | 73 | continue; |
architmuchhal | 0:94f21066fc43 | 74 | } |
architmuchhal | 0:94f21066fc43 | 75 | |
architmuchhal | 0:94f21066fc43 | 76 | // Selecting one of the cards or key chains |
architmuchhal | 0:94f21066fc43 | 77 | if ( ! RfChip.PICC_ReadCardSerial()) { |
architmuchhal | 0:94f21066fc43 | 78 | wait_ms(1000); |
architmuchhal | 0:94f21066fc43 | 79 | continue; |
architmuchhal | 0:94f21066fc43 | 80 | } |
architmuchhal | 0:94f21066fc43 | 81 | |
architmuchhal | 0:94f21066fc43 | 82 | // When a card is detected, a Green Led flashes! |
architmuchhal | 0:94f21066fc43 | 83 | timeBetweenCards.start(); |
architmuchhal | 0:94f21066fc43 | 84 | LedGreen = 0; |
architmuchhal | 0:94f21066fc43 | 85 | |
architmuchhal | 0:94f21066fc43 | 86 | for (uint8_t i = 0; i < RfChip.uid.size; i++) { |
architmuchhal | 0:94f21066fc43 | 87 | user[i]=RfChip.uid.uidByte[i]; |
architmuchhal | 0:94f21066fc43 | 88 | } |
architmuchhal | 0:94f21066fc43 | 89 | |
architmuchhal | 0:94f21066fc43 | 90 | sprintf(tempo,"%X02%X02%X02%X02",user[0],user[1],user[2],user[3]); |
architmuchhal | 0:94f21066fc43 | 91 | printf("\n\r"); |
architmuchhal | 0:94f21066fc43 | 92 | if (strcmp(tempo, Richard)==0) { |
architmuchhal | 0:94f21066fc43 | 93 | //Thermocouple program |
architmuchhal | 0:94f21066fc43 | 94 | pc.printf("Thermocouple Output:\r\n"); |
architmuchhal | 0:94f21066fc43 | 95 | x=0; |
architmuchhal | 0:94f21066fc43 | 96 | x= check.read_u16(); |
architmuchhal | 0:94f21066fc43 | 97 | x=x/65536; // ADC Scaling |
architmuchhal | 0:94f21066fc43 | 98 | x=x*330; // GPIO Scaling and IC Scaling(*100) |
architmuchhal | 0:94f21066fc43 | 99 | pc.printf("Voltage Difference = %.2f C\n\r",x); |
architmuchhal | 0:94f21066fc43 | 100 | wait(3); |
architmuchhal | 0:94f21066fc43 | 101 | |
architmuchhal | 0:94f21066fc43 | 102 | |
architmuchhal | 0:94f21066fc43 | 103 | //DHT |
architmuchhal | 0:94f21066fc43 | 104 | int err; |
architmuchhal | 0:94f21066fc43 | 105 | printf("\r\nDHT Test program"); |
architmuchhal | 0:94f21066fc43 | 106 | printf("\r\n******************\r\n"); |
architmuchhal | 0:94f21066fc43 | 107 | err = sensor.readData(); |
architmuchhal | 0:94f21066fc43 | 108 | if (err == 0) { |
architmuchhal | 0:94f21066fc43 | 109 | printf("Temperature is %4.2f C \r\n",sensor.ReadTemperature(CELCIUS)); |
architmuchhal | 0:94f21066fc43 | 110 | printf("Temperature is %4.2f F \r\n",sensor.ReadTemperature(FARENHEIT)); |
architmuchhal | 0:94f21066fc43 | 111 | printf("Temperature is %4.2f K \r\n",sensor.ReadTemperature(KELVIN)); |
architmuchhal | 0:94f21066fc43 | 112 | printf("Humidity is %4.2f \r\n",sensor.ReadHumidity()); |
architmuchhal | 0:94f21066fc43 | 113 | printf("Dew point is %4.2f \r\n",sensor.CalcdewPoint(sensor.ReadTemperature(CELCIUS), sensor.ReadHumidity())); |
architmuchhal | 0:94f21066fc43 | 114 | printf("Dew point (fast) is %4.2f \r\n",sensor.CalcdewPointFast(sensor.ReadTemperature(CELCIUS), sensor.ReadHumidity())); |
architmuchhal | 0:94f21066fc43 | 115 | printf("--------------------------------------------\r\n"); |
architmuchhal | 0:94f21066fc43 | 116 | } |
architmuchhal | 0:94f21066fc43 | 117 | wait(2); |
architmuchhal | 0:94f21066fc43 | 118 | |
architmuchhal | 0:94f21066fc43 | 119 | tmp_f = sensor.ReadTemperature(FARENHEIT); |
architmuchhal | 0:94f21066fc43 | 120 | tmp_c = sensor.ReadTemperature(CELCIUS); |
architmuchhal | 0:94f21066fc43 | 121 | hum = sensor.ReadHumidity(); |
architmuchhal | 0:94f21066fc43 | 122 | |
architmuchhal | 0:94f21066fc43 | 123 | esp_send(); |
architmuchhal | 0:94f21066fc43 | 124 | } else if (strcmp(tempo, Richard)!=0) { |
architmuchhal | 0:94f21066fc43 | 125 | printf("NOT AUTHORISED\n\r"); |
architmuchhal | 0:94f21066fc43 | 126 | |
architmuchhal | 0:94f21066fc43 | 127 | } |
architmuchhal | 0:94f21066fc43 | 128 | |
architmuchhal | 0:94f21066fc43 | 129 | } else if(inputPin==0) { |
architmuchhal | 0:94f21066fc43 | 130 | } |
architmuchhal | 0:94f21066fc43 | 131 | wait(5); |
architmuchhal | 0:94f21066fc43 | 132 | } |
architmuchhal | 0:94f21066fc43 | 133 | } |
architmuchhal | 0:94f21066fc43 | 134 | |
architmuchhal | 0:94f21066fc43 | 135 | void esp_initialize(void) |
architmuchhal | 0:94f21066fc43 | 136 | { |
architmuchhal | 0:94f21066fc43 | 137 | pc.printf("Initializing ESP\r\n"); |
architmuchhal | 0:94f21066fc43 | 138 | |
architmuchhal | 0:94f21066fc43 | 139 | pc.printf("Reset ESP\r\n"); |
architmuchhal | 0:94f21066fc43 | 140 | esp.Reset(); //RESET ESP |
architmuchhal | 0:94f21066fc43 | 141 | esp.RcvReply(rcv, 400); //receive a response from ESP |
architmuchhal | 0:94f21066fc43 | 142 | wait(2); |
architmuchhal | 0:94f21066fc43 | 143 | |
architmuchhal | 0:94f21066fc43 | 144 | strcpy(snd,"AT"); |
architmuchhal | 0:94f21066fc43 | 145 | esp.SendCMD(snd); |
architmuchhal | 0:94f21066fc43 | 146 | pc.printf(snd); |
architmuchhal | 0:94f21066fc43 | 147 | esp.RcvReply(rcv, 400); |
architmuchhal | 0:94f21066fc43 | 148 | pc.printf(rcv); |
architmuchhal | 0:94f21066fc43 | 149 | wait(0.1); |
architmuchhal | 0:94f21066fc43 | 150 | |
architmuchhal | 0:94f21066fc43 | 151 | strcpy(snd,"AT+CWMODE=1"); |
architmuchhal | 0:94f21066fc43 | 152 | esp.SendCMD(snd); |
architmuchhal | 0:94f21066fc43 | 153 | pc.printf(snd); |
architmuchhal | 0:94f21066fc43 | 154 | wait(2); |
architmuchhal | 0:94f21066fc43 | 155 | |
architmuchhal | 0:94f21066fc43 | 156 | strcpy(snd,"AT+CWJAP=\""); |
architmuchhal | 0:94f21066fc43 | 157 | strcat(snd,WIFI_SSID); |
architmuchhal | 0:94f21066fc43 | 158 | strcat(snd,"\",\""); |
architmuchhal | 0:94f21066fc43 | 159 | strcat(snd,WIFI_PASS); |
architmuchhal | 0:94f21066fc43 | 160 | strcat(snd,"\""); |
architmuchhal | 0:94f21066fc43 | 161 | |
architmuchhal | 0:94f21066fc43 | 162 | esp.SendCMD(snd); |
architmuchhal | 0:94f21066fc43 | 163 | pc.printf(snd); |
architmuchhal | 0:94f21066fc43 | 164 | wait(5); |
architmuchhal | 0:94f21066fc43 | 165 | esp.RcvReply(rcv, 400); |
architmuchhal | 0:94f21066fc43 | 166 | pc.printf("\n %s \n", rcv); |
architmuchhal | 0:94f21066fc43 | 167 | |
architmuchhal | 0:94f21066fc43 | 168 | strcpy(snd,"AT+CIPMUX=1"); |
architmuchhal | 0:94f21066fc43 | 169 | esp.SendCMD(snd); |
architmuchhal | 0:94f21066fc43 | 170 | pc.printf(snd); |
architmuchhal | 0:94f21066fc43 | 171 | esp.RcvReply(rcv, 400); |
architmuchhal | 0:94f21066fc43 | 172 | pc.printf("\n %s \n", rcv); |
architmuchhal | 0:94f21066fc43 | 173 | |
architmuchhal | 0:94f21066fc43 | 174 | } |
architmuchhal | 0:94f21066fc43 | 175 | |
architmuchhal | 0:94f21066fc43 | 176 | void esp_send(void) |
architmuchhal | 0:94f21066fc43 | 177 | { |
architmuchhal | 0:94f21066fc43 | 178 | //ESP updates the Status of Thingspeak channel// |
architmuchhal | 0:94f21066fc43 | 179 | |
architmuchhal | 0:94f21066fc43 | 180 | strcpy(snd,"AT+CIPSTART="); |
architmuchhal | 0:94f21066fc43 | 181 | strcat(snd,"\"TCP\",\""); |
architmuchhal | 0:94f21066fc43 | 182 | strcat(snd,IP); |
architmuchhal | 0:94f21066fc43 | 183 | strcat(snd,"\",80"); |
architmuchhal | 0:94f21066fc43 | 184 | |
architmuchhal | 0:94f21066fc43 | 185 | esp.SendCMD(snd); |
architmuchhal | 0:94f21066fc43 | 186 | pc.printf("Send\r\n%s",snd); |
architmuchhal | 0:94f21066fc43 | 187 | esp.RcvReply(rcv, 1000); |
architmuchhal | 0:94f21066fc43 | 188 | pc.printf("Receive\r\n%s",rcv); |
architmuchhal | 0:94f21066fc43 | 189 | wait(1); |
architmuchhal | 0:94f21066fc43 | 190 | |
architmuchhal | 0:94f21066fc43 | 191 | sprintf(snd,"GET https://api.thingspeak.com/update?api_key=D9BGPWTNAGUAZPMB&field1=%f&field2=%f&field3=%f&field4=%.2f\r\n",tmp_f,tmp_c,hum,x); |
architmuchhal | 0:94f21066fc43 | 192 | |
architmuchhal | 0:94f21066fc43 | 193 | int i=0; |
architmuchhal | 0:94f21066fc43 | 194 | for(i=0; snd[i]!='\0'; i++); |
architmuchhal | 0:94f21066fc43 | 195 | i++; |
architmuchhal | 0:94f21066fc43 | 196 | char cmd[255]; |
architmuchhal | 0:94f21066fc43 | 197 | |
architmuchhal | 0:94f21066fc43 | 198 | sprintf(cmd,"AT+CIPSEND=%d",i); //Send Number of open connection and Characters to send |
architmuchhal | 0:94f21066fc43 | 199 | esp.SendCMD(cmd); |
architmuchhal | 0:94f21066fc43 | 200 | pc.printf("Send\r\n%s",cmd); |
architmuchhal | 0:94f21066fc43 | 201 | while(i<=20 || rcv == ">") { |
architmuchhal | 0:94f21066fc43 | 202 | esp.RcvReply(rcv, 1000); |
architmuchhal | 0:94f21066fc43 | 203 | wait(100); |
architmuchhal | 0:94f21066fc43 | 204 | i++; |
architmuchhal | 0:94f21066fc43 | 205 | } |
architmuchhal | 0:94f21066fc43 | 206 | pc.printf("Receive\r\n%s",rcv); |
architmuchhal | 0:94f21066fc43 | 207 | |
architmuchhal | 0:94f21066fc43 | 208 | esp.SendCMD(snd); //Post value to thingspeak channel |
architmuchhal | 0:94f21066fc43 | 209 | pc.printf("Send\r\n%s",snd); |
architmuchhal | 0:94f21066fc43 | 210 | |
architmuchhal | 0:94f21066fc43 | 211 | while(i<=20 || rcv == "OK") { |
architmuchhal | 0:94f21066fc43 | 212 | esp.RcvReply(rcv, 1000); |
architmuchhal | 0:94f21066fc43 | 213 | wait(100); |
architmuchhal | 0:94f21066fc43 | 214 | i++; |
architmuchhal | 0:94f21066fc43 | 215 | } |
architmuchhal | 0:94f21066fc43 | 216 | pc.printf("Receive\r\n%s",rcv); |
architmuchhal | 0:94f21066fc43 | 217 | } |
architmuchhal | 0:94f21066fc43 | 218 |