Advertisement
ossipee

Untitled

Jan 16th, 2016
310
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.06 KB | None | 0 0
  1.  
  2. //
  3. // Plot DTH22 data on thingspeak.com using an ESP8266
  4. // April 11 2015
  5. // Author: Jeroen Beemster
  6. // Website: www.arduinesp.com
  7.  
  8. #include <DHT.h>
  9. #include <ESP8266WiFi.h>
  10.  
  11. // replace with your channel's thingspeak API key,
  12. String apiKey = "BWAUP5A38C3KLFJA";
  13. const char* ssid = "10FX11111580";
  14. const char* password = "password";
  15.  
  16. const char* server = "api.thingspeak.com";
  17. #define DHTPIN 2 // what pin we're connected to
  18.  
  19. DHT dht(DHTPIN, DHT22,15);
  20. WiFiClient client;
  21.  
  22. void setup() {
  23.   Serial.begin(115200);
  24.   delay(10);
  25.   dht.begin();
  26.  
  27.   WiFi.begin(ssid, password);
  28.  
  29.   Serial.println();
  30.   Serial.println();
  31.   Serial.print("Connecting to ");
  32.   Serial.println(ssid);
  33.  
  34.   WiFi.begin(ssid, password);
  35.  
  36.   while (WiFi.status() != WL_CONNECTED) {
  37.     delay(500);
  38.     Serial.print(".");
  39.   }
  40.   Serial.println("");
  41.   Serial.println("WiFi connected");
  42.  
  43. }
  44.  
  45. void loop() {
  46.  
  47.   float h = dht.readHumidity();
  48.   float t = dht.readTemperature();
  49.   if (isnan(h) || isnan(t)) {
  50.     Serial.println("Failed to read from DHT sensor!");
  51.     return;
  52.   }
  53.  
  54.   if (client.connect(server,80)) {  //   "184.106.153.149" or api.thingspeak.com
  55.     String postStr = apiKey;
  56.            postStr +="&field1=";
  57.            postStr += String(t);
  58.            postStr +="&field2=";
  59.            postStr += String(h);
  60.            postStr += "\r\n\r\n";
  61.  
  62.      client.print("POST /update HTTP/1.1\n");
  63.      client.print("Host: api.thingspeak.com\n");
  64.      client.print("Connection: close\n");
  65.      client.print("X-THINGSPEAKAPIKEY: "+apiKey+"\n");
  66.      client.print("Content-Type: application/x-www-form-urlencoded\n");
  67.      client.print("Content-Length: ");
  68.      client.print(postStr.length());
  69.      client.print("\n\n");
  70.      client.print(postStr);
  71.  
  72.      Serial.print("Temperature: ");
  73.      Serial.print(t);
  74.      Serial.print(" degrees Celcius Humidity: ");
  75.      Serial.print(h);
  76.      Serial.println("% send to Thingspeak");
  77.   }
  78.   client.stop();
  79.  
  80.   Serial.println("Waiting...");
  81.   // thingspeak needs minimum 15 sec delay between updates
  82.   delay(20000);
  83. };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement