Skip to content

Commit cf0810d

Browse files
committed
Fixed Google Calendar example for all Inkplates
1 parent 2813e7e commit cf0810d

File tree

24 files changed

+362
-266
lines changed

24 files changed

+362
-266
lines changed

examples/Inkplate10/Projects/Inkplate10_Google_Calendar/Inkplate10_Google_Calendar.ino

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -106,14 +106,14 @@ void setup()
106106
display.setTextWrap(false);
107107
display.setTextColor(0, 7);
108108

109-
delay(5000);
110-
network.begin();
109+
// Connect Inkplate to the WiFi network
110+
network.begin(ssid, pass);
111111

112-
// Keep trying to get data if it fails the first time
113-
Serial.print("Failed getting data, retrying");
114-
while (!network.getData(data))
112+
// Get the data from Google Calendar
113+
// Repeat attempts until data is fully downloaded
114+
Serial.println("Getting data... ");
115+
while (!network.getData(calendarURL, data))
115116
{
116-
Serial.print('.');
117117
delay(1000);
118118
}
119119

@@ -375,7 +375,7 @@ bool drawEvent(entry *event, int day, int beginY, int maxHeigth, int *heigthNeed
375375
// Gets text bounds
376376
display.getTextBounds(line, 0, 0, &xt1, &yt1, &w, &h);
377377

378-
if (w > (1200 / 3))
378+
if (w > (800 / 3))
379379
{
380380
for (int j = i - 1; j > max(-1, i - 4); --j)
381381
line[j] = '.';
@@ -442,12 +442,12 @@ void drawData()
442442
char *timeStart = strstr(data + i, "DTSTART:") + 8;
443443
char *timeEnd = strstr(data + i, "DTEND:") + 6;
444444

445-
if (summary && summary < end)
445+
if (summary && summary < end && (summary - data) > 0)
446446
{
447447
strncpy(entries[entriesNum].name, summary, strchr(summary, '\n') - summary);
448448
entries[entriesNum].name[strchr(summary, '\n') - summary] = 0;
449449
}
450-
if (location && location < end)
450+
if (location && location < end && (location - data) > 0)
451451
{
452452
strncpy(entries[entriesNum].location, location, strchr(location, '\n') - location);
453453
entries[entriesNum].location[strchr(location, '\n') - location] = 0;

examples/Inkplate10/Projects/Inkplate10_Google_Calendar/Network.cpp

Lines changed: 37 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -16,27 +16,24 @@ Distributed as-is; no warranty is given.
1616

1717
#include "Network.h"
1818

19-
#include <HTTPClient.h>
20-
#include <WiFi.h>
21-
#include <WiFiClientSecure.h>
22-
23-
void Network::begin()
19+
// Connect Inkplate to the WiFi
20+
void Network::begin(char *ssid, char *pass)
2421
{
2522
// Initiating wifi, like in BasicHttpClient example
2623
WiFi.mode(WIFI_STA);
2724
WiFi.begin(ssid, pass);
2825

26+
// Waiting to WiFi connect
2927
int cnt = 0;
3028
Serial.print(F("Waiting for WiFi to connect..."));
3129
while ((WiFi.status() != WL_CONNECTED))
3230
{
31+
// Prints a dot every second that wifi isn't connected
3332
Serial.print(F("."));
3433
delay(1000);
3534
++cnt;
3635

37-
WiFi.reconnect();
38-
delay(5000);
39-
36+
// If it doesn't connect to wifi in 10 seconds, reset the ESP
4037
if (cnt == 10)
4138
{
4239
Serial.println("Can't connect to WIFI, restarting");
@@ -46,14 +43,14 @@ void Network::begin()
4643
}
4744
Serial.println(F(" connected"));
4845

49-
// Find internet time
46+
// Find and print internet time, the timezone will be added later
5047
setTime();
5148
}
5249

5350
// Gets time from ntp server
54-
void Network::getTime(char *timeStr, long offSet)
51+
void Network::getTime(char *timeStr, long offSet, int timeZone)
5552
{
56-
// Get seconds since 1.1.1970.
53+
// Get seconds since 1.1.1970. and add timezone
5754
time_t nowSecs = time(nullptr) + (long)timeZone * 3600L + offSet;
5855

5956
// Used to store time
@@ -64,19 +61,18 @@ void Network::getTime(char *timeStr, long offSet)
6461
strcpy(timeStr, asctime(&timeinfo));
6562
}
6663

67-
// Function to get all war data from web
68-
bool Network::getData(char *data)
64+
// Function to get all raw data from the web
65+
bool Network::getData(char *calendarURL, char *data)
6966
{
7067
// Variable to store fail
7168
bool f = 0;
7269

73-
// If not connected to wifi reconnect wifi
70+
// If not connected to WiFi, reconnect wifi
7471
if (WiFi.status() != WL_CONNECTED)
7572
{
7673
WiFi.reconnect();
7774

78-
delay(5000);
79-
75+
// Waiting to WiFi connect again
8076
int cnt = 0;
8177
Serial.println(F("Waiting for WiFi to reconnect..."));
8278
while ((WiFi.status() != WL_CONNECTED))
@@ -86,9 +82,7 @@ bool Network::getData(char *data)
8682
delay(1000);
8783
++cnt;
8884

89-
WiFi.reconnect();
90-
delay(5000);
91-
85+
// If it doesn't connect to wifi in 10 seconds, reset the ESP
9286
if (cnt == 10)
9387
{
9488
Serial.println("Can't connect to WIFI, restart initiated.");
@@ -100,28 +94,46 @@ bool Network::getData(char *data)
10094

10195
// Http object used to make get request
10296
HTTPClient http;
103-
10497
http.getStream().setTimeout(10);
10598
http.getStream().flush();
99+
http.getStream().setNoDelay(true);
106100

107101
// Begin http by passing url to it
108102
http.begin(calendarURL);
109103

110104
delay(300);
111105

106+
int attempts = 1;
107+
108+
// Download data until it's a verified complete download
112109
// Actually do request
113110
int httpCode = http.GET();
114111

115112
if (httpCode == 200)
116113
{
117114
long n = 0;
118-
while (http.getStream().available())
119-
data[n++] = http.getStream().read();
115+
116+
long now = millis();
117+
118+
while (millis() - now < DOWNLOAD_TIMEOUT)
119+
{
120+
while (http.getStream().available())
121+
{
122+
data[n++] = http.getStream().read();
123+
now = millis();
124+
}
125+
}
126+
120127
data[n++] = 0;
128+
129+
// If the calendar doesn't contain this string - it's invalid
130+
if(strstr(data, "END:VCALENDAR") == NULL) f = 1;
121131
}
122132
else
123133
{
124-
Serial.println(httpCode);
134+
// In case there was another HTTP code, break from the function
135+
Serial.print("HTTP Code: ");
136+
Serial.print(httpCode);
125137
f = 1;
126138
}
127139

@@ -131,6 +143,7 @@ bool Network::getData(char *data)
131143
return !f;
132144
}
133145

146+
// Find internet time
134147
void Network::setTime()
135148
{
136149
// Used for setting correct time
@@ -145,13 +158,13 @@ void Network::setTime()
145158
yield();
146159
nowSecs = time(nullptr);
147160
}
148-
149161
Serial.println();
150162

151163
// Used to store time info
152164
struct tm timeinfo;
153165
gmtime_r(&nowSecs, &timeinfo);
154166

167+
// Print the current time without adding a timezone
155168
Serial.print(F("Current time: "));
156169
Serial.print(asctime(&timeinfo));
157170
}

examples/Inkplate10/Projects/Inkplate10_Google_Calendar/Network.h

Lines changed: 8 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -15,32 +15,24 @@ Distributed as-is; no warranty is given.
1515
*/
1616

1717
#include "Arduino.h"
18-
19-
#include <HTTPClient.h>
20-
#include <WiFi.h>
21-
#include <WiFiClientSecure.h>
22-
23-
// To get timeZone from main file
24-
extern int timeZone;
25-
26-
// Wifi ssid and password
27-
extern char ssid[];
28-
extern char pass[];
29-
30-
extern char calendarURL[];
18+
#include "HTTPClient.h"
19+
#include "WiFi.h"
20+
#include "WiFiClientSecure.h"
3121

3222
#ifndef NETWORK_H
3323
#define NETWORK_H
3424

25+
#define DOWNLOAD_TIMEOUT 1000
26+
3527
// All functions defined in Network.cpp
3628

3729
class Network
3830
{
3931
public:
4032
// Functions we can access in main file
41-
void begin();
42-
void getTime(char *timeStr, long offset = 0);
43-
bool getData(char *data);
33+
void begin(char *ssid, char *pass);
34+
void getTime(char *timeStr, long offset = 0, int timeZone = 0);
35+
bool getData(char *calendarURL, char *data);
4436

4537
private:
4638
// Functions called from within our class

examples/Inkplate2/Projects/Inkplate2_Google_Calendar/Inkplate2_Google_Calendar.ino

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -109,10 +109,11 @@ void setup()
109109

110110
network.begin(ssid, pass); // Connect to wifi and get data
111111

112-
// Keep trying to get data if it fails the first time
113-
while (!network.getData(data, calendarURL))
112+
// Get the data from Google Calendar
113+
// Repeat attempts until data is fully downloaded
114+
Serial.println("Getting data... ");
115+
while (!network.getData(calendarURL, data))
114116
{
115-
Serial.println("Failed getting data, retrying");
116117
delay(1000);
117118
}
118119

@@ -343,13 +344,13 @@ void getEvents()
343344
char *timeStart = strstr(data + i, "DTSTART:") + 8;
344345
char *timeEnd = strstr(data + i, "DTEND:") + 6;
345346

346-
if (summary && summary < end)
347+
if (summary && summary < end && (summary - data) > 0)
347348
{
348349
strncpy(entries[entriesNum].name, summary,
349350
strchr(summary, '\n') - summary); // Copy summary to struct variable name
350351
entries[entriesNum].name[strchr(summary, '\n') - summary] = 0;
351352
}
352-
if (location && location < end)
353+
if (location && location < end && (location - data) > 0)
353354
{
354355
strncpy(entries[entriesNum].location, location,
355356
strchr(location, '\n') - location); // Copy location to struct variable location

examples/Inkplate2/Projects/Inkplate2_Google_Calendar/Network.cpp

Lines changed: 46 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -101,18 +101,17 @@ void Network::getTime(char *timeStr, long offSet, struct tm *timeinfo, int timeZ
101101
*
102102
* @return True if succcessful, false if there was an error
103103
*/
104-
bool Network::getData(char *data, char *calendarURL)
104+
bool Network::getData(char *calendarURL, char *data)
105105
{
106106
// Variable to store fail
107107
bool f = 0;
108108

109-
// If not connected to wifi reconnect wifi
109+
// If not connected to WiFi, reconnect wifi
110110
if (WiFi.status() != WL_CONNECTED)
111111
{
112112
WiFi.reconnect();
113113

114-
delay(5000);
115-
114+
// Waiting to WiFi connect again
116115
int cnt = 0;
117116
Serial.println(F("Waiting for WiFi to reconnect..."));
118117
while ((WiFi.status() != WL_CONNECTED))
@@ -122,9 +121,7 @@ bool Network::getData(char *data, char *calendarURL)
122121
delay(1000);
123122
++cnt;
124123

125-
WiFi.reconnect();
126-
delay(5000);
127-
124+
// If it doesn't connect to wifi in 10 seconds, reset the ESP
128125
if (cnt == 10)
129126
{
130127
Serial.println("Can't connect to WIFI, restart initiated.");
@@ -134,29 +131,53 @@ bool Network::getData(char *data, char *calendarURL)
134131
}
135132
}
136133

134+
// Http object used to make get request
135+
HTTPClient http;
136+
http.getStream().setTimeout(10);
137+
http.getStream().flush();
138+
http.getStream().setNoDelay(true);
139+
137140
// Begin http by passing url to it
138-
bool sleep = WiFi.getSleep();
139-
WiFi.setSleep(false);
141+
http.begin(calendarURL);
142+
143+
delay(300);
140144

141-
// WiFiClientSecure object used to make GET request
142-
WiFiClientSecure client;
145+
int attempts = 1;
143146

144-
int result = getRequest(&client, "calendar.google.com", calendarURL);
145-
if (result == 0)
147+
// Download data until it's a verified complete download
148+
// Actually do request
149+
int httpCode = http.GET();
150+
151+
if (httpCode == 200)
146152
{
147-
Serial.println("HTTP Error!");
148-
Serial.println("Restarting...");
149-
delay(100);
150-
ESP.restart();
151-
}
153+
long n = 0;
154+
155+
long now = millis();
156+
157+
while (millis() - now < DOWNLOAD_TIMEOUT)
158+
{
159+
while (http.getStream().available())
160+
{
161+
data[n++] = http.getStream().read();
162+
now = millis();
163+
}
164+
}
152165

153-
long n = 0;
154-
while (client.available())
155-
data[n++] = client.read();
156-
data[n++] = 0;
157-
delayMicroseconds(2);
166+
data[n++] = 0;
158167

159-
client.stop();
168+
// If the calendar doesn't contain this string - it's invalid
169+
if(strstr(data, "END:VCALENDAR") == NULL) f = 1;
170+
}
171+
else
172+
{
173+
// In case there was another HTTP code, break from the function
174+
Serial.print("HTTP Code: ");
175+
Serial.print(httpCode);
176+
f = 1;
177+
}
178+
179+
// end http
180+
http.end();
160181

161182
return !f;
162183
}
@@ -251,4 +272,4 @@ bool Network::getRequest(WiFiClientSecure *client, char *_api_root_url, char *_a
251272
(void)client->read();
252273

253274
return true;
254-
}
275+
}

0 commit comments

Comments
 (0)