Sending data to thingspeak

Dependencies:   DHT ESP8266 mbed

Fork of temp_hum by Siddharth Pimprikar

Revision:
5:0d98bbc39c7b
Parent:
4:aa3e25ccc05d
Child:
6:fd279a17f523
--- a/main.cpp	Sun Dec 04 21:21:05 2016 +0000
+++ b/main.cpp	Sun Apr 02 13:37:24 2017 +0000
@@ -1,12 +1,15 @@
 #include "mbed.h"
-#include "DHT.h"
-#include "ESP8266.h"
- 
-DHT sensor(D4, DHT11);
+#include "ESP8266.h" 
 ESP8266 wifi(PTC17, PTC16, 115200);
 Serial pc(USBTX,USBRX);
 DigitalOut RED(LED1);
 char snd[255],rcv[1000];
+float cur = 0.0f;//current value
+float comp = 0.0f;//compressed air flow
+float temp = 0.0f;//temperature
+float hum = 0.0f;// humidity
+float co = 0.0f;// CO2 
+float pres = 0.0f;//pressure
 #define IP "184.106.153.149" // thingspeak.com IP Address
 void wifi_send(void);
 
@@ -18,73 +21,50 @@
     wifi.RcvReply(rcv, 1000);    //receive a response from ESP
     pc.printf("%s",rcv);    //Print the response onscreen
     pc.printf("Conneting to Wifi\r\n");
-    wifi.Join("ssid", "password");     // Your wifi username & Password 
+    wifi.Join("SSID", "password");     // Your wifi username & Password 
     wifi.RcvReply(rcv, 1000);    //receive a response from ESP
     pc.printf("%s\n", rcv);    //Print the response onscreen
     wait(8);     //waits for response from ESP
     pc.printf("Getting IP\r\n");    //get IP addresss from the connected AP
     wifi.GetIP(rcv);    //receive an IP address from the AP
     pc.printf("%s\n", rcv);
+    //WIFI updates the Status to Thingspeak servers//
+    pc.printf("PLEASE STAY AWAY\r\n");
+    pc.printf("Sending WiFi information\r\n");
+    strcpy(snd,"AT+CIPMUX=1\r\n");//Setting WiFi into MultiChannel mode
+    wifi.SendCMD(snd);
+    pc.printf(snd);
+    wait(2.0);
+    wifi.RcvReply(rcv, 1000);
+    pc.printf("%s\n", rcv);
+    wait(2);
+    sprintf(snd,"AT+CIPSTART=4,\"TCP\",\"%s\",80\r\n",IP); //Initiate connection with THINGSPEAK server 
+    pc.printf(snd);
+    wait(3.0);
+    wifi.RcvReply(rcv, 1000);
+    pc.printf("%s\n", rcv);
+    wait(2);
+    strcpy(snd,"AT+CIPSEND=4,100\r\n");    //Send Number of open connections,Characters to send 
+    wifi.SendCMD(snd);
+    pc.printf(snd);
+    wait(2.0);
+    wifi.RcvReply(rcv, 1000);
+    pc.printf("%s\n", rcv);
+    wait(2);
     while (1) 
     {
-        pc.printf("PLEASE STAY AWAY\r\n");
-        pc.printf("Sending WiFi information\n");
         wifi_send();
         RED=1;
-        wait(2.0f);
+        wait(10);
         RED=0;
-        wait(1.5f);
+        wait(10);
     }    
 }
-void wifi_send(void){
-    int error = 0;
-    float h = 0.0f, c = 0.0f;
-        
-        wait(2.0f);
-        error = sensor.readData();
-        if (0 == error) 
-        {
-            c   = sensor.ReadTemperature(CELCIUS);
-            h   = sensor.ReadHumidity();
-            //printf("Temperature in Celcius: %f\n", c);
-            //printf("Humidity is %f\n", h);
-        } 
-        else 
-        {
-            printf("Error: %d\n", error);
-        }
-  //WIFI updates the Status to Thingspeak servers//
-  strcpy(snd,"AT+CIPMUX=1\n");//Setting WiFi into MultiChannel mode
-  wifi.SendCMD(snd);
-  pc.printf(snd);
-  wait(2.0);
-  wifi.RcvReply(rcv, 1000);
-  pc.printf("%s\n", rcv);
-  wait(2);
-  sprintf(snd,"AT+CIPSTART=4,\"TCP\",\"%s\",80\n",IP); //Initiate connection with THINGSPEAK server 
-  pc.printf(snd);
-  wait(3.0);
-  wifi.RcvReply(rcv, 1000);
-  pc.printf("%s\n", rcv);
-  wait(2);
-  strcpy(snd,"AT+CIPSEND=4,47\n");    //Send Number of open connections,Characters to send 
-  wifi.SendCMD(snd);
-  pc.printf(snd);
-  wait(2.0);
-  wifi.RcvReply(rcv, 1000);
-  pc.printf("%s\n", rcv);
-  wait(2);    
-  sprintf(snd,"GET http://api.thingspeak.com/update?api_key=**********&field1=%1.3f\n", c); //Post values to thingspeak
+void wifi_send(void)
+{            
+  sprintf(snd,"GET http://api.thingspeak.com/update?api_key=NVG5E6ZAB970OMRF&field1=%f&field2=%f&field3=%f&field4=%f&field5=%f&field6=%f\r\n", cur, comp,temp,hum,co,pres); //Post values to thingspeak
   pc.printf("%s",snd);
   wifi.SendCMD(snd);
-  wait(2);
-  sprintf(snd,"GET http://api.thingspeak.com/update?api_key=***********&field2=%1.3f\n", h); //Post values to thingspeak
-  pc.printf("%s",snd);
-  wifi.SendCMD(snd);
-  wait(2);
-  wifi.RcvReply(rcv, 1000);
-  pc.printf("%s", rcv);
-  wifi.SendCMD("AT+CIPCLOSE"); //Close the connection to server
   wifi.RcvReply(rcv, 1000);
   pc.printf("%s", rcv);
 }
\ No newline at end of file