Advertisement
symondavis

ESP8266 - ThingSpeak with WiFi Manager - DHT22

Oct 16th, 2017
746
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #include <ESP8266WiFi.h>
  2. #include <DHT.h>
  3. #include <DNSServer.h>
  4. #include <ESP8266WebServer.h>
  5. #include <WiFiManager.h>
  6.  
  7. #define DHTPIN D4
  8. const char* server = "api.thingspeak.com";
  9. String apiKey = "KT1M58YURJ6YVVHD";
  10. DHT dht(DHTPIN, DHT22,15);
  11. WiFiClient client;
  12.  
  13. void setup() {
  14.     Serial.begin(115200);
  15.     Serial.printf("Starting up");
  16.     Serial.print("1");
  17.     Serial.println("2");
  18.    
  19.     WiFiManager wifiManager;
  20.     //reset saved settings
  21.     //wifiManager.resetSettings();
  22.  
  23.     wifiManager.autoConnect("RoomTemp1");
  24.    
  25.     //if you get here you have connected to the WiFi
  26.     Serial.printf("connected");
  27.     delay(10);
  28.     dht.begin();
  29. }
  30.  
  31. void loop() {
  32.   float h = dht.readHumidity();
  33.   float t = dht.readTemperature();
  34.   if (isnan(h) || isnan(t)) {
  35.     Serial.println("Failed to read from DHT sensor!");
  36.     return;
  37.   }
  38.  
  39.  
  40.   if (client.connect(server,80)) {
  41.     String postStr = apiKey;
  42.     postStr +="&field1=";
  43.     postStr += String(t);
  44.     postStr +="&field2=";
  45.     postStr += String(h);
  46.     postStr += "\r\n\r\n";
  47.    
  48.     client.print("POST /update HTTP/1.1\n");
  49.     client.print("Host: api.thingspeak.com\n");
  50.     client.print("Connection: close\n");
  51.     client.print("X-THINGSPEAKAPIKEY: "+apiKey+"\n");
  52.     client.print("Content-Type: application/x-www-form-urlencoded\n");
  53.     client.print("Content-Length: ");
  54.     client.print(postStr.length());
  55.     client.print("\n\n");
  56.     client.print(postStr);
  57.    
  58.     Serial.print("Temperature: ");
  59.     Serial.print(t);
  60.     Serial.print(" degrees Celsius Humidity: ");
  61.     Serial.print(h);
  62.     Serial.println("% send to Thingspeak");
  63.   }
  64.   client.stop();
  65.  
  66.   Serial.println("Waiting");
  67.  
  68.   delay(60000);
  69. }
Advertisement
RAW Paste Data Copied
Advertisement