-->
Page 1 of 6

How to read current time from internet using ESP8266?

PostPosted: Wed Oct 14, 2015 11:12 pm
by sushma
I want to read current time from internet using ESP8266 which is interfaced with Arduino Uno. I want to display the current time read on LCD display interfaced with Arduino. Which AT command is used to get current time?

Re: How to read current time from internet using ESP8266?

PostPosted: Thu Oct 15, 2015 12:02 am
by kolban
Looking at the document from Espressif called "ESP8266 AT Instruction Set v0.40" I don't see any commands related to Simple Network Time Protocol (SNTP) which is what is used to retrieve the current time. The SNTP APIs are available for SDK applications written in C but I don't believe these have been mapped to the AT command processor.

Re: How to read current time from internet using ESP8266?

PostPosted: Thu Oct 15, 2015 2:18 am
by sushma
Is there any other way to read current time using ESP8266?

Re: How to read current time from internet using ESP8266?

PostPosted: Thu Oct 15, 2015 3:59 am
by torntrousers
The HTTP RFC defines a Date header so pretty much all websites will return the current date/time in all http responses, so you can use that. Here's a sketch function to do that by making an HTTP HEAD request to google.com:
Code: Select allString getTime() {
  WiFiClient client;
  while (!!!client.connect("google.com", 80)) {
    Serial.println("connection failed, retrying...");
  }

  client.print("HEAD / HTTP/1.1\r\n\r\n");
  while(!!!client.available()) {
     yield();
  }

  while(client.available()){
    if (client.read() == '\n') {   
      if (client.read() == 'D') {   
        if (client.read() == 'a') {   
          if (client.read() == 't') {   
            if (client.read() == 'e') {   
              if (client.read() == ':') {   
                client.read();
                String theDate = client.readStringUntil('\r');
                client.stop();
                return theDate;
              }
            }
          }
        }
      }
    }
  }
}


That will return a String like "Thu, 15 Oct 2015 08:57:03 GMT"