Simple code for multiple sensor test using K64f

Dependencies:   mbed MQ2 FXOS8700Q DHT TextLCD ESP8266

Committer:
siddharthp
Date:
Tue Nov 29 13:56:18 2016 +0000
Revision:
0:da7e87322045
Child:
1:402b14b56d24
Temperature and humidity sensor using FRDM-K64F and Thingspeak

Who changed what in which revision?

UserRevisionLine numberNew contents of line
siddharthp 0:da7e87322045 1 #include "mbed.h"
siddharthp 0:da7e87322045 2 #include "DHT.h"
siddharthp 0:da7e87322045 3 #include "ESP8266.h"
siddharthp 0:da7e87322045 4
siddharthp 0:da7e87322045 5 DHT sensor(D4, DHT11);
siddharthp 0:da7e87322045 6 ESP8266 wifi(PTC17, PTC16, 115200);
siddharthp 0:da7e87322045 7 Serial pc(USBTX,USBRX);
siddharthp 0:da7e87322045 8 DigitalOut RED(LED1);
siddharthp 0:da7e87322045 9 char snd[255],rcv[1000];
siddharthp 0:da7e87322045 10 #define IP "184.106.153.149" // thingspeak.com IP Address
siddharthp 0:da7e87322045 11 void wifi_send(void);
siddharthp 0:da7e87322045 12
siddharthp 0:da7e87322045 13 int main()
siddharthp 0:da7e87322045 14 {
siddharthp 0:da7e87322045 15 pc.baud(115200);
siddharthp 0:da7e87322045 16 pc.printf("SET mode to AP\r\n");
siddharthp 0:da7e87322045 17 wifi.SetMode(1); // set ESP mode to 1
siddharthp 0:da7e87322045 18 wifi.RcvReply(rcv, 1000); //receive a response from ESP
siddharthp 0:da7e87322045 19 pc.printf("%s",rcv); //Print the response onscreen
siddharthp 0:da7e87322045 20 pc.printf("Conneting to Wifi\r\n");
siddharthp 0:da7e87322045 21 wifi.Join("533B", "ransom007"); // Your wifi username & Password
siddharthp 0:da7e87322045 22 wifi.RcvReply(rcv, 1000); //receive a response from ESP
siddharthp 0:da7e87322045 23 pc.printf("%s\n", rcv); //Print the response onscreen
siddharthp 0:da7e87322045 24 wait(8); //waits for response from ESP
siddharthp 0:da7e87322045 25 pc.printf("Getting IP\r\n"); //get IP addresss from the connected AP
siddharthp 0:da7e87322045 26 wifi.GetIP(rcv); //receive an IP address from the AP
siddharthp 0:da7e87322045 27 pc.printf("%s\n", rcv);
siddharthp 0:da7e87322045 28 wait(5); // Delay 5 sec to give the pir time to get snapshut of the surrounding
siddharthp 0:da7e87322045 29 while (1)
siddharthp 0:da7e87322045 30 {
siddharthp 0:da7e87322045 31 pc.printf("PLEASE STAY AWAY\r\n");
siddharthp 0:da7e87322045 32 pc.printf("Sending WiFi information\n");
siddharthp 0:da7e87322045 33 wifi_send();
siddharthp 0:da7e87322045 34 RED=1; // when the motion detected turn of the on board red led
siddharthp 0:da7e87322045 35 wait(2.0f);
siddharthp 0:da7e87322045 36 RED=1;
siddharthp 0:da7e87322045 37 wait(1.5f);
siddharthp 0:da7e87322045 38 }
siddharthp 0:da7e87322045 39 }
siddharthp 0:da7e87322045 40 void wifi_send(void){
siddharthp 0:da7e87322045 41 int error = 0;
siddharthp 0:da7e87322045 42 float h = 0.0f, c = 0.0f;
siddharthp 0:da7e87322045 43
siddharthp 0:da7e87322045 44 wait(2.0f);
siddharthp 0:da7e87322045 45 error = sensor.readData();
siddharthp 0:da7e87322045 46 if (0 == error)
siddharthp 0:da7e87322045 47 {
siddharthp 0:da7e87322045 48 c = sensor.ReadTemperature(CELCIUS);
siddharthp 0:da7e87322045 49 h = sensor.ReadHumidity();
siddharthp 0:da7e87322045 50 //printf("Temperature in Celcius: %f\n", c);
siddharthp 0:da7e87322045 51 //printf("Humidity is %f\n", h);
siddharthp 0:da7e87322045 52 }
siddharthp 0:da7e87322045 53 else
siddharthp 0:da7e87322045 54 {
siddharthp 0:da7e87322045 55 printf("Error: %d\n", error);
siddharthp 0:da7e87322045 56 }
siddharthp 0:da7e87322045 57 //WIFI updates the Status to Thingspeak servers//
siddharthp 0:da7e87322045 58 strcpy(snd,"AT+CIPMUX=1\n");//Setting WiFi into MultiChannel mode
siddharthp 0:da7e87322045 59 wifi.SendCMD(snd);
siddharthp 0:da7e87322045 60 pc.printf(snd);
siddharthp 0:da7e87322045 61 wait(2.0);
siddharthp 0:da7e87322045 62 wifi.RcvReply(rcv, 1000);
siddharthp 0:da7e87322045 63 pc.printf("%s\n", rcv);
siddharthp 0:da7e87322045 64 wait(2);
siddharthp 0:da7e87322045 65 sprintf(snd,"AT+CIPSTART=4,\"TCP\",\"%s\",80\n",IP); //Initiate connection with THINGSPEAK server
siddharthp 0:da7e87322045 66 pc.printf(snd);
siddharthp 0:da7e87322045 67 wait(3.0);
siddharthp 0:da7e87322045 68 wifi.RcvReply(rcv, 1000);
siddharthp 0:da7e87322045 69 pc.printf("%s\n", rcv);
siddharthp 0:da7e87322045 70 wait(2);
siddharthp 0:da7e87322045 71 strcpy(snd,"AT+CIPSEND=4,47\n"); //Send Number of open connections,Characters to send
siddharthp 0:da7e87322045 72 wifi.SendCMD(snd);
siddharthp 0:da7e87322045 73 pc.printf(snd);
siddharthp 0:da7e87322045 74 wait(2.0);
siddharthp 0:da7e87322045 75 wifi.RcvReply(rcv, 1000);
siddharthp 0:da7e87322045 76 pc.printf("%s\n", rcv);
siddharthp 0:da7e87322045 77 wait(2);
siddharthp 0:da7e87322045 78 sprintf(snd,"GET http://api.thingspeak.com/update?api_key=NVG5E6ZAB970OMRF&field1=%1.3f\n", c); //Post values to thingspeak
siddharthp 0:da7e87322045 79 pc.printf("%s",snd);
siddharthp 0:da7e87322045 80 wifi.SendCMD(snd);
siddharthp 0:da7e87322045 81 wait(2);
siddharthp 0:da7e87322045 82 sprintf(snd,"GET http://api.thingspeak.com/update?api_key=NVG5E6ZAB970OMRF&field2=%1.3f\n", h); //Post values to thingspeak
siddharthp 0:da7e87322045 83 pc.printf("%s",snd);
siddharthp 0:da7e87322045 84 wifi.SendCMD(snd);
siddharthp 0:da7e87322045 85 wait(2);
siddharthp 0:da7e87322045 86 wifi.RcvReply(rcv, 1000);
siddharthp 0:da7e87322045 87 pc.printf("%s", rcv);
siddharthp 0:da7e87322045 88 wifi.SendCMD("AT+CIPCLOSE"); //Close the connection to server
siddharthp 0:da7e87322045 89 wifi.RcvReply(rcv, 1000);
siddharthp 0:da7e87322045 90 pc.printf("%s", rcv);
siddharthp 0:da7e87322045 91 }