Use NIST Time and Date on ESP Webserver

I try to combine 2 simple programs :

  1. NIST Date and time from the Internet
  2. Switch On/Off button on ESP Webserver

The reason I would like to do this is that I would like to switch off a pump after 22:00h in the evening. But I would to still able to switch it on/off myself. There is a third requirement which I did not yet included but I would like to get also the outside temperature so that the water pump keeps running during the night when the outside temperature is below 4 deg C (to avoid that we water freeze)

What I tried in below code is :

  1. Connect the ESP12E (NodeMCU 1.0) to my local network wifi point (Internet)
  2. Get timedate string from NIST website
  3. Put on/off buttons on the ESP12 website

The problem is that I do not understand how to close the client with the NIST website and than open the ESP webserver.

 #include <ESP8266WiFi.h>
#include "SSD1306.h" // alias for `#include "SSD1306Wire.h"`
 
const char* ssid = "WiFiPoint1";
const char* password = "password1";
const char* host = "time.nist.gov"; // Round-robin DAYTIME protocol
 
int ledPin = 13; // GPIO13 D7
int startPin = 12; // GPI012 D6

int ln = 0; // Length off
String TimeDate = ""; //Empty Time and Date
WiFiServer server(80);
 
void setup() {
  Serial.begin(115200);
  delay(10);
  pinMode(startPin, OUTPUT);
  pinMode(ledPin, OUTPUT);
  digitalWrite(ledPin, LOW);
  digitalWrite(startPin, HIGH);
  delay(500);
  digitalWrite(startPin, LOW);
  delay(500);
  digitalWrite(startPin, HIGH);
  delay(500);
  digitalWrite(startPin, LOW);
  // Connect to WiFi network
  Serial.println();
  Serial.println();
  Serial.print("Connecting to ");
  Serial.println(ssid);
 
  WiFi.begin(ssid, password);
 
  while (WiFi.status() != WL_CONNECTED) {
    for (int i=0; i <= 6; i++){
      digitalWrite(startPin, HIGH);
      delay(50);
      digitalWrite(startPin, LOW);
      delay(50);
    }
    Serial.print(".");
  }
  Serial.println("");
  Serial.println("WiFi connected");
  digitalWrite(startPin, HIGH);
  delay(500);
  digitalWrite(startPin, LOW);
  delay(100);
  digitalWrite(startPin, HIGH);
  delay(500);
  digitalWrite(startPin, LOW);
  delay(100);
  digitalWrite(startPin, HIGH);
  delay(500);
  digitalWrite(startPin, LOW);
  // Start the server
  server.begin();
  Serial.println("Server started");
 
  // Print the IP address
  Serial.print("Use this URL to connect: ");
  Serial.print("http://");
  Serial.print(WiFi.localIP());
  Serial.println("/");
 
}
 
void loop() {
  Serial.println("");
  Serial.print("Connecting to ");
  Serial.println(host);

// Use WiFiClient class to create TCP connections
  WiFiClient client;
  const int httpPort = 13;

  if (!client.connect(host, httpPort)) {
    Serial.println("connection failed");
    return;
  }
  Serial.println("");
  Serial.println("> Sending Header <");

// This will send the request to the server
  client.print("HEAD / HTTP/1.1\r\nAccept: */*\r\nUser-Agent: Mozilla/4.0 (compatible; ESP8266 NodeMcu Lua;)\r\n\r\n");

  delay(100);

  // Read all the lines of the reply from server and print them to Serial
  // expected line is like : Date: Thu, 01 Jan 2015 22:00:14 GMT
  char buffer[12];
  String dateTime = "";
  Serial.println("");
  Serial.println("> Listening <");
  
 while(client.available())
  {
    String line = client.readStringUntil('\r');

    if (line.indexOf("Date") != -1)
    {
      Serial.print("=====>");
    } else
    {
      Serial.print(line);
      // date starts at pos 7
      TimeDate = line.substring(7);
      Serial.println(TimeDate);
      // time starts at pos 14
      TimeDate = line.substring(7, 15);
      TimeDate.toCharArray(buffer, 10);
      Serial.print("UTC Date/Time:");
      Serial.println(buffer);
      TimeDate = line.substring(16, 24);
      TimeDate.toCharArray(buffer, 10);
      Serial.println(buffer);
    }
  }
  Serial.println();
  Serial.println("closing connection");


  
  // Check if a client has connected
  WiFiClient client = server.available();
  if (!client) {
    return;
  }
 
  // Wait until the client sends some data
  Serial.println("new client");
  while(!client.available()){
    delay(1);
  }
 
  // Read the first line of the request
  String request = client.readStringUntil('\r');
  Serial.println(request);
  client.flush();
 
  // Match the request
 
  int value = LOW;
  if (request.indexOf("/LED=ON") != -1)  {
    analogWrite(ledPin, 150);
    value = HIGH;
  }
  if (request.indexOf("/LED=OFF") != -1)  {
    analogWrite(ledPin, 20);
    value = LOW;
  }
 
// Set ledPin according to the request
//digitalWrite(ledPin, value);
 
  // Return the response
  client.println("HTTP/1.1 200 OK");
  client.println("Content-Type: text/html");
  client.println(""); //  do not forget this one
  client.println("<!DOCTYPE HTML>");
  client.println("<html>");
 
  client.print("Led pin is now: ");
 
  if(value == HIGH) {
    client.print("On");
  } else {
    client.print("Off");
  }
  client.println("

");
  client.println("<a href=\"/LED=ON\"\"><button>Turn On </button></a>");
  client.println("<a href=\"/LED=OFF\"\"><button>Turn Off </button></a>
");  
  client.println("</html>");
 
  delay(1);
  Serial.println("Client disonnected");
  Serial.println("");
 
}

The Error message I get at the moment is

Arduino: 1.6.12 (Windows 10), Board: "NodeMCU 1.0 (ESP-12E Module), 80 MHz, 115200, 4M (3M SPIFFS)"

C:\.....\Arduino\led_wifi_time\led_wifi_time.ino: In function 'void loop()':

led_wifi_time:127: error: redeclaration of 'WiFiClient client'

   WiFiClient client = server.available();

              ^

led_wifi_time:76: error: 'WiFiClient client' previously declared here

   WiFiClient client;

              ^

exit status 1
redeclaration of 'WiFiClient client'

Question is how this should be done and how to add a 3rd client to get the outside temperature (e.g via thingspeak.com) ?

The name of the WiFiClient object has no significance. You can create three instances, named Tom, Dick, and Harry. No one will mind.

So what you try to say is that I can call the first WiFiClient client but the next WiFiClent should than call different e.g WiFiClent TimeClient or WiFiClent John

// Use WiFiClient class to create TCP connections
  WiFiClient TimeClient  const int httpPort = 13;

  if (!TimeClient.connect(host, httpPort)) {
    Serial.println("connection failed");
    return;
  }

I will try .......

Paul, Thanks....it worked !