Skip to content

Commit 6b201d5

Browse files
committed
Initial import
0 parents  commit 6b201d5

File tree

6 files changed

+1228
-0
lines changed

6 files changed

+1228
-0
lines changed

TimeClient.cpp

Lines changed: 112 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,112 @@
1+
/**The MIT License (MIT)
2+
3+
Copyright (c) 2015 by Daniel Eichhorn
4+
5+
Permission is hereby granted, free of charge, to any person obtaining a copy
6+
of this software and associated documentation files (the "Software"), to deal
7+
in the Software without restriction, including without limitation the rights
8+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9+
copies of the Software, and to permit persons to whom the Software is
10+
furnished to do so, subject to the following conditions:
11+
12+
The above copyright notice and this permission notice shall be included in all
13+
copies or substantial portions of the Software.
14+
15+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21+
SOFTWARE.
22+
23+
See more at http://blog.squix.ch
24+
*/
25+
26+
#include "TimeClient.h"
27+
28+
TimeClient::TimeClient(int utcOffset) {
29+
myUtcOffset = utcOffset;
30+
}
31+
32+
void TimeClient::updateTime() {
33+
WiFiClient client;
34+
const int httpPort = 80;
35+
if (!client.connect("www.google.com", httpPort)) {
36+
Serial.println("connection failed");
37+
return;
38+
}
39+
40+
// This will send the request to the server
41+
client.print(String("GET / HTTP/1.1\r\n") +
42+
String("Host: www.google.com\r\n") +
43+
String("Connection: close\r\n\r\n"));
44+
while(!client.available()) {
45+
delay(1000);
46+
}
47+
48+
String line;
49+
50+
int size = 0;
51+
client.setNoDelay(false);
52+
while(client.connected()) {
53+
while((size = client.available()) > 0) {
54+
line = client.readStringUntil('\n');
55+
line.toUpperCase();
56+
// example:
57+
// date: Thu, 19 Nov 2015 20:25:40 GMT
58+
if (line.startsWith("DATE: ")) {
59+
Serial.println(line.substring(23, 25) + ":" + line.substring(26, 28) + ":" +line.substring(29, 31));
60+
int parsedHours = line.substring(23, 25).toInt();
61+
int parsedMinutes = line.substring(26, 28).toInt();
62+
int parsedSeconds = line.substring(29, 31).toInt();
63+
64+
localEpoc = (parsedHours * 60 * 60 + parsedMinutes * 60 + parsedSeconds);
65+
}
66+
}
67+
}
68+
69+
}
70+
71+
String TimeClient::getHours() {
72+
if (localEpoc == 0) {
73+
return "--";
74+
}
75+
int hours = (getCurrentEpoch() % 86400L) / 3600 + myUtcOffset;
76+
if (hours < 10) {
77+
return "0" + String(hours);
78+
}
79+
return String(hours); // print the hour (86400 equals secs per day)
80+
81+
}
82+
String TimeClient::getMinutes() {
83+
if (localEpoc == 0) {
84+
return "--";
85+
}
86+
int minutes = ((getCurrentEpoch() % 3600) / 60);
87+
if (minutes < 10 ) {
88+
// In the first 10 minutes of each hour, we'll want a leading '0'
89+
return "0" + String(minutes);
90+
}
91+
return String(minutes);
92+
}
93+
String TimeClient::getSeconds() {
94+
if (localEpoc == 0) {
95+
return "--";
96+
}
97+
int seconds = getCurrentEpoch() % 60;
98+
if ( seconds < 10 ) {
99+
// In the first 10 seconds of each minute, we'll want a leading '0'
100+
return "0" + String(seconds);
101+
}
102+
return String(seconds);
103+
}
104+
105+
String TimeClient::getFormattedTime() {
106+
return getHours() + ":" + getMinutes() + ":" + getSeconds();
107+
}
108+
109+
long TimeClient::getCurrentEpoch() {
110+
return localEpoc + ((millis() - localMillisAtUpdate) / 1000);
111+
}
112+

TimeClient.h

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
/**The MIT License (MIT)
2+
3+
Copyright (c) 2015 by Daniel Eichhorn
4+
5+
Permission is hereby granted, free of charge, to any person obtaining a copy
6+
of this software and associated documentation files (the "Software"), to deal
7+
in the Software without restriction, including without limitation the rights
8+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9+
copies of the Software, and to permit persons to whom the Software is
10+
furnished to do so, subject to the following conditions:
11+
12+
The above copyright notice and this permission notice shall be included in all
13+
copies or substantial portions of the Software.
14+
15+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21+
SOFTWARE.
22+
23+
See more at http://blog.squix.ch
24+
*/
25+
#pragma once
26+
27+
#include <ESP8266WiFi.h>
28+
#include <WiFiUdp.h>
29+
30+
#define NTP_PACKET_SIZE 48
31+
32+
class TimeClient {
33+
34+
private:
35+
int myUtcOffset = 0;
36+
long localEpoc = 0;
37+
long localMillisAtUpdate;
38+
39+
const char* ntpServerName = "time.nist.gov";
40+
unsigned int localPort = 2390;
41+
42+
byte packetBuffer[ NTP_PACKET_SIZE]; //buffer to hold incoming and outgoing packets
43+
44+
45+
public:
46+
TimeClient(int utcOffset);
47+
void updateTime();
48+
49+
String getHours();
50+
String getMinutes();
51+
String getSeconds();
52+
String getFormattedTime();
53+
long getCurrentEpoch();
54+
55+
};
56+

0 commit comments

Comments
 (0)