Re: How to read current time from internet using ESP8266?
Posted: Thu Oct 29, 2015 7:10 am
Hi
This has helped me immensely with my project. Thanks!
Even though I understand in principle what you have done, I don't understand the 'how you knew' part.
Is there some documentation that explains what you'll get when you query 'google.com'? Obviously the web page is sent for you to load, but it doesn't display the time.
Apologies if this question is is really stupid and I'm missing the obvious, but web stuff is new to me.
Thanks,
Simon
This has helped me immensely with my project. Thanks!
Even though I understand in principle what you have done, I don't understand the 'how you knew' part.
Is there some documentation that explains what you'll get when you query 'google.com'? Obviously the web page is sent for you to load, but it doesn't display the time.
Apologies if this question is is really stupid and I'm missing the obvious, but web stuff is new to me.
Thanks,
Simon
torntrousers wrote: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.