Skip to content

Commit d356bee

Browse files
committed
Implemented ConnectWiFi in Inkplate projects
Credit @koderchina
1 parent f6ef4ce commit d356bee

File tree

176 files changed

+3177
-1985
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

176 files changed

+3177
-1985
lines changed

examples/Inkplate10/Projects/Inkplate10_Crypto_Currency_Tracker/Inkplate10_Crypto_Currency_Tracker.ino

Lines changed: 47 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ char pass[] = "";
3636

3737
// Delay between refreshed calls in miliseconds
3838
#define DELAY_MS 3 * 60 * 1000
39+
#define DELAY_WIFI_RETRY_SECONDS 10
3940

4041
// OPTIONAL:
4142
// change to a different currency
@@ -113,6 +114,7 @@ textElement elements[] = {
113114
};
114115

115116
// Our functions declared below setup and loop
117+
void getTime();
116118
void drawGraph();
117119
void drawAll();
118120

@@ -126,9 +128,32 @@ void setup()
126128
display.setTextWrap(false);
127129
display.setTextColor(0, 7);
128130

129-
// Our begin function
130-
network.begin();
131+
// Try connecting to a WiFi network.
132+
// Parameters are network SSID, password, timeout in seconds and whether to print to serial.
133+
// If the Inkplate isn't able to connect to a network stop further code execution and print an error message.
134+
if (!display.connectWiFi(ssid, pass, WIFI_TIMEOUT, true))
135+
{
136+
// Can't connect to netowrk
137+
// Clear display for the error message
138+
display.clearDisplay();
139+
// Set the font size;
140+
display.setTextSize(3);
141+
// Set the cursor positions and print the text.
142+
display.setCursor((display.width() / 2) - 200, display.height() / 2);
143+
display.print(F("Unable to connect to "));
144+
display.println(F(ssid));
145+
display.setCursor((display.width() / 2) - 200, (display.height() / 2) + 30);
146+
display.println(F("Please check ssid and pass!"));
147+
// Display the error message on the Inkplate and go to deep sleep
148+
display.display();
149+
esp_sleep_enable_timer_wakeup(1000L * DELAY_WIFI_RETRY_SECONDS);
150+
(void)esp_deep_sleep_start();
151+
}
131152

153+
// After connecting to WiFi we need to get internet time from NTP server
154+
setTime();
155+
156+
// After setting the correct time, we can start pulling data about BTC prices.
132157
Serial.print("Retrying retriving data ");
133158
while (!network.getData(data))
134159
{
@@ -153,6 +178,26 @@ void loop()
153178
// Never here
154179
}
155180

181+
// Function for getting time from NTP server
182+
void setTime()
183+
{
184+
// Structure used to hold time information
185+
struct tm timeInfo;
186+
time_t nowSec;
187+
// Fetch current time in epoch format and store it
188+
display.getNTPEpoch(&nowSec);
189+
// This loop ensures that the NTP time fetched is valid and beyond a certain threshold
190+
while (nowSec < 8 * 3600 * 2)
191+
{
192+
delay(500);
193+
yield();
194+
display.getNTPEpoch(&nowSec);
195+
}
196+
gmtime_r(&nowSec, &timeInfo);
197+
Serial.print(F("Current time: "));
198+
Serial.print(asctime(&timeInfo));
199+
}
200+
156201
// Function to draw our graph
157202
void drawGraph()
158203
{

examples/Inkplate10/Projects/Inkplate10_Crypto_Currency_Tracker/Network.cpp

Lines changed: 0 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -58,9 +58,6 @@ void Network::begin()
5858
}
5959
}
6060
Serial.println(F(" connected"));
61-
62-
// Find internet time
63-
setTime();
6461
}
6562

6663
// Gets time from ntp server
@@ -189,29 +186,3 @@ bool Network::getData(double *data)
189186

190187
return !f;
191188
}
192-
193-
// Function for initial time setting ovet the ntp server
194-
void Network::setTime()
195-
{
196-
// Used for setting correct time
197-
configTime(0, 0, "pool.ntp.org", "time.nist.gov");
198-
199-
Serial.print(F("Waiting for NTP time sync: "));
200-
time_t nowSecs = time(nullptr);
201-
while (nowSecs < 8 * 3600 * 2)
202-
{
203-
delay(500);
204-
Serial.print(F("."));
205-
yield();
206-
nowSecs = time(nullptr);
207-
}
208-
209-
Serial.println();
210-
211-
// Used to store time info
212-
struct tm timeinfo;
213-
gmtime_r(&nowSecs, &timeinfo);
214-
215-
Serial.print(F("Current time: "));
216-
Serial.print(asctime(&timeinfo));
217-
}

examples/Inkplate10/Projects/Inkplate10_Daily_Weather_Station/Inkplate10_Daily_Weather_Station.ino

Lines changed: 47 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,7 @@ char apiKey[] = "";
5757

5858
// Delay between API calls, about 1000 per month, which is the free tier limit
5959
#define DELAY_MS 267800L
60+
#define DELAY_WIFI_RETRY_SECONDS 10
6061

6162
// Inkplate object
6263
Inkplate display(INKPLATE_1BIT);
@@ -92,7 +93,7 @@ RTC_DATA_ATTR char temps[4][8] = {
9293
"-",
9394
};
9495

95-
// Variables for storing hour strings
96+
// Variables for storing hour stringsF
9697
RTC_DATA_ATTR uint8_t hours = 0;
9798

9899
// Variables for storing current time and weather info
@@ -115,15 +116,37 @@ void drawCurrent();
115116
void drawTemps();
116117
void drawCity();
117118
void drawTime();
119+
void setTime();
118120

119121
void setup()
120122
{
121123
// Begin serial and display
122124
Serial.begin(115200);
123125
display.begin();
124126

125-
// Calling our begin from network.h file
126-
network.begin(ssid, pass);
127+
// Try connecting to a WiFi network.
128+
// Parameters are network SSID, password, timeout in seconds and whether to print to serial.
129+
// If the Inkplate isn't able to connect to a network stop further code execution and print an error message.
130+
if (!display.connectWiFi(ssid, pass, WIFI_TIMEOUT, true))
131+
{
132+
//Can't connect to netowrk
133+
// Clear display for the error message
134+
display.clearDisplay();
135+
// Set the font size;
136+
display.setTextSize(3);
137+
// Set the cursor positions and print the text.
138+
display.setCursor((display.width() / 2) - 200, display.height() / 2);
139+
display.print(F("Unable to connect to "));
140+
display.println(F(ssid));
141+
display.setCursor((display.width() / 2) - 200, (display.height() / 2) + 30);
142+
display.println(F("Please check ssid and pass!"));
143+
// Display the error message on the Inkplate and go to deep sleep
144+
display.display();
145+
esp_sleep_enable_timer_wakeup(100L * DELAY_WIFI_RETRY_SECONDS);
146+
(void)esp_deep_sleep_start();
147+
}
148+
149+
setTime();
127150

128151
// Get all relevant data, see Network.cpp for info
129152
if (refreshes % fullRefresh == 0)
@@ -163,6 +186,27 @@ void loop()
163186
// nothing here
164187
}
165188

189+
// Function for getting time from NTP server
190+
void setTime()
191+
{
192+
// Structure used to hold time information
193+
struct tm timeInfo;
194+
display.getNTPDateTime(&timeInfo);
195+
time_t nowSec;
196+
// Fetch current time in epoch format and store it
197+
display.getNTPEpoch(&nowSec);
198+
// This loop ensures that the NTP time fetched is valid and beyond a certain threshold
199+
while (nowSec < 8 * 3600 * 2)
200+
{
201+
delay(500);
202+
yield();
203+
display.getNTPEpoch(&nowSec);
204+
}
205+
gmtime_r(&nowSec, &timeInfo);
206+
Serial.print(F("Current time: "));
207+
Serial.print(asctime(&timeInfo));
208+
}
209+
166210
// Function for drawing weather info
167211
void drawWeather()
168212
{

examples/Inkplate10/Projects/Inkplate10_Daily_Weather_Station/Network.cpp

Lines changed: 0 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -43,9 +43,6 @@ void Network::begin(char *ssid, char *pass)
4343
}
4444
}
4545
Serial.println(F(" connected"));
46-
47-
// Find internet time
48-
setTime();
4946
}
5047

5148
// Gets time from ntp server
@@ -227,32 +224,6 @@ bool Network::getData(char *lat, char *lon, char *apiKey, char *city, char *temp
227224
return 1;
228225
}
229226

230-
void Network::setTime()
231-
{
232-
// Used for setting correct time
233-
configTime(0, 0, "pool.ntp.org", "time.nist.gov");
234-
235-
Serial.print(F("Waiting for NTP time sync: "));
236-
time_t nowSecs = time(nullptr);
237-
while (nowSecs < 8 * 3600 * 2)
238-
{
239-
// Print a dot every half a second while time is not set
240-
delay(500);
241-
Serial.print(F("."));
242-
yield();
243-
nowSecs = time(nullptr);
244-
}
245-
246-
Serial.println();
247-
248-
// Used to store time info
249-
struct tm timeinfo;
250-
gmtime_r(&nowSecs, &timeinfo);
251-
252-
Serial.print(F("Current time: "));
253-
Serial.print(asctime(&timeinfo));
254-
}
255-
256227
void Network::getDays(char *day, char *day1, char *day2, char *day3)
257228
{
258229
// Seconds since 1.1.1970.

examples/Inkplate10/Projects/Inkplate10_Google_Calendar/Inkplate10_Google_Calendar.ino

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,7 @@ int timeZone = 2;
5555

5656
// Delay between API calls
5757
#define DELAY_MS 1 * 60000
58+
#define DELAY_WIFI_RETRY_SECONDS 10
5859

5960
// Variables to keep count of when to get new data, and when to just update time
6061
RTC_DATA_ATTR unsigned int refreshes = 0;
@@ -125,7 +126,7 @@ void setup()
125126
display.println(F("Please check SSID and PASS!"));
126127
// Display the error message on the Inkplate and go to deep sleep
127128
display.display();
128-
esp_sleep_enable_timer_wakeup(1000L * DELAY_MS);
129+
esp_sleep_enable_timer_wakeup(1000L * DELAY_WIFI_RETRY_SECONDS);
129130
(void)esp_deep_sleep_start();
130131
}
131132

@@ -499,9 +500,7 @@ void drawData()
499500
getToFrom(entries[entriesNum].time, timeStart, timeEnd, &entries[entriesNum].day,
500501
&entries[entriesNum].timeStamp);
501502
}
502-
// Increment the counter
503503
++entriesNum;
504-
// If we're over the limit, exit the loading loop
505504
if(entriesNum == 128)
506505
{
507506
break;
@@ -515,7 +514,6 @@ void drawData()
515514
bool clogged[3] = {0};
516515
int cloggedCount[3] = {0};
517516

518-
// If required, uncomment this line of debug if required, could be useful:
519517
//Serial.println("Displaying events one by one. There is " + String(entriesNum) + " events to display.");
520518
// Displaying events one by one
521519
for (int i = 0; i < entriesNum; ++i)
@@ -534,6 +532,7 @@ void drawData()
534532
// If it overflowed, set column to clogged and add one event as not shown
535533
if (!s)
536534
{
535+
//Serial.println("Event " + String(i) + " has overflowed.");
537536
++cloggedCount[entries[i].day];
538537
clogged[entries[i].day] = 1;
539538
}

examples/Inkplate10/Projects/Inkplate10_Google_Calendar/Network.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ void Network::begin(char *ssid, char *pass)
4444
Serial.println(F(" connected"));
4545

4646
// Find and print internet time, the timezone will be added later
47-
setTime();
47+
//setTime();
4848
}
4949

5050
// Gets time from ntp server

0 commit comments

Comments
 (0)