Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <ESP8266WiFi.h>
- #include <DHT.h>
- #include <DNSServer.h>
- #include <ESP8266WebServer.h>
- #include <WiFiManager.h>
- #define DHTPIN D4
- const char* server = "api.thingspeak.com";
- String apiKey = "KT1M58YURJ6YVVHD";
- DHT dht(DHTPIN, DHT22,15);
- WiFiClient client;
- void setup() {
- Serial.begin(115200);
- Serial.printf("Starting up");
- Serial.print("1");
- Serial.println("2");
- WiFiManager wifiManager;
- //reset saved settings
- //wifiManager.resetSettings();
- wifiManager.autoConnect("RoomTemp1");
- //if you get here you have connected to the WiFi
- Serial.printf("connected");
- delay(10);
- dht.begin();
- }
- void loop() {
- float h = dht.readHumidity();
- float t = dht.readTemperature();
- if (isnan(h) || isnan(t)) {
- Serial.println("Failed to read from DHT sensor!");
- return;
- }
- if (client.connect(server,80)) {
- String postStr = apiKey;
- postStr +="&field1=";
- postStr += String(t);
- postStr +="&field2=";
- postStr += String(h);
- postStr += "\r\n\r\n";
- client.print("POST /update HTTP/1.1\n");
- client.print("Host: api.thingspeak.com\n");
- client.print("Connection: close\n");
- client.print("X-THINGSPEAKAPIKEY: "+apiKey+"\n");
- client.print("Content-Type: application/x-www-form-urlencoded\n");
- client.print("Content-Length: ");
- client.print(postStr.length());
- client.print("\n\n");
- client.print(postStr);
- Serial.print("Temperature: ");
- Serial.print(t);
- Serial.print(" degrees Celsius Humidity: ");
- Serial.print(h);
- Serial.println("% send to Thingspeak");
- }
- client.stop();
- Serial.println("Waiting");
- delay(60000);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement