Motion activated temperature and humidity sensor is a project designed to implement the functionalities that FRDM K64F developmental board and software developmental platform offers on mbed complier. Such as: Peripherals that are enabled for prototyping different headers and functionalities.Bluetooth; Wi-Fi module (ESP 8266) which supports connecting the board to cloud storage; PIR, electronic sensor for detecting motion, sensor are incorporated to build the project. The communication of peripherals with the board is implemented on serial communication and ports configured for UART.

Dependencies:   DHT ESP8266 MFRC522 mbed

Committer:
ndgohil
Date:
Thu Apr 28 16:41:40 2016 +0000
Revision:
0:17278ea5a8b5
Motion Activated Temperature_Humidity Sensor Using ESP8266 and HC-06 Module

Who changed what in which revision?

UserRevisionLine numberNew contents of line
ndgohil 0:17278ea5a8b5 1 /*
ndgohil 0:17278ea5a8b5 2 *******************************************************************************************
ndgohil 0:17278ea5a8b5 3 * Motion activated temprature and humidity measuring using humidity and PIR sensor.
ndgohil 0:17278ea5a8b5 4 * while measuring it will notify the sourunding by blikinking LED and making buzzer sound.
ndgohil 0:17278ea5a8b5 5 * The measuured data is sent to Thingsspeak cloud server
ndgohil 0:17278ea5a8b5 6 *--PIR Motion Sensor Alarm + ESP8622 WiFi Module + FRDM-k64f
ndgohil 0:17278ea5a8b5 7 * --PIR sensor can detect change on its surrounding by measuring the change of infra red
ndgohil 0:17278ea5a8b5 8 * --Then will send high=1 to the FRDM-k64f to flash the LED, Run the Buzzer,
ndgohil 0:17278ea5a8b5 9 * --Send information through UART serial Port to Tera Term and display how many motion been
ndgohil 0:17278ea5a8b5 10 *---detected,also will send information to Thingspeak IoT website using WiFi connection
ndgohil 0:17278ea5a8b5 11 ********************************************************************************************
ndgohil 0:17278ea5a8b5 12 */
ndgohil 0:17278ea5a8b5 13
ndgohil 0:17278ea5a8b5 14 #include "mbed.h"
ndgohil 0:17278ea5a8b5 15 #include "DHT.h"
ndgohil 0:17278ea5a8b5 16 #include "ESP8266.h"
ndgohil 0:17278ea5a8b5 17 #include "MFRC522.h"
ndgohil 0:17278ea5a8b5 18
ndgohil 0:17278ea5a8b5 19
ndgohil 0:17278ea5a8b5 20 #define APIKEY 7PPRUQANXEJ0UURH
ndgohil 0:17278ea5a8b5 21 #define IP "184.106.153.149" // thingspeak.com IP Address
ndgohil 0:17278ea5a8b5 22 DHT sensor(D4, DHT11);
ndgohil 0:17278ea5a8b5 23
ndgohil 0:17278ea5a8b5 24 void printer_contractor( );
ndgohil 0:17278ea5a8b5 25 float timprature_humidity_contractor();
ndgohil 0:17278ea5a8b5 26 DigitalOut outside_green_led(D13);
ndgohil 0:17278ea5a8b5 27 Serial blue(PTC15, PTC14);
ndgohil 0:17278ea5a8b5 28 DigitalOut ledgreen(LED_GREEN);
ndgohil 0:17278ea5a8b5 29 DigitalOut ledred(LED_RED);
ndgohil 0:17278ea5a8b5 30 Serial pc(USBTX,USBRX);
ndgohil 0:17278ea5a8b5 31 DigitalOut Buzzer(D10); // output
ndgohil 0:17278ea5a8b5 32 PwmOut LED(D13); // flashing the led
ndgohil 0:17278ea5a8b5 33 DigitalIn inputPin(D2); // pir senor input
ndgohil 0:17278ea5a8b5 34 DigitalOut greenled(LED2);
ndgohil 0:17278ea5a8b5 35 ESP8266 wifi(PTC17, PTC16, 9600); // baud rate for wifi
ndgohil 0:17278ea5a8b5 36
ndgohil 0:17278ea5a8b5 37
ndgohil 0:17278ea5a8b5 38 char snd[255],rcv[1000];
ndgohil 0:17278ea5a8b5 39 int myclock=0; //( myclock=0; myclock < max_clock; myclock ++ )
ndgohil 0:17278ea5a8b5 40 int max_clock=20;
ndgohil 0:17278ea5a8b5 41 int val = 0; // value to holed the high/low info from pir from pin D2
ndgohil 0:17278ea5a8b5 42 int cnt = 0; // counter for motion
ndgohil 0:17278ea5a8b5 43 //String thingtweetAPIKey = "TTEVLP931ODJ5GMT";
ndgohil 0:17278ea5a8b5 44
ndgohil 0:17278ea5a8b5 45 /************ WiFi INTIALIZATION *********/
ndgohil 0:17278ea5a8b5 46 void printer_contractor( );
ndgohil 0:17278ea5a8b5 47 float timprature_humidity_contractor();
ndgohil 0:17278ea5a8b5 48 void wifi_initialize();
ndgohil 0:17278ea5a8b5 49 void wifi_send();
ndgohil 0:17278ea5a8b5 50
ndgohil 0:17278ea5a8b5 51 int main()
ndgohil 0:17278ea5a8b5 52 {
ndgohil 0:17278ea5a8b5 53 blue.baud(9600);
ndgohil 0:17278ea5a8b5 54
ndgohil 0:17278ea5a8b5 55 while(1)
ndgohil 0:17278ea5a8b5 56
ndgohil 0:17278ea5a8b5 57 { // calling functions for diffrent task
ndgohil 0:17278ea5a8b5 58
ndgohil 0:17278ea5a8b5 59 printer_contractor(); // calling the function that print message for the process.
ndgohil 0:17278ea5a8b5 60
ndgohil 0:17278ea5a8b5 61 wifi_initialize(); //
ndgohil 0:17278ea5a8b5 62 wifi_send(); //
ndgohil 0:17278ea5a8b5 63
ndgohil 0:17278ea5a8b5 64
ndgohil 0:17278ea5a8b5 65
ndgohil 0:17278ea5a8b5 66 }
ndgohil 0:17278ea5a8b5 67
ndgohil 0:17278ea5a8b5 68
ndgohil 0:17278ea5a8b5 69
ndgohil 0:17278ea5a8b5 70
ndgohil 0:17278ea5a8b5 71 }
ndgohil 0:17278ea5a8b5 72
ndgohil 0:17278ea5a8b5 73
ndgohil 0:17278ea5a8b5 74 void printer_contractor()
ndgohil 0:17278ea5a8b5 75 { outside_green_led = 1; // toggle pin
ndgohil 0:17278ea5a8b5 76 ledred= 1; // toggle led
ndgohil 0:17278ea5a8b5 77 LED=1;
ndgohil 0:17278ea5a8b5 78 Buzzer=1;
ndgohil 0:17278ea5a8b5 79 printf(" Motion activated TEMPRATURE and HUMIDITY measurement device: \n");
ndgohil 0:17278ea5a8b5 80 blue.printf(" Motion activated TEMPRATURE and HUMIDITY measurement device: \n");
ndgohil 0:17278ea5a8b5 81 printf("\t :Sending IMPOTRTANT information to AGENT- CLOUD \n ");
ndgohil 0:17278ea5a8b5 82 blue.printf("\t :Sending IMPOTRTANT information to AGENT- CLOUD \n ");
ndgohil 0:17278ea5a8b5 83 printf("\t :tmprature & humidity testing begin wait... \n ");
ndgohil 0:17278ea5a8b5 84 blue.printf("\t :tmprature & humidity testing begin wait... \n ");
ndgohil 0:17278ea5a8b5 85 wait(3);
ndgohil 0:17278ea5a8b5 86 }
ndgohil 0:17278ea5a8b5 87
ndgohil 0:17278ea5a8b5 88
ndgohil 0:17278ea5a8b5 89
ndgohil 0:17278ea5a8b5 90
ndgohil 0:17278ea5a8b5 91 void wifi_initialize()
ndgohil 0:17278ea5a8b5 92
ndgohil 0:17278ea5a8b5 93 {
ndgohil 0:17278ea5a8b5 94
ndgohil 0:17278ea5a8b5 95 pc.baud(9600);
ndgohil 0:17278ea5a8b5 96 pc.printf("SET mode to AP\r\n");
ndgohil 0:17278ea5a8b5 97 blue.printf("SET mode to AP\r\n");
ndgohil 0:17278ea5a8b5 98 wifi.SetMode(1); // set ESP mode to 1
ndgohil 0:17278ea5a8b5 99 wifi.RcvReply(rcv, 1000); //receive a response from ESP
ndgohil 0:17278ea5a8b5 100 pc.printf("%s",rcv); //Print the response onscreen
ndgohil 0:17278ea5a8b5 101 blue.printf("%s",rcv);
ndgohil 0:17278ea5a8b5 102 pc.printf("Conneting to AP\r\n");
ndgohil 0:17278ea5a8b5 103 blue.printf("Conneting to AP\r\n");
ndgohil 0:17278ea5a8b5 104 wifi.Join("Network", "Password"); // Your wifi username & Password
ndgohil 0:17278ea5a8b5 105 wifi.RcvReply(rcv, 1000); //receive a response from ESP
ndgohil 0:17278ea5a8b5 106 pc.printf("%s", rcv); //Print the response onscreen
ndgohil 0:17278ea5a8b5 107 blue.printf("%s",rcv);
ndgohil 0:17278ea5a8b5 108 wait(5); //waits for response from ESP
ndgohil 0:17278ea5a8b5 109 pc.printf("Getting IP\r\n"); //get IP addresss from the connected AP
ndgohil 0:17278ea5a8b5 110 blue.printf("Getting IP\r\n");
ndgohil 0:17278ea5a8b5 111 wifi.GetIP(rcv); //receive an IP address from the AP
ndgohil 0:17278ea5a8b5 112 pc.printf("%s", rcv);
ndgohil 0:17278ea5a8b5 113 blue.printf("%s", rcv);
ndgohil 0:17278ea5a8b5 114 wait(5); // Delay 5 sec to give the pir time to get snapshut of the surrounding
ndgohil 0:17278ea5a8b5 115 pc.printf(" WAIT:Initializing WiFi\r\n");
ndgohil 0:17278ea5a8b5 116 blue.printf(" WAIT:Initializing WiFi\r\n");
ndgohil 0:17278ea5a8b5 117 while (1)
ndgohil 0:17278ea5a8b5 118 {
ndgohil 0:17278ea5a8b5 119
ndgohil 0:17278ea5a8b5 120 val = inputPin.read();
ndgohil 0:17278ea5a8b5 121
ndgohil 0:17278ea5a8b5 122 if (val==0) {
ndgohil 0:17278ea5a8b5 123 cnt++;
ndgohil 0:17278ea5a8b5 124 pc.printf(" The Sensor is ON: %i Motion Detected NOW \r\n\n",cnt);
ndgohil 0:17278ea5a8b5 125 blue.printf(" The Sensor is ON: %i Motion Detected NOW \r\n\n",cnt);
ndgohil 0:17278ea5a8b5 126
ndgohil 0:17278ea5a8b5 127 printf(" NOW lets messure Temprature and humudity of the enviroment:\n\n\n\n");
ndgohil 0:17278ea5a8b5 128 blue.printf(" NOW lets messure Temprature and humudity of the enviroment:\n\n\n\n");
ndgohil 0:17278ea5a8b5 129 timprature_humidity_contractor();// measruing the temprature and humidity.
ndgohil 0:17278ea5a8b5 130 pc.printf("Sending WiFi information to \n");
ndgohil 0:17278ea5a8b5 131 blue.printf("Sending WiFi information to \n");
ndgohil 0:17278ea5a8b5 132 wifi_send();
ndgohil 0:17278ea5a8b5 133 greenled=1; // when the motion detected turn of the on board red led
ndgohil 0:17278ea5a8b5 134 LED.period(2); // 2 seconds period
ndgohil 0:17278ea5a8b5 135 wait(2);
ndgohil 0:17278ea5a8b5 136 LED.pulsewidth(.02); // 2 mseconds pulse (on)
ndgohil 0:17278ea5a8b5 137 greenled=1;
ndgohil 0:17278ea5a8b5 138 Buzzer = 1;
ndgohil 0:17278ea5a8b5 139 wait(2);
ndgohil 0:17278ea5a8b5 140
ndgohil 0:17278ea5a8b5 141
ndgohil 0:17278ea5a8b5 142 }
ndgohil 0:17278ea5a8b5 143
ndgohil 0:17278ea5a8b5 144 else {
ndgohil 0:17278ea5a8b5 145
ndgohil 0:17278ea5a8b5 146 pc.printf(" The Sensor is OFF \r\n");
ndgohil 0:17278ea5a8b5 147 blue.printf(" The Sensor is OFF \r\n");
ndgohil 0:17278ea5a8b5 148 LED = 0;
ndgohil 0:17278ea5a8b5 149 Buzzer =0;
ndgohil 0:17278ea5a8b5 150 greenled=0; // turn the on board red led on
ndgohil 0:17278ea5a8b5 151 wait(2);
ndgohil 0:17278ea5a8b5 152 }
ndgohil 0:17278ea5a8b5 153 }
ndgohil 0:17278ea5a8b5 154 }
ndgohil 0:17278ea5a8b5 155
ndgohil 0:17278ea5a8b5 156 void wifi_send()
ndgohil 0:17278ea5a8b5 157 {
ndgohil 0:17278ea5a8b5 158
ndgohil 0:17278ea5a8b5 159 //WIFI updates the Status to Thingspeak servers//
ndgohil 0:17278ea5a8b5 160 strcpy(snd,"AT+CIPMUX=1");//Setting WiFi into MultiChannel mode
ndgohil 0:17278ea5a8b5 161 wifi.SendCMD(snd);
ndgohil 0:17278ea5a8b5 162 pc.printf(snd);
ndgohil 0:17278ea5a8b5 163 blue.printf(snd);
ndgohil 0:17278ea5a8b5 164 wait(2);
ndgohil 0:17278ea5a8b5 165 wifi.RcvReply(rcv, 1000);
ndgohil 0:17278ea5a8b5 166 pc.printf("%s", rcv);
ndgohil 0:17278ea5a8b5 167 blue.printf("%s", rcv);
ndgohil 0:17278ea5a8b5 168 wait(2);
ndgohil 0:17278ea5a8b5 169 sprintf(snd,"AT+CIPSTART=4,\"TCP\",\"%s\",80",IP); //Initiate connection with THINGSPEAK server
ndgohil 0:17278ea5a8b5 170 pc.printf(snd);
ndgohil 0:17278ea5a8b5 171 blue.printf(snd);
ndgohil 0:17278ea5a8b5 172 wait(3);
ndgohil 0:17278ea5a8b5 173 wifi.RcvReply(rcv, 1000);
ndgohil 0:17278ea5a8b5 174 pc.printf("%s", rcv);
ndgohil 0:17278ea5a8b5 175 blue.printf("%s", rcv);
ndgohil 0:17278ea5a8b5 176 wait(2);
ndgohil 0:17278ea5a8b5 177 strcpy(snd,"AT+CIPSEND=4,47"); //Send Number of open connections,Characters to send
ndgohil 0:17278ea5a8b5 178 wifi.SendCMD(snd);
ndgohil 0:17278ea5a8b5 179 pc.printf(snd);
ndgohil 0:17278ea5a8b5 180 blue.printf(snd);
ndgohil 0:17278ea5a8b5 181 wait(2.0);
ndgohil 0:17278ea5a8b5 182 wifi.RcvReply(rcv, 1000);
ndgohil 0:17278ea5a8b5 183 wait(2);
ndgohil 0:17278ea5a8b5 184 pc.printf("%s", rcv);
ndgohil 0:17278ea5a8b5 185 blue.printf("%s", rcv);
ndgohil 0:17278ea5a8b5 186 wait(2);
ndgohil 0:17278ea5a8b5 187 pc.printf("%s",snd);
ndgohil 0:17278ea5a8b5 188 blue.printf("%s",snd);
ndgohil 0:17278ea5a8b5 189 sprintf(snd,"GET https://api.thingspeak.com/update?key=7PPRUQANXEJ0UURH&feild1=2 HTTP/1.0 \r\n\r\n"); //post it
ndgohil 0:17278ea5a8b5 190 pc.printf("%s",snd);
ndgohil 0:17278ea5a8b5 191 blue.printf("%s",snd);
ndgohil 0:17278ea5a8b5 192 wifi.SendCMD(snd);
ndgohil 0:17278ea5a8b5 193 wait(3);
ndgohil 0:17278ea5a8b5 194 wifi.RcvReply(rcv, 1000);
ndgohil 0:17278ea5a8b5 195 pc.printf("%s", rcv);
ndgohil 0:17278ea5a8b5 196 blue.printf("%s", rcv);
ndgohil 0:17278ea5a8b5 197 wifi.SendCMD("AT+CIPCLOSE"); //Close the connection to server
ndgohil 0:17278ea5a8b5 198 wifi.RcvReply(rcv, 1000);
ndgohil 0:17278ea5a8b5 199 pc.printf("%s", rcv);
ndgohil 0:17278ea5a8b5 200 blue.printf("%s", rcv);
ndgohil 0:17278ea5a8b5 201 }
ndgohil 0:17278ea5a8b5 202
ndgohil 0:17278ea5a8b5 203 float timprature_humidity_contractor()
ndgohil 0:17278ea5a8b5 204 {
ndgohil 0:17278ea5a8b5 205 int error = 0;
ndgohil 0:17278ea5a8b5 206 float h =0 , c =0, f = 0 , k = 0, dp = 0, dpf = 0; // clear sensor variable for new data place
ndgohil 0:17278ea5a8b5 207
ndgohil 0:17278ea5a8b5 208 for ( myclock=0; myclock < max_clock; myclock ++ ) //while(1)
ndgohil 0:17278ea5a8b5 209 {
ndgohil 0:17278ea5a8b5 210 wait(2);
ndgohil 0:17278ea5a8b5 211 error = sensor.readData();
ndgohil 0:17278ea5a8b5 212 //
ndgohil 0:17278ea5a8b5 213 if (error ==0 ) {
ndgohil 0:17278ea5a8b5 214 ledred=0;
ndgohil 0:17278ea5a8b5 215 LED=1;
ndgohil 0:17278ea5a8b5 216
ndgohil 0:17278ea5a8b5 217 // censior variales are assigned from the sensoor reading
ndgohil 0:17278ea5a8b5 218 // and after the reading celcius to farenheit equvallent qalqucaltion.
ndgohil 0:17278ea5a8b5 219
ndgohil 0:17278ea5a8b5 220 c = sensor.ReadTemperature(CELCIUS);
ndgohil 0:17278ea5a8b5 221 f = sensor.ReadTemperature(FARENHEIT);
ndgohil 0:17278ea5a8b5 222 k = sensor.ReadTemperature(KELVIN);
ndgohil 0:17278ea5a8b5 223 h = sensor.ReadHumidity();
ndgohil 0:17278ea5a8b5 224 dp = sensor.CalcdewPoint(c, h);
ndgohil 0:17278ea5a8b5 225 dpf = sensor.CalcdewPointFast(c, h);
ndgohil 0:17278ea5a8b5 226
ndgohil 0:17278ea5a8b5 227 outside_green_led = 1; // toggle pin
ndgohil 0:17278ea5a8b5 228 ledgreen = 1;//ledgreen=1;
ndgohil 0:17278ea5a8b5 229 wait(2);
ndgohil 0:17278ea5a8b5 230
ndgohil 0:17278ea5a8b5 231 printf(" \n\n AM2 302 tmp/Hmd sensro device is giving with:%d precsion \n", error);
ndgohil 0:17278ea5a8b5 232 printf(" ===========HERE IS THE DATA YOU MAY WANT============\n " );
ndgohil 0:17278ea5a8b5 233
ndgohil 0:17278ea5a8b5 234 printf("Temperature in Kelvin: %5.2f \n", k );
ndgohil 0:17278ea5a8b5 235 printf("Temprature in Celcius: %5.2f \n", c );
ndgohil 0:17278ea5a8b5 236 printf("Temprature in Farenheit %5.2f \n", f);
ndgohil 0:17278ea5a8b5 237 printf("Humidity is : %6.2f \n",h);
ndgohil 0:17278ea5a8b5 238 printf("Dew Point is : %6.2f \n",dp);
ndgohil 0:17278ea5a8b5 239 printf("Dew Pointvfastv is %6.2f \n ",dpf);
ndgohil 0:17278ea5a8b5 240 printf("--wait for the NEXT reading please.\n\n\n\n\n\n");
ndgohil 0:17278ea5a8b5 241 //*********************blue
ndgohil 0:17278ea5a8b5 242 blue.printf(" \n\n AM2 302 tmp/Hmd sensro device is giving with:%d precsion \n", error);
ndgohil 0:17278ea5a8b5 243 blue.printf(" ===========HERE IS THE DATA YOU MAY WANT============\n " );
ndgohil 0:17278ea5a8b5 244
ndgohil 0:17278ea5a8b5 245 blue.printf("Temperature in Kelvin: %5.2f \n", k );
ndgohil 0:17278ea5a8b5 246 blue.printf("Temprature in Celcius: %5.2f \n", c );
ndgohil 0:17278ea5a8b5 247 blue.printf("Temprature in Farenheit %5.2f \n", f);
ndgohil 0:17278ea5a8b5 248 blue.printf("Humidity is : %6.2f \n",h);
ndgohil 0:17278ea5a8b5 249 blue.printf("Dew Point is : %6.2f \n",dp);
ndgohil 0:17278ea5a8b5 250 blue.printf("Dew Pointvfastv is %6.2f \n ",dpf);
ndgohil 0:17278ea5a8b5 251 blue.printf("...wait for the NEXT reading please.\n\n\n\n\n\n");
ndgohil 0:17278ea5a8b5 252
ndgohil 0:17278ea5a8b5 253
ndgohil 0:17278ea5a8b5 254
ndgohil 0:17278ea5a8b5 255 }
ndgohil 0:17278ea5a8b5 256 else {
ndgohil 0:17278ea5a8b5 257 ledgreen=0;
ndgohil 0:17278ea5a8b5 258 ledred=1;// ledred = 1; // toggle led
ndgohil 0:17278ea5a8b5 259 wait(2);
ndgohil 0:17278ea5a8b5 260 printf(" Error value ( offset measured) is : %d\n", error);
ndgohil 0:17278ea5a8b5 261 printf(" wait: Device is calbrating for good reading \n\n\n\n\n");
ndgohil 0:17278ea5a8b5 262 printf("*****************************\n");
ndgohil 0:17278ea5a8b5 263 //********************blue
ndgohil 0:17278ea5a8b5 264 blue.printf(" Error value ( offset measured) is : %d\n", error);
ndgohil 0:17278ea5a8b5 265 blue.printf(" wait: Device is calbrating for good reading \n\n\n\n\n");
ndgohil 0:17278ea5a8b5 266 blue.printf("*****************************\n");
ndgohil 0:17278ea5a8b5 267
ndgohil 0:17278ea5a8b5 268
ndgohil 0:17278ea5a8b5 269 }
ndgohil 0:17278ea5a8b5 270 }
ndgohil 0:17278ea5a8b5 271 return c;
ndgohil 0:17278ea5a8b5 272 // dpf;
ndgohil 0:17278ea5a8b5 273 //return dp;
ndgohil 0:17278ea5a8b5 274 }
ndgohil 0:17278ea5a8b5 275