Skip to content

Commit 7f889f3

Browse files
committed
Added world clock demo
1 parent b02c75e commit 7f889f3

File tree

6 files changed

+1562
-1
lines changed

6 files changed

+1562
-1
lines changed

WorldClockClient.cpp

Lines changed: 195 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,195 @@
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+
27+
#include "WorldClockClient.h"
28+
29+
30+
WorldClockClient::WorldClockClient(String language, String country, String dateFormat, int numberOfTimeZones, String* timeZoneIds) {
31+
myLanguage = language;
32+
myCountry = country;
33+
myDateFormat = dateFormat;
34+
myNumberOfTimeZoneIds = numberOfTimeZones;
35+
myTimeZoneIds = timeZoneIds;
36+
timeZoneOffsetToUtcMillis = (long*) malloc(numberOfTimeZones * sizeof(long));
37+
}
38+
39+
void WorldClockClient::updateTime() {
40+
JsonStreamingParser parser;
41+
parser.setListener(this);
42+
WiFiClient client;
43+
44+
// http://api.thingspeak.com/channels/CHANNEL_ID/feeds.json?results=2&api_key=API_KEY
45+
const char host[] = "oleddisplay.squix.ch";
46+
String url = "/rest/time";
47+
48+
const int httpPort = 80;
49+
if (!client.connect(host, httpPort)) {
50+
Serial.println("connection failed");
51+
return;
52+
}
53+
54+
55+
Serial.print("Requesting URL: ");
56+
Serial.println(url);
57+
58+
// {"language":"de","country":"CH","timeZoneIds":["Europe/Zurich", "Europe/London"],"dateFormat":"dd.MM.YYYY"}
59+
String timeZoneIdJson = "\"timeZoneIds\":[";
60+
for (int i = 0; i < myNumberOfTimeZoneIds; i++) {
61+
if (i > 0) {
62+
timeZoneIdJson +=",";
63+
}
64+
timeZoneIdJson += "\"" + myTimeZoneIds[i] + "\"";
65+
}
66+
timeZoneIdJson += "]";
67+
String request = "{\"language\":\""
68+
+ myLanguage + "\",\"country\":\""
69+
+ myCountry + "\","
70+
+ timeZoneIdJson +",\"dateFormat\":\""
71+
+ myDateFormat +"\"}\r\n\r\n";
72+
Serial.println("Request: " + request);
73+
// This will send the request to the server
74+
client.print("POST " + url + " HTTP/1.1\r\n" +
75+
"Host: " + host + "\r\n" +
76+
"Content-Length: " + String(request.length()) + "\r\n" +
77+
"Connection: close\r\n\r\n");
78+
79+
client.println(request);
80+
81+
while(!client.available()) {
82+
Serial.println(".");
83+
delay(1000);
84+
}
85+
86+
int pos = 0;
87+
boolean isBody = false;
88+
char c;
89+
90+
int size = 0;
91+
client.setNoDelay(false);
92+
while(client.connected()) {
93+
while((size = client.available()) > 0) {
94+
c = client.read();
95+
if (c == '{' || c == '[') {
96+
isBody = true;
97+
}
98+
if (isBody) {
99+
parser.parse(c);
100+
}
101+
}
102+
}
103+
}
104+
105+
106+
String WorldClockClient::getFormattedTime(int timeZoneIndex) {
107+
return getHours(timeZoneIndex) + ":" + getMinutes(timeZoneIndex) + ":" + getSeconds(timeZoneIndex);
108+
}
109+
110+
String WorldClockClient::getHours(int timeZoneIndex) {
111+
if (millisOfDayAtUpdate == 0) {
112+
return "--";
113+
}
114+
int hours = ((getSecondsOfDay(timeZoneIndex) % 86400L) / 3600) % 24;
115+
if (hours < 10) {
116+
return "0" + String(hours);
117+
}
118+
return String(hours); // print the hour (86400 equals secs per day)
119+
}
120+
121+
String WorldClockClient::getMinutes(int timeZoneIndex) {
122+
if (millisOfDayAtUpdate == 0) {
123+
return "--";
124+
}
125+
int minutes = ((getSecondsOfDay(timeZoneIndex) % 3600) / 60);
126+
if (minutes < 10 ) {
127+
// In the first 10 minutes of each hour, we'll want a leading '0'
128+
return "0" + String(minutes);
129+
}
130+
return String(minutes);
131+
}
132+
133+
String WorldClockClient::getSeconds(int timeZoneIndex) {
134+
if (millisOfDayAtUpdate == 0) {
135+
return "--";
136+
}
137+
int seconds = getSecondsOfDay(timeZoneIndex) % 60;
138+
if ( seconds < 10 ) {
139+
// In the first 10 seconds of each minute, we'll want a leading '0'
140+
return "0" + String(seconds);
141+
}
142+
return String(seconds);
143+
144+
}
145+
146+
long WorldClockClient::getSecondsOfDay(int timeZoneIndex) {
147+
return (millisOfDayAtUpdate + millis() - localMillisAtUpdate + timeZoneOffsetToUtcMillis[timeZoneIndex]) / 1000;
148+
}
149+
150+
void WorldClockClient::whitespace(char c) {
151+
152+
}
153+
154+
void WorldClockClient::startDocument() {
155+
156+
}
157+
158+
void WorldClockClient::key(String key) {
159+
currentKey = key;
160+
}
161+
162+
void WorldClockClient::value(String value) {
163+
Serial.println(currentKey + ": " + value);
164+
if (currentKey == "millisOfDayUtc") {
165+
millisOfDayAtUpdate = value.toInt();
166+
localMillisAtUpdate = millis();
167+
} else if (currentKey == "index") {
168+
currentTimeZoneIndex = value.toInt();
169+
Serial.println("\n-->Current index: " + String(currentTimeZoneIndex));
170+
} else if (currentKey == "timeZoneOffsetToUtcMillis") {
171+
Serial.println("\n-->Index: " + String(currentTimeZoneIndex));
172+
Serial.println("\n-->value: " + value);
173+
timeZoneOffsetToUtcMillis[currentTimeZoneIndex] = value.toInt();
174+
}
175+
}
176+
177+
void WorldClockClient::endArray() {
178+
179+
}
180+
181+
void WorldClockClient::endObject() {
182+
183+
}
184+
185+
void WorldClockClient::endDocument() {
186+
187+
}
188+
189+
void WorldClockClient::startArray() {
190+
191+
}
192+
193+
void WorldClockClient::startObject() {
194+
195+
}

WorldClockClient.h

Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
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+
#pragma once
27+
28+
#include <JsonListener.h>
29+
#include <JsonStreamingParser.h>
30+
#include <ESP8266WiFi.h>
31+
#include <WiFiClient.h>
32+
33+
34+
class WorldClockClient: public JsonListener {
35+
private:
36+
long millisOfDayAtUpdate = 0;
37+
long localMillisAtUpdate;
38+
boolean isHeader = true;
39+
String currentKey = "";
40+
String myLanguage;
41+
String myCountry;
42+
String* myTimeZoneIds;
43+
int myNumberOfTimeZoneIds;
44+
String myDateFormat;
45+
46+
int currentTimeZoneIndex;
47+
long* timeZoneOffsetToUtcMillis;
48+
49+
public:
50+
WorldClockClient(String language, String country, String dateFormat, int numberOfTimeZones, String* timeZoneIds);
51+
52+
void updateTime();
53+
54+
String getFormattedTime(int timeZoneIndex);
55+
56+
String getHours(int timeZoneIndex);
57+
58+
String getMinutes(int timeZoneIndex);
59+
60+
String getSeconds(int timeZoneIndex);
61+
62+
long getSecondsOfDay(int timeZoneIndex);
63+
64+
virtual void whitespace(char c);
65+
66+
virtual void startDocument();
67+
68+
virtual void key(String key);
69+
70+
virtual void value(String value);
71+
72+
virtual void endArray();
73+
74+
virtual void endObject();
75+
76+
virtual void endDocument();
77+
78+
virtual void startArray();
79+
80+
virtual void startObject();
81+
};

0 commit comments

Comments
 (0)