Re: How to read current time from internet using ESP8266?
Posted: Thu Oct 15, 2015 5:20 am
Can I have complete program?
-->
Open Community Forum for ESP8266, Come share Arduino and IoT (Internet of Things)
https://www.esp8266.com/
sushma wrote:Can I have complete program?
#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());
}