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("");
}