-->
Page 2 of 6

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

PostPosted: Fri Oct 16, 2015 5:18 am
by torntrousers
sushma wrote:Can I have complete program?

Code: Select all#include <ESP8266WiFi.h>

const char* ssid = "<yourWiFiSSID>";
const char* password = "<yourWiFiPswd>";

void setup() {
  Serial.begin(115200);
  Serial.println();
  initWifi();
}

void loop() {
  Serial.println(getTime());
  delay(5000);
}
 
String 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;
              }
            }
          }
        }
      }
    }
  }
}

void initWifi() {
   Serial.print("Connecting to ");
   Serial.print(ssid);
   if (strcmp (WiFi.SSID(),ssid) != 0) {
      WiFi.begin(ssid, password);
   }

   while (WiFi.status() != WL_CONNECTED) {
      delay(500);
      Serial.print(".");
   }
  Serial.print("\nWiFi connected, IP address: ");
  Serial.println(WiFi.localIP());
}

That is for an ESP8266 running the native Arduino core which might not be quite what you want because above you mentioned using AT commands and having the ESP connected to an Uno. From a quick Google there is an example of using this approach of getting the time from the HTTP Date header using ESP AT commands here.

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

PostPosted: Fri Oct 16, 2015 5:51 am
by sushma
Thank you so much

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

PostPosted: Fri Oct 16, 2015 7:14 am
by sushma
When compiling the above program, Arduino is displaying "ESP8266WiFi.h - no such file or directory".
I downloaded 'ESP8266WiFi.h' file and saved it in Arduino libraries. Arduino is showing same error. How to correct it?

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

PostPosted: Fri Oct 16, 2015 9:04 am
by martinayotte
The ESP8266Wifi.h is part of the libraries coming with ArduinoESP.
You can not simply download and copy individual files, you need to install ArduinoESP properly as described in https://github.com/esp8266/Arduino.