D1 mini connection failed frequency

I have a D1 mini that pings time.nist.gov for current date and time. It seems to have a lot of failed connections, or connection attempts that return an empty string. A lot of time it only returns a usable value from about 1/3 connection attempts. Is this normal, or is there a way to improve reliability?

Code:

#include <ESP8266WiFi.h>
 
const char* ssid = "**********";
const char* password = "***********";
const char* host = "time.nist.gov"; // Round-robin DAYTIME protocol

int ledPin = D6, value=0;

String TimeDate = "", 
       Hour = "", Minute = "", Second = "";

unsigned long tic, toc;

uint8_t clock_time[2];

WiFiServer server(80);
 
void setup() {
  Serial.begin(9600);
  delay(10);
 
  pinMode(ledPin, OUTPUT);
  digitalWrite(ledPin, LOW);
 
  // Connect to WiFi network
  Serial.println();
  Serial.println();
  Serial.print("Connecting to ");
  Serial.println(ssid);

  WiFi.mode(WIFI_STA);
  WiFi.disconnect();
  WiFi.begin(ssid, password);
 
  while (WiFi.status() != WL_CONNECTED) {
    delay(500);
    Serial.print(".");
  }
  Serial.println("");
  Serial.println("WiFi connected");
 
  // 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() {
  Get_Time();
  delay(2);

  tic = millis();
  toc = millis() - tic;

  while (toc < 1000){
    Check_Client();

    switch(value){
      case 0:
        digitalWrite(ledPin, LOW);
        break;

      case 1:
        digitalWrite(ledPin, HIGH);
        break;
      
      case 2:
        if ((clock_time[1]%2)>0){
          digitalWrite(ledPin, HIGH);
        }
        if ((clock_time[1]%2)<1){
          digitalWrite(ledPin, LOW); 
        }
        break;
    }
    toc = millis() - tic;
  }
}

void Get_Time(){
  String line;
      Serial.print("connecting to ");
      Serial.println(host);

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

      delay(2);

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

      // 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);

      while(client.available())
      {
        line = client.readStringUntil('\r');
        if (line.indexOf("Date") != -1)
        {
          delay(2);
          Serial.print("=====>");
        } 
        else
        {
        }
      }

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

      delay(100);

      TimeDate = line.substring(16,24);
      Hour = line.substring(16,18);
      Minute = line.substring(19,21);
      Second = line.substring(22, 24);

      Serial.println(TimeDate);
      Serial.print(Hour);
      Serial.print(":");
      Serial.print(Minute);
      Serial.print(":");
      Serial.println(Second);
      Serial.print("Size: ");
      Serial.println(Hour.length());

      if (Hour.length()){
        clock_time[0] = Hour.toInt();
        clock_time[1] = Minute.toInt();
      }
}
//
//
//
void Check_Client(){
  // 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(5);
  }
 
  // Read the first line of the request
  String request = client.readStringUntil('\r');
  Serial.println(request);
  client.flush();
 
  // Match the request
 
  if (request.indexOf("/LED=ON") != -1)  {
    value = 1;
  }
  if (request.indexOf("/LED=OFF") != -1)  {
    value = 0;
  }
  if (request.indexOf("/LED=TIME") != -1){
    value = 2;
  }
 
// 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 == 1) {
    client.print("On");
  } 
  if (value == 0) {
    client.print("Off");
  }
  if (value == 2){
    client.print("Time-controlled");
  }
  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("<a href=\"/LED=TIME\"\"><button>Time Control </button></a>
");
  client.println("

");
  
  client.print("The time is now: ");
  client.print(Hour);
  client.print(":");
  client.print(Minute);
  client.print(":");
  client.println(Second); 
  client.println("</html>");
 
  delay(5);
  Serial.println("Client disonnected");
  Serial.println("");
 
}

I often use the Network Time Protocol. It's much simpler than HTTP and is non-blocking.
Take a look at this example: https://tttapa.github.io/ESP8266/Chap15 - NTP.html

Pieter

There are several things in the code that just luckily end in the correct result.

The HTTP port is not 13, so you shouldn't call the corresponding variable "httpPort".
On that port there's not a web server listening, so you shouldn't send a HTTP header to it. It will send you the time and date without any command to be sent to it.

As time.nist.gov is a pool with a lot of servers in it you never know which one you get. You also might get problems on the way from your WiFi device to the internet during that time.

So would you recommend changing the httpPort to a different value, or keep that port but remove the command?

And forgive my ignorance, but what would it look like to keep the port 13, but still get the date and time without sending a command?

So would you recommend changing the httpPort to a different value, or keep that port but remove the command?

No, just don't call it "HTTP" if it's not HTTP!

And forgive my ignorance, but what would it look like to keep the port 13, but still get the date and time without sending a command?

That's what I tried to tell you: you don't need to send any command/request header. Just open the connection to port 13 and the date is sent. Some time servers may block you if you send them any data.

Thanks pylon.

I've been doing AVR stuff for a while now, so my C++ isn't too bad, but I'm pretty much at square 1 sorting out the internet side of it all. So no matter how simple something is, I find a way to over-think and over-complicate it into something I can't understand. :confused: