/
Google_clock_serial.ino
128 lines (111 loc) · 4.79 KB
/
Google_clock_serial.ino
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
/* Sample sketch to demonstrate the HttpDateTime library function
* Makes GET request to server and extracting date / time from received header.
* Adds timezone and daylight saving if necessary
* Print the result over serial, Copyright (c) 2018 by Dave Waldesbuehl
* Nicu FLORICA (niq_ro) test the google clock + date
*/
#include <ESP8266WiFi.h>
#include <HttpDateTime.h> // https://github.com/DIYDave
// WiFi settings
const char* SSID = "network_name"; // The SSID of your WiFi accesspoint
const char* PASSWORD = "network_pass"; // password for your WiFi accesspoint
// Http settings
const char SERVER[30] = "www.google.com"; // Every host is possible
String COMMAND ="https://www.google.com/"; // Every site or API request is possible
const int SSL_PORT = 80; //443; // Use port 80 for non secure connection
// Time settings
const int TIMEZONE = 2; // +/- Timezone offset to GMT. e.g. 1 for MEZ, 0 for GMT
const bool USEDAYLIGHT = 1; // Use European daylight saving or not
//static char *weekday[7] = {"Mo", "Di", "Mi", "Do", "Fr", "Sa", "Sonntag"}; //Change to your language
static char *weekday[7] = {"Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday", "Sunday"};
unsigned long lastRequestTime = 10000; // Last server request. INTERVAL time for instant requeat after start
unsigned long INTERVAL = 10000; // Intervall time for request
const unsigned long TIMEOUT = 2500; // Timeout for header recieve
stHttpDT LocalTime; // Define variable with "stHttpDT" struct
HttpDateTime HttpDateTime(TIMEZONE,USEDAYLIGHT); // Create class instance
//WiFiClientSecure client; // SSL Client connection to server. use "WiFiClient client;" for port 80
WiFiClient client;
void setup(void)
{
Serial.begin(19200);
Serial.println();
Serial.print("Connecting to ");
Serial.println(SSID);
WiFi.mode(WIFI_STA);
WiFi.begin(SSID, PASSWORD);
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
}
Serial.println("");
Serial.println("WiFi connected");
Serial.println("IP address: ");
Serial.println(WiFi.localIP());
}
void loop(void)
{
if (millis() - lastRequestTime > INTERVAL) {
parseDateTime(HttpRequest()); // Make request to server an give the header to the parser
lastRequestTime = millis();
}
}
/////////////////////////////////// Online Function ///////////////////////////////////////////////////////
String HttpRequest() { // makes a HTTP request
unsigned long timeNow;
bool HeaderComplete = false;
bool currentLineIsBlank = true;
String header="";
client.stop(); // Close any connection before send a new request. This will free the socket on the WiFi
if (client.connect(SERVER, SSL_PORT)) { // if there's a successful connection:
client.println("GET " + COMMAND + " HTTP/1.1");
client.print("HOST: ");
client.println(SERVER);
client.println();
timeNow=millis();
while (millis() - timeNow < TIMEOUT) {
while (client.available()) {
char c = client.read();
if(!HeaderComplete){
if (currentLineIsBlank && c == '\n') {
HeaderComplete = true;
}else{
header = header + c;
}
}
if (c == '\n') {
currentLineIsBlank = true;
}else if (c != '\r') {
currentLineIsBlank = false;
}
}
if (HeaderComplete) break;
}
}else {
Serial.println("Connection failed");
}
if(client.connected()) {
client.stop();
}
return header;
}
/////////////////////////////////// End Of Online Function ///////////////////////////////////////////////////////
void parseDateTime(String _header){
// Serial.println(_header);
if (HttpDateTime.getDateTime(_header,LocalTime)){ // Parse date and time from header and calculate timezone and daylight saving
printString();
}else{
Serial.println("Error parsing header");
}
}
void printString(){
char dtString[30];
sprintf(dtString,"%s, %02u.%02u.%04u %02u:%02u:%02u",
weekday[LocalTime.dayOfWeek],
LocalTime.day,
LocalTime.month,
LocalTime.year,
LocalTime.hour,
LocalTime.minute,
LocalTime.second);
Serial.println(dtString);
}