#include <ESP8266WiFi.h>
#include <ESP8266HTTPClient.h>
#include "DHTesp.h"
const char* ssid = "DES";
const char* password = "12345678";
String payload;
const char* url = "http://api.thingspeak.com/update?" ;
String myApikey="HL4MVUTCSR9UTJGD";
String X="60";
//int x=random(30);
HTTPClient http;
WiFiClient wiwi ;
DHTesp xyz;
void setup() {
Serial.begin(115200);
WiFi.begin(ssid, password);
Serial.print("Connecting to WiFi");
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
}
xyz.setup(16, DHTesp::DHT11);
http.begin(wiwi,url);
Serial.print("HTTP done");
}
void loop() {
float y = xyz.getHumidity();
float x= xyz.getTemperature();
http.addHeader("Content-type","application/json");
String httpRequestdata="{\"api_key\":\""+myApikey+"\",\"field2\":\""+String(X)+"\"}";
Serial.print (httpRequestdata);
int httpResponseCode = http.POST(httpRequestdata);
if (httpResponseCode > 0) {
Serial.print("HTTP ");
Serial.println(httpResponseCode);
Serial.print("Data Sent");
// Disconnect
http.end();
}
else {
Serial.print("Error code: ");
Serial.println(httpResponseCode);
Serial.println(":-(");
}
delay(10000);
}