/*
    This sketch sends data via HTTP GET requests to data.sparkfun.com service.

    You need to get streamId and privateKey at data.sparkfun.com and paste them
    below. Or just customize this script to talk to other HTTP servers.

*/

#include <ESP8266WiFi.h>
#include <NTPClient.h>
#include <WiFiUdp.h>
const char* ssid = "VOVA-47V";
const char* password = "vovak26102003";
WiFiUDP ntpUDP;
const char* host = "api.openweathermap.org";
String line ;
NTPClient timeClient(ntpUDP, "europe.pool.ntp.org", 3600 + 7200, 60000);
int a = 0;
//****************************************************************************
void setup() {

  // delay(1000);
  Serial.begin(9600);
  delay(1000);

  //pinMode(2,  OUTPUT);//gpio2//motor
  //   digitalWrite(2, 1);
  // We start by connecting to a WiFi network


  //  Serial.println();
  //  Serial.println();7
  Serial.print("Connecting to ");
  Serial.println(ssid);

  WiFi.begin(ssid, password);

  while (WiFi.status() != WL_CONNECTED) {
    delay(500);
    a++;
    if (a > 10)
    { a = 0;
      goto q;
    }
    // Serial.print(".");
  }

  // Serial.println("");
  Serial.println("WiFi connected");
  Serial.println("IP");
  Serial.println(WiFi.localIP());
q:
  //Serial.println();
  delay(2000);
}

int value = 0;
int n = 0;



void loop() {
  Serial.println("hi");

  a = 0;
  n = 0;
  while (a < 10)
  {
    delay(2000);
    if (Serial.available() > 0) {
      n = Serial.read() - 48;

      if (n == 7)
      { Serial.print(n);
        Serial.println("+");

        a = 11;
      }
    }
    a++;
  }
  if (n > 0)
  { a = 0;
    n = 0;
    while (a < 10)
    { delay(2000);
      a++;
      if (Serial.available() > 0) {
        n = Serial.read() - 48;

        if (n == 1)
        {
          Serial.print(n);
          Serial.println("+");

          Serial.print("connecting to ");
          Serial.println(host);

          // Use WiFiClient class to create TCP connections
          WiFiClient client;
          const int httpPort = 80;
          // We now create a URI for the request
          String url = "/data/2.5/forecast/daily?q=";
          // url +="yevpatoriya&mode=xml&units=metric&cnt=7";
          url += "Yevpatoriya,UA&APPID=a0b9f94d153cbcbfeb00ea5632db24ac&mode=xml&cnt=7";
          //http://api.openweathermap.org/data/2.5/forecast/daily?q=
          //    Yevpatoriya,UA&APPID=a0b9f94d153cbcbfeb00ea5632db24ac&mode=metric&cnt=7






          // url=data/2.5/forecast/daily?q=yevpatoriya&mode=xml&units=metric&cnt=7


          // This will send the request to the server
          if (!client.connect(host, httpPort)) {
            Serial.println("connection failed");
            a = 0;
            return;
          }


          client.print(String("GET ") + url + " HTTP/1.1\r\n" +
                       "Host: " + host + "\r\n" +
                       "Connection: close\r\n\r\n");
          Serial.print("Requesting URL: ");
          Serial.println(url);
          delay(1000);
          Serial.print( "host1");
          Serial.println();
          // Read all the lines of the reply from server and print them to Serial
          while (client.available()) {
            line = client.readStringUntil('\r');
            Serial.print(line);
          }
          Serial.println();
          Serial.print(line.length());
          delay(5000);
          if (line.length() < 10)
          { if (!client.connect(host, httpPort)) {
              Serial.println("connection failed");
              a = 0;
              return;
            }
            // This will send the request to the server
            client.print(String("GET ") + url + " HTTP/1.1\r\n" +
                         "Host: " + host + "\r\n" +
                         "Connection: close\r\n\r\n");
            Serial.print("Requesting URL: ");
            Serial.println(url);
            delay(500);
            Serial.print( "host1");
            Serial.println();
            // Read all the lines of the reply from server and print them to Serial
            while (client.available()) {
              String line = client.readStringUntil('\r');
              Serial.print(line);
            }
          }

          Serial.println();
          Serial.println("closing connection");
          delay(2000);

        }
        //******************************Time
        if (n == 2)
        {
          Serial.print(n);
          Serial.println("+");

          timeClient.update();

          time_t epochTime = timeClient.getEpochTime();


          Serial.println(timeClient.getFormattedTime());
          //Get a time structure
          struct tm *ptm = gmtime ((time_t *)&epochTime);

          int monthDay = ptm->tm_mday;
          // Serial.print("Month day: ");
          // Serial.println(monthDay);

          int currentMonth = ptm->tm_mon + 1;
          // Serial.print("Month: ");
          // Serial.println(currentMonth);
          int currentYear = ptm->tm_year + 1900;
          //Serial.print("Year: ");
          // Serial.println(currentYear);
          String currentDate = String(monthDay) + "-" + String(currentMonth) + "-" + String(currentYear) ;
          Serial.println(currentDate);

        }
      } //serial available
    }
  }
  Serial.println("sleep");
  delay(1000);
  ESP.deepSleep(9000000);


}


void con()
{
  WiFi.begin(ssid, password);
  while (WiFi.status() != WL_CONNECTED) {
    a = 0;
    delay(500);
    a++;
    if (a > 10)
    { a = 0;
      return;
    }
  }


}
