Advertisement
Hadlock

ESP8266 DHT-11 Thingspeak Sketch

Apr 9th, 2016
192
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 3.50 KB | None | 0 0
  1. // www.arduinesp.com
  2. //
  3. // Plot DTH11 data on thingspeak.com using an ESP8266
  4. // April 11 2015
  5. // Author: Jeroen Beemster
  6. // Website: www.arduinesp.com
  7. // http://www.arduinesp.com/thingspeak
  8.  
  9.  
  10. #include <DHT.h>
  11. #include <ESP8266WiFi.h>
  12.  
  13. // replace with your channel's thingspeak API key,
  14. String apiKey = "F2C4EOEXBO746TX5";
  15. const char* ssid = "SomethingAwful";
  16. const char* password = "lljk";
  17.  
  18. const char* server = "api.thingspeak.com";
  19. #define DHTPIN 2 // what pin we're connected to
  20.  
  21. DHT dht(DHTPIN, DHT11,15);
  22. WiFiClient client;
  23.    
  24.  
  25. void setup() {                
  26.   Serial.begin(115200);
  27.   delay(10);
  28.   //initialize digital pin 13 as an output.
  29.   pinMode(13, OUTPUT);
  30.   dht.begin();
  31.  
  32.   WiFi.begin(ssid, password);
  33.  
  34.   Serial.println();
  35.   Serial.println();
  36.   Serial.print("Connecting to ");
  37.   Serial.println(ssid);
  38.    
  39.   WiFi.begin(ssid, password);
  40.    
  41.   while (WiFi.status() != WL_CONNECTED) {
  42.     delay(500);
  43.     Serial.print(".");
  44.   }
  45.   Serial.println("");
  46.   Serial.println("WiFi connected");
  47.   digitalWrite(13, HIGH);   // turn the LED on (HIGH is the voltage level)
  48.   delay(200);              // wait for a second
  49.   digitalWrite(13, LOW);    // turn the LED off by making the voltage LOW
  50.   delay(200);              // wait for a second  
  51.  
  52.   digitalWrite(13, HIGH);   // turn the LED on (HIGH is the voltage level)
  53.   delay(200);              // wait for a second
  54.   digitalWrite(13, LOW);    // turn the LED off by making the voltage LOW
  55.   delay(1000);              // wait for a second  
  56.   digitalWrite(13, HIGH);   // turn the LED on (HIGH is the voltage level)
  57.   delay(200);              // wait for a second
  58.   digitalWrite(13, LOW);    // turn the LED off by making the voltage LOW
  59. }
  60.  
  61.  
  62. void loop() {
  63.  
  64.  
  65.   float h = 14; //dht.readHumidity();
  66.   float t = 192; //dht.readTemperature();
  67.   if (isnan(h) || isnan(t)) {
  68.     Serial.println("Failed to read from DHT sensor!");
  69.     return;
  70.   }
  71.  
  72.   if (client.connect(server,80)) {  //   "184.106.153.149" or api.thingspeak.com
  73.     digitalWrite(13, HIGH);   // turn the LED on (HIGH is the voltage level)
  74.     Serial.print("Begin send\n");
  75.     String postStr = apiKey;
  76.            postStr +="&field1=";
  77.            postStr += "THRONEGUARD1"; //String(t);
  78.            postStr +="&field2=";
  79.            postStr += "THRONEGUARD2"; //String(h);
  80.            postStr += "\r\n\r\n";
  81.  
  82.      client.print("POST /update HTTP/1.1\n");
  83.      client.print("Host: api.thingspeak.com\n");
  84.      client.print("Connection: close\n");
  85.      client.print("X-THINGSPEAKAPIKEY: "+apiKey+"\n");
  86.      client.print("Content-Type: application/x-www-form-urlencoded\n");
  87.      client.print("Content-Length: ");
  88.      client.print(postStr.length());
  89.      client.print("\n\n");
  90.      client.print(postStr);
  91.      Serial.print("-End send\n");
  92.      digitalWrite(13, LOW);    // turn the LED off by making the voltage LOW          
  93.  
  94.      Serial.print("Temperature: ");
  95.      Serial.print(t);
  96.      Serial.print(" degrees Celcius Humidity: ");
  97.      Serial.print(h);
  98.      Serial.println("% send to Thingspeak");
  99. /*
  100.     digitalWrite(13, HIGH);   // turn the LED on (HIGH is the voltage level)
  101.     delay(1000);              // wait for a second
  102.     digitalWrite(13, LOW);    // turn the LED off by making the voltage LOW
  103.     delay(1000);              // wait for a second
  104. */
  105.   }
  106.   client.stop();
  107.    
  108.   Serial.println("Waiting...");    
  109.   // thingspeak needs minimum 15 sec delay between updates
  110.   delay(360000);  //20000 = 20 seconds //120000 = 2 min
  111. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement