#include <ESP8266WiFi.h>
#include <WiFiClient.h>
#include <DHT.h>
#include <ESP8266HTTPClient.h>

#define DHTPIN 5 //5 = D1
#define DHTTYPE DHT22

DHT dht(DHTPIN, DHTTYPE);

const char* ssid = "ssid";
const char* password = "password";

void setup(void) {
  Serial.begin(115200);
  WiFi.begin(ssid, password);
  dht.begin();

  // Wait for connection
  while (WiFi.status() != WL_CONNECTED) {
    delay(500);
  }

  // Reading temperature or humidity takes about 250 milliseconds
  float h = dht.readHumidity();
  // Read temperature as Celsius (the default)
  float t = dht.readTemperature();

  // Check if any reads failed and exit early (to try again).
  if (isnan(h) || isnan(t)) {
    return;
  }

  HTTPClient http;
  http.begin("http://192.168.1.129/index.php");
  http.addHeader("Content-Type", "application/x-www-form-urlencoded");

  char outstr[15];
  String POSTString = "temp=";
  dtostrf(t, 6, 2, outstr);
  POSTString += outstr;
  POSTString += "&humi=";
  dtostrf(h, 6, 2, outstr);
  POSTString += outstr;
  POSTString += "&deviceid=1";

  http.POST(POSTString);
  http.writeToStream(&Serial);
  http.end();

  ESP.deepSleep(1800e6); //600e6 = 10 min, 1800 = 30 min.

}

void loop(void) {
}