Example sketches for the new Arduino IDE for ESP8266

Moderator: igrr

User avatar
By ludzinc
#32541 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

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.
User avatar
By sushma
#32609 I used following commands to read current time and I got data after 5mins. When I tried to read second time I didn't get any data and became unlink after some time. Why is this happening sometimes it fetches data sometimes it wont. Is ESP8266 is not reliable?
AT+CIPSTART="TCP","time.is",80


OK
Linked
AT+CIPSEND=35

> GET / HTTP /1.1\r\n
Host: time.is\r\n\r\n\r\n\r\n

busy s...

SEND OK

+IPD,394:HTTP/1.1 408 Request Time-out
Date: Fri, 30 Oct 2015 09:51:08 GMT
Server: Apache
Content-Length: 223
Connection: close
Content-Type: text/html; charset=iso-8859-1

<!DOCTYPE HTML PUBLIC "-//IETF//DTD HTML 2.0//EN">
<html><head>
<title>408 Request Time-out</title>
</head><body>
<h1>Request Time-out</h1>
<p>Server timeout waiting for the HTTP request from the client.</p>
</body></html>

OK

OK
Unlink
AT+CIPSTART="TCP","time.is",80


OK
Linked
AT+CIPSEND=35

> GET / HTTP /1.1\r\n Host: time.is\r\n\r\n\r\n\r\n

busy s...

SEND OK
OK
Unlink
If the result is like this how can I read current time repeatedly? I want to use ESP8266 for real time project. If ESP8266 is not responding properly how can I complete my project? Anyone help me to resolve this, so that I can get accurate response from ESP8266
User avatar
By rafi4439
#67002 // this coding is working now guys! :) :) :) :) :)

#include <ESP8266WiFi.h>

const char* ssid = "xxxx";
const char* password = "xxxx";

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 (WiFi.status() != WL_CONNECTED) {
WiFi.begin(ssid, password);
}

while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
}
Serial.print("\nWiFi connected, IP address: ");
Serial.println(WiFi.localIP());
}// - See more at: viewtopic.php?f=29&t=6007&start=5#sthash.PzIMlnpX.dpuf