From d356beeb160bd71b9712e6135456dee88ab14c9e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Robert=20Sori=C4=87?= Date: Tue, 17 Sep 2024 11:39:42 +0200 Subject: [PATCH] Implemented ConnectWiFi in Inkplate projects Credit @koderchina --- .../Inkplate10_Crypto_Currency_Tracker.ino | 49 ++++- .../Network.cpp | 29 --- .../Inkplate10_Daily_Weather_Station.ino | 50 ++++- .../Network.cpp | 29 --- .../Inkplate10_Google_Calendar.ino | 7 +- .../Inkplate10_Google_Calendar/Network.cpp | 2 +- .../Inkplate10_Hourly_Weather_Station.ino | 185 +++++++++++++++++- .../Network.cpp | 28 --- .../Inkplate10_News_API.ino | 101 +++++++++- .../Projects/Inkplate10_News_API/Network.cpp | 26 --- .../Projects/Inkplate10_News_API/Network.h | 3 +- .../Inkplate10_OpenWeather_Station.ino | 62 +++--- .../Inkplate10_Quotables.ino | 30 ++- .../Projects/Inkplate10_Quotables/Network.cpp | 4 +- .../Inkplate10_Youtube_Subscriber_Counter.ino | 26 ++- .../Network.cpp | 5 +- .../Inkplate2_Clock/Inkplate2_Clock.ino | 27 ++- .../Inkplate2_Crypto_Currency_Tracker.ino | 34 +++- .../Network.cpp | 29 --- .../Network.h | 2 - .../Inkplate2_Daily_Weather_Station.ino | 34 +++- .../Network.cpp | 27 +-- .../Inkplate2_Daily_Weather_Station/Network.h | 1 - .../Inkplate2_Google_Calendar.ino | 36 +++- .../Inkplate2_Google_Calendar/Network.cpp | 33 ---- .../Inkplate2_Google_Calendar/Network.h | 2 - .../Inkplate2_Hourly_Weather_Station.ino | 33 +++- .../Network.cpp | 29 --- .../Network.h | 2 - ...Inkplate2_Kickstarter_Campaign_Tracker.ino | 50 ++++- .../Inkplate2_Quotables.ino | 23 ++- .../Inkplate2_World_Clock.ino | 32 ++- .../Inkplate2_Youtube_Subscriber_Counter.ino | 23 ++- .../Inkplate4_Crypto_Currency_Tracker.ino | 34 +++- .../Network.cpp | 35 +--- .../Network.h | 2 - .../Inkplate4_Daily_Weather_Station.ino | 33 +++- .../Network.cpp | 31 +-- .../Inkplate4_Daily_Weather_Station/Network.h | 2 - .../Inkplate4_Google_Calendar.ino | 36 +++- .../Inkplate4_Google_Calendar/Network.cpp | 29 --- .../Inkplate4_Google_Calendar/Network.h | 2 - .../Inkplate4_Hourly_Weather_Station.ino | 34 +++- .../Network.cpp | 28 --- .../Network.h | 2 - .../Inkplate4_News_API/Inkplate4_News_API.ino | 26 ++- .../Inkplate4_OpenWeather_Station.ino | 64 +++--- .../Inkplate4_Quotables.ino | 24 ++- .../Inkplate4_Youtube_Subscriber_Counter.ino | 24 ++- ...kplate4TEMPERA_Crypto_Currency_Checker.ino | 35 +++- .../Network.cpp | 29 --- .../Network.h | 2 - ...kplate4TEMPERA_Crypto_Currency_Tracker.ino | 35 +++- .../Network.cpp | 25 --- .../Network.h | 2 - ...Inkplate4TEMPERA_Daily_Weather_Station.ino | 33 +++- .../Network.cpp | 29 --- .../Network.h | 2 - .../Inkplate4TEMPERA_Google_Calendar.ino | 53 ++++- .../Network.cpp | 28 --- .../Network.h | 2 - ...nkplate4TEMPERA_Hourly_Weather_Station.ino | 35 +++- .../Network.cpp | 28 --- .../Network.h | 2 - .../Inkplate4TEMPERA_News_API.ino | 49 ++++- .../Inkplate4TEMPERA_News_API/Network.cpp | 33 +--- .../Inkplate4TEMPERA_News_API/Network.h | 2 - .../Inkplate4TEMPERA_OpenWeather_Station.ino | 65 +++--- .../Inkplate4TEMPERA_Quotables.ino | 28 ++- ...ate4TEMPERA_Youtube_Subscriber_Counter.ino | 25 ++- .../Inkplate5_Crypto_Currency_Tracker.ino | 33 +++- .../Network.cpp | 33 ---- .../Network.h | 2 - .../Inkplate5_Daily_Weather_Station.ino | 33 +++- .../Network.cpp | 31 +-- .../Inkplate5_Daily_Weather_Station/Network.h | 2 - .../Inkplate5_Google_Calendar.ino | 36 +++- .../Inkplate5_Google_Calendar/Network.cpp | 29 --- .../Inkplate5_Google_Calendar/Network.h | 2 - .../Inkplate5_Hourly_Weather_Station.ino | 34 +++- .../Network.cpp | 29 --- .../Network.h | 2 - .../Inkplate5_News_API/Inkplate5_News_API.ino | 26 ++- .../Inkplate5_OpenWeather_Station.ino | 119 +++++------ .../Inkplate5_Quotables.ino | 24 ++- .../Inkplate5_Youtube_Subscriber_Counter.ino | 25 ++- .../Inkplate5V2_Crypto_Currency_Tracker.ino | 48 ++++- .../Network.cpp | 34 +--- .../Network.h | 2 - .../Inkplate5V2_Daily_Weather_Station.ino | 48 ++++- .../Network.cpp | 29 --- .../Network.h | 2 - .../Inkplate5V2_Google_Calendar.ino | 52 ++++- .../Inkplate5V2_Google_Calendar/Network.cpp | 30 +-- .../Inkplate5V2_Google_Calendar/Network.h | 2 - .../Inkplate5V2_Hourly_Weather_Station.ino | 48 ++++- .../Network.cpp | 29 --- .../Network.h | 3 - .../Inkplate5V2_News_API.ino | 25 ++- .../Inkplate5V2_OpenWeather_Station.ino | 89 +++++---- .../Inkplate5V2_Quotables.ino | 24 ++- ...Inkplate5V2_Youtube_Subscriber_Counter.ino | 24 ++- .../Inkplate6_Crypto_Currency_Tracker.ino | 50 ++++- .../Network.cpp | 28 --- .../Network.h | 2 - .../Inkplate6_Daily_Weather_Station.ino | 47 ++++- .../Network.cpp | 26 --- .../Inkplate6_Daily_Weather_Station/Network.h | 2 - .../Inkplate6_Google_Calendar.ino | 52 ++++- .../Inkplate6_Google_Calendar/Network.cpp | 28 --- .../Inkplate6_Google_Calendar/Network.h | 2 - .../Inkplate6_Hourly_Weather_Station.ino | 48 ++++- .../Network.cpp | 29 --- .../Network.h | 2 - .../Inkplate6_News_API/Inkplate6_News_API.ino | 49 ++++- .../Projects/Inkplate6_News_API/Network.cpp | 27 --- .../Projects/Inkplate6_News_API/Network.h | 3 - .../Inkplate6_OpenWeather_Station.ino | 97 +++++---- .../Inkplate6_Quotables.ino | 25 ++- .../Inkplate6_Youtube_Subscriber_Counter.ino | 24 ++- ...Inkplate6COLOR_Crypto_Currency_Tracker.ino | 49 ++++- .../Network.cpp | 29 --- .../Network.h | 2 - .../Inkplate6COLOR_Daily_Weather_Station.ino | 48 ++++- .../Network.cpp | 29 --- .../Network.h | 2 - .../Inkplate6COLOR_Google_Calendar.ino | 51 ++++- .../Network.cpp | 28 --- .../Inkplate6COLOR_Google_Calendar/Network.h | 2 - .../Inkplate6COLOR_Hourly_Weather_Station.ino | 48 ++++- .../Network.cpp | 29 --- .../Network.h | 2 - .../Inkplate6COLOR_News_API.ino | 49 ++++- .../Inkplate6COLOR_News_API/Network.cpp | 29 +-- .../Inkplate6COLOR_News_API/Network.h | 2 - .../Inkplate6COLOR_Quotables.ino | 25 ++- ...Inkplate6FLICK_Crypto_Currency_Checker.ino | 49 ++++- .../Network.cpp | 27 --- .../Network.h | 2 - ...Inkplate6FLICK_Crypto_Currency_Tracker.ino | 82 ++++++-- .../Network.cpp | 31 +-- .../Network.h | 2 - .../Inkplate6FLICK_Daily_Weather_Station.ino | 47 ++++- .../Network.cpp | 29 --- .../Network.h | 2 - .../Inkplate6FLICK_Google_Calendar.ino | 51 ++++- .../Network.cpp | 28 --- .../Inkplate6FLICK_Google_Calendar/Network.h | 2 - .../Inkplate6FLICK_Hourly_Weather_Station.ino | 49 ++++- .../Network.cpp | 29 --- .../Network.h | 2 - .../Inkplate6FLICK_News_API.ino | 69 ++++++- .../Inkplate6FLICK_News_API/Network.cpp | 29 --- .../Inkplate6FLICK_News_API/Network.h | 2 - .../Inkplate6FLICK_OpenWeather_Station.ino | 48 +++-- .../Inkplate6FLICK_Quotables.ino | 24 ++- ...plate6FLICK_Youtube_Subscriber_Counter.ino | 24 ++- .../Inkplate6PLUS_Crypto_Currency_Checker.ino | 49 ++++- .../Network.cpp | 29 --- .../Network.h | 2 - .../Inkplate6PLUS_Crypto_Currency_Tracker.ino | 78 ++++++-- .../Inkplate6PLUS_Daily_Weather_Station.ino | 48 ++++- .../Network.cpp | 29 --- .../Network.h | 2 - .../Inkplate6PLUS_Google_Calendar.ino | 50 ++++- .../Inkplate6PLUS_Google_Calendar/Network.cpp | 28 --- .../Inkplate6PLUS_Google_Calendar/Network.h | 2 - .../Inkplate6PLUS_Hourly_Weather_Station.ino | 49 ++++- .../Network.cpp | 29 --- .../Network.h | 2 - .../Inkplate6PLUS_News_API.ino | 49 ++++- .../Inkplate6PLUS_News_API/Network.cpp | 29 --- .../Projects/Inkplate6PLUS_News_API/Network.h | 2 - .../Inkplate6PLUS_OpenWeather_Station.ino | 95 ++++----- .../Inkplate6PLUS_Quotables.ino | 23 ++- ...kplate6PLUS_Youtube_Subscriber_Counter.ino | 24 ++- 176 files changed, 3177 insertions(+), 1985 deletions(-) diff --git a/examples/Inkplate10/Projects/Inkplate10_Crypto_Currency_Tracker/Inkplate10_Crypto_Currency_Tracker.ino b/examples/Inkplate10/Projects/Inkplate10_Crypto_Currency_Tracker/Inkplate10_Crypto_Currency_Tracker.ino index e432acb9..e3ade6eb 100644 --- a/examples/Inkplate10/Projects/Inkplate10_Crypto_Currency_Tracker/Inkplate10_Crypto_Currency_Tracker.ino +++ b/examples/Inkplate10/Projects/Inkplate10_Crypto_Currency_Tracker/Inkplate10_Crypto_Currency_Tracker.ino @@ -36,6 +36,7 @@ char pass[] = ""; // Delay between refreshed calls in miliseconds #define DELAY_MS 3 * 60 * 1000 +#define DELAY_WIFI_RETRY_SECONDS 10 // OPTIONAL: // change to a different currency @@ -113,6 +114,7 @@ textElement elements[] = { }; // Our functions declared below setup and loop +void getTime(); void drawGraph(); void drawAll(); @@ -126,9 +128,32 @@ void setup() display.setTextWrap(false); display.setTextColor(0, 7); - // Our begin function - network.begin(); + // Try connecting to a WiFi network. + // Parameters are network SSID, password, timeout in seconds and whether to print to serial. + // If the Inkplate isn't able to connect to a network stop further code execution and print an error message. + if (!display.connectWiFi(ssid, pass, WIFI_TIMEOUT, true)) + { + // Can't connect to netowrk + // Clear display for the error message + display.clearDisplay(); + // Set the font size; + display.setTextSize(3); + // Set the cursor positions and print the text. + display.setCursor((display.width() / 2) - 200, display.height() / 2); + display.print(F("Unable to connect to ")); + display.println(F(ssid)); + display.setCursor((display.width() / 2) - 200, (display.height() / 2) + 30); + display.println(F("Please check ssid and pass!")); + // Display the error message on the Inkplate and go to deep sleep + display.display(); + esp_sleep_enable_timer_wakeup(1000L * DELAY_WIFI_RETRY_SECONDS); + (void)esp_deep_sleep_start(); + } + // After connecting to WiFi we need to get internet time from NTP server + setTime(); + + // After setting the correct time, we can start pulling data about BTC prices. Serial.print("Retrying retriving data "); while (!network.getData(data)) { @@ -153,6 +178,26 @@ void loop() // Never here } +// Function for getting time from NTP server +void setTime() +{ + // Structure used to hold time information + struct tm timeInfo; + time_t nowSec; + // Fetch current time in epoch format and store it + display.getNTPEpoch(&nowSec); + // This loop ensures that the NTP time fetched is valid and beyond a certain threshold + while (nowSec < 8 * 3600 * 2) + { + delay(500); + yield(); + display.getNTPEpoch(&nowSec); + } + gmtime_r(&nowSec, &timeInfo); + Serial.print(F("Current time: ")); + Serial.print(asctime(&timeInfo)); +} + // Function to draw our graph void drawGraph() { diff --git a/examples/Inkplate10/Projects/Inkplate10_Crypto_Currency_Tracker/Network.cpp b/examples/Inkplate10/Projects/Inkplate10_Crypto_Currency_Tracker/Network.cpp index 17807707..70f3f432 100644 --- a/examples/Inkplate10/Projects/Inkplate10_Crypto_Currency_Tracker/Network.cpp +++ b/examples/Inkplate10/Projects/Inkplate10_Crypto_Currency_Tracker/Network.cpp @@ -58,9 +58,6 @@ void Network::begin() } } Serial.println(F(" connected")); - - // Find internet time - setTime(); } // Gets time from ntp server @@ -189,29 +186,3 @@ bool Network::getData(double *data) return !f; } - -// Function for initial time setting ovet the ntp server -void Network::setTime() -{ - // Used for setting correct time - configTime(0, 0, "pool.ntp.org", "time.nist.gov"); - - Serial.print(F("Waiting for NTP time sync: ")); - time_t nowSecs = time(nullptr); - while (nowSecs < 8 * 3600 * 2) - { - delay(500); - Serial.print(F(".")); - yield(); - nowSecs = time(nullptr); - } - - Serial.println(); - - // Used to store time info - struct tm timeinfo; - gmtime_r(&nowSecs, &timeinfo); - - Serial.print(F("Current time: ")); - Serial.print(asctime(&timeinfo)); -} diff --git a/examples/Inkplate10/Projects/Inkplate10_Daily_Weather_Station/Inkplate10_Daily_Weather_Station.ino b/examples/Inkplate10/Projects/Inkplate10_Daily_Weather_Station/Inkplate10_Daily_Weather_Station.ino index dca28ccc..83e06aee 100644 --- a/examples/Inkplate10/Projects/Inkplate10_Daily_Weather_Station/Inkplate10_Daily_Weather_Station.ino +++ b/examples/Inkplate10/Projects/Inkplate10_Daily_Weather_Station/Inkplate10_Daily_Weather_Station.ino @@ -57,6 +57,7 @@ char apiKey[] = ""; // Delay between API calls, about 1000 per month, which is the free tier limit #define DELAY_MS 267800L +#define DELAY_WIFI_RETRY_SECONDS 10 // Inkplate object Inkplate display(INKPLATE_1BIT); @@ -92,7 +93,7 @@ RTC_DATA_ATTR char temps[4][8] = { "-", }; -// Variables for storing hour strings +// Variables for storing hour stringsF RTC_DATA_ATTR uint8_t hours = 0; // Variables for storing current time and weather info @@ -115,6 +116,7 @@ void drawCurrent(); void drawTemps(); void drawCity(); void drawTime(); +void setTime(); void setup() { @@ -122,8 +124,29 @@ void setup() Serial.begin(115200); display.begin(); - // Calling our begin from network.h file - network.begin(ssid, pass); + // Try connecting to a WiFi network. + // Parameters are network SSID, password, timeout in seconds and whether to print to serial. + // If the Inkplate isn't able to connect to a network stop further code execution and print an error message. + if (!display.connectWiFi(ssid, pass, WIFI_TIMEOUT, true)) + { + //Can't connect to netowrk + // Clear display for the error message + display.clearDisplay(); + // Set the font size; + display.setTextSize(3); + // Set the cursor positions and print the text. + display.setCursor((display.width() / 2) - 200, display.height() / 2); + display.print(F("Unable to connect to ")); + display.println(F(ssid)); + display.setCursor((display.width() / 2) - 200, (display.height() / 2) + 30); + display.println(F("Please check ssid and pass!")); + // Display the error message on the Inkplate and go to deep sleep + display.display(); + esp_sleep_enable_timer_wakeup(100L * DELAY_WIFI_RETRY_SECONDS); + (void)esp_deep_sleep_start(); + } + + setTime(); // Get all relevant data, see Network.cpp for info if (refreshes % fullRefresh == 0) @@ -163,6 +186,27 @@ void loop() // nothing here } +// Function for getting time from NTP server +void setTime() +{ + // Structure used to hold time information + struct tm timeInfo; + display.getNTPDateTime(&timeInfo); + time_t nowSec; + // Fetch current time in epoch format and store it + display.getNTPEpoch(&nowSec); + // This loop ensures that the NTP time fetched is valid and beyond a certain threshold + while (nowSec < 8 * 3600 * 2) + { + delay(500); + yield(); + display.getNTPEpoch(&nowSec); + } + gmtime_r(&nowSec, &timeInfo); + Serial.print(F("Current time: ")); + Serial.print(asctime(&timeInfo)); +} + // Function for drawing weather info void drawWeather() { diff --git a/examples/Inkplate10/Projects/Inkplate10_Daily_Weather_Station/Network.cpp b/examples/Inkplate10/Projects/Inkplate10_Daily_Weather_Station/Network.cpp index 3075b000..be765ed6 100644 --- a/examples/Inkplate10/Projects/Inkplate10_Daily_Weather_Station/Network.cpp +++ b/examples/Inkplate10/Projects/Inkplate10_Daily_Weather_Station/Network.cpp @@ -43,9 +43,6 @@ void Network::begin(char *ssid, char *pass) } } Serial.println(F(" connected")); - - // Find internet time - setTime(); } // Gets time from ntp server @@ -227,32 +224,6 @@ bool Network::getData(char *lat, char *lon, char *apiKey, char *city, char *temp return 1; } -void Network::setTime() -{ - // Used for setting correct time - configTime(0, 0, "pool.ntp.org", "time.nist.gov"); - - Serial.print(F("Waiting for NTP time sync: ")); - time_t nowSecs = time(nullptr); - while (nowSecs < 8 * 3600 * 2) - { - // Print a dot every half a second while time is not set - delay(500); - Serial.print(F(".")); - yield(); - nowSecs = time(nullptr); - } - - Serial.println(); - - // Used to store time info - struct tm timeinfo; - gmtime_r(&nowSecs, &timeinfo); - - Serial.print(F("Current time: ")); - Serial.print(asctime(&timeinfo)); -} - void Network::getDays(char *day, char *day1, char *day2, char *day3) { // Seconds since 1.1.1970. diff --git a/examples/Inkplate10/Projects/Inkplate10_Google_Calendar/Inkplate10_Google_Calendar.ino b/examples/Inkplate10/Projects/Inkplate10_Google_Calendar/Inkplate10_Google_Calendar.ino index 74123397..9d946d2b 100644 --- a/examples/Inkplate10/Projects/Inkplate10_Google_Calendar/Inkplate10_Google_Calendar.ino +++ b/examples/Inkplate10/Projects/Inkplate10_Google_Calendar/Inkplate10_Google_Calendar.ino @@ -55,6 +55,7 @@ int timeZone = 2; // Delay between API calls #define DELAY_MS 1 * 60000 +#define DELAY_WIFI_RETRY_SECONDS 10 // Variables to keep count of when to get new data, and when to just update time RTC_DATA_ATTR unsigned int refreshes = 0; @@ -125,7 +126,7 @@ void setup() display.println(F("Please check SSID and PASS!")); // Display the error message on the Inkplate and go to deep sleep display.display(); - esp_sleep_enable_timer_wakeup(1000L * DELAY_MS); + esp_sleep_enable_timer_wakeup(1000L * DELAY_WIFI_RETRY_SECONDS); (void)esp_deep_sleep_start(); } @@ -499,9 +500,7 @@ void drawData() getToFrom(entries[entriesNum].time, timeStart, timeEnd, &entries[entriesNum].day, &entries[entriesNum].timeStamp); } - // Increment the counter ++entriesNum; - // If we're over the limit, exit the loading loop if(entriesNum == 128) { break; @@ -515,7 +514,6 @@ void drawData() bool clogged[3] = {0}; int cloggedCount[3] = {0}; - // If required, uncomment this line of debug if required, could be useful: //Serial.println("Displaying events one by one. There is " + String(entriesNum) + " events to display."); // Displaying events one by one for (int i = 0; i < entriesNum; ++i) @@ -534,6 +532,7 @@ void drawData() // If it overflowed, set column to clogged and add one event as not shown if (!s) { + //Serial.println("Event " + String(i) + " has overflowed."); ++cloggedCount[entries[i].day]; clogged[entries[i].day] = 1; } diff --git a/examples/Inkplate10/Projects/Inkplate10_Google_Calendar/Network.cpp b/examples/Inkplate10/Projects/Inkplate10_Google_Calendar/Network.cpp index 35bf72fe..d951750c 100644 --- a/examples/Inkplate10/Projects/Inkplate10_Google_Calendar/Network.cpp +++ b/examples/Inkplate10/Projects/Inkplate10_Google_Calendar/Network.cpp @@ -44,7 +44,7 @@ void Network::begin(char *ssid, char *pass) Serial.println(F(" connected")); // Find and print internet time, the timezone will be added later - setTime(); + //setTime(); } // Gets time from ntp server diff --git a/examples/Inkplate10/Projects/Inkplate10_Hourly_Weather_Station/Inkplate10_Hourly_Weather_Station.ino b/examples/Inkplate10/Projects/Inkplate10_Hourly_Weather_Station/Inkplate10_Hourly_Weather_Station.ino index 140b0309..1dad4dd4 100644 --- a/examples/Inkplate10/Projects/Inkplate10_Hourly_Weather_Station/Inkplate10_Hourly_Weather_Station.ino +++ b/examples/Inkplate10/Projects/Inkplate10_Hourly_Weather_Station/Inkplate10_Hourly_Weather_Station.ino @@ -44,6 +44,8 @@ char pass[] = ""; // Change to your api key, if you don't have one, head over to: // https://openweathermap.org/guide , register and copy the key provided char apiKey[] = ""; +// Also, declare the function to check if the API key is valid +bool checkIfAPIKeyIsValid(char *APIKEY); //---------------------------------- @@ -63,6 +65,8 @@ char apiKey[] = ""; // Delay between API calls, about 1000 per month, which is the free tier limit #define DELAY_MS 267800L +#define DELAY_WIFI_RETRY_SECONDS 10 +#define DELAY_API_RETRY_SECONDS 10 // Inkplate object Inkplate display(INKPLATE_1BIT); @@ -120,11 +124,13 @@ RTC_DATA_ATTR char abbr3[16]; RTC_DATA_ATTR char abbr4[16]; // functions defined below +void formatTime(); void drawWeather(); void drawCurrent(); void drawTemps(); void drawCity(); void drawTime(); +void setTime(); void setup() { @@ -135,11 +141,52 @@ void setup() // Initial cleaning of buffer and physical screen display.clearDisplay(); - // Calling our begin from network.h file - network.begin(city); - // Get all relevant data, see Network.cpp for info - network.getTime(currentTime); + // Connect Inkplate to the WiFi network + // Try connecting to a WiFi network. + // Parameters are network SSID, password, timeout in seconds and whether to print to serial. + // If the Inkplate isn't able to connect to a network stop further code execution and print an error message. + if (!display.connectWiFi(ssid, pass, WIFI_TIMEOUT, true)) + { + //Can't connect to netowrk + // Clear display for the error message + display.clearDisplay(); + // Set the font size; + display.setTextSize(3); + // Set the cursor positions and print the text. + display.setCursor((display.width() / 2) - 200, display.height() / 2); + display.print(F("Unable to connect to ")); + display.println(F(ssid)); + display.setCursor((display.width() / 2) - 200, (display.height() / 2) + 30); + display.println(F("Please check SSID and PASS!")); + // Display the error message on the Inkplate and go to deep sleep + display.display(); + esp_sleep_enable_timer_wakeup(1000L * DELAY_WIFI_RETRY_SECONDS); + (void)esp_deep_sleep_start(); + } + + // Check if we have a valid API key: + Serial.println("Checking if API key is valid..."); + if (!checkIfAPIKeyIsValid(apiKey)) + { + // If we don't, notify the user + Serial.println("API key is invalid!"); + display.clearDisplay(); + display.setCursor(0, 0); + display.setTextSize(2); + display.println("Can't get data from OpenWeatherMaps! Check your API key!"); + display.println("Only older API keys for OneCall 2.5 work in free tier."); + display.println("See the code comments the example for more info."); + display.display(); + // After displaying the error message go to deep sleep + esp_sleep_enable_timer_wakeup(1000L * DELAY_API_RETRY_SECONDS); + (void)esp_deep_sleep_start(); + } + Serial.println("API key is valid!"); + + // A function called to format time into a nice string. + formatTime(); + if (refreshes % fullRefresh == 0) { while (!network.getData(city, temps[0], temps[1], temps[2], temps[3], currentTemp, currentWind, currentTime, @@ -174,6 +221,136 @@ void loop() { } +// Function for getting time from NTP server +void setTime() +{ + // Structure used to hold time information + struct tm timeInfo; + time_t nowSec; + // Fetch current time in epoch format and store it + display.getNTPEpoch(&nowSec); + // This loop ensures that the NTP time fetched is valid and beyond a certain threshold + while (nowSec < 8 * 3600 * 2) + { + delay(500); + yield(); + display.getNTPEpoch(&nowSec); + } + gmtime_r(&nowSec, &timeInfo); + Serial.print(F("Current time: ")); + Serial.print(asctime(&timeInfo)); +} + + +/** + * Make a test API call to see if we have a valid API key + * + * Older keys made for OpenWeather 2.5 OneCall API work, while newer ones won't work, due to the service becoming + * deprecated. + */ +bool checkIfAPIKeyIsValid(char *APIKEY) +{ + bool failed = false; + + // Create the buffer for holding the API response, make it large enough just in case + char *data; + data = (char *)ps_malloc(50000LL); + + // Http object used to make GET request + HTTPClient http; + http.getStream().setTimeout(10); + http.getStream().flush(); + http.getStream().setNoDelay(true); + + // Combine the base URL and the API key to do a test call + char *baseURL = "https://api.openweathermap.org/data/2.5/onecall?lat=45.560001&lon=18.675880&units=metric&appid="; + char apiTestURL[200]; + sprintf(apiTestURL, "%s%s", baseURL, APIKEY); + + // Begin http by passing url to it + http.begin(apiTestURL); + + delay(300); + + // Download data until it's a verified complete download + // Actually do request + int httpCode = http.GET(); + + if (httpCode == 200) + { + long n = 0; + + long now = millis(); + + while (millis() - now < 1000) + { + while (http.getStream().available()) + { + data[n++] = http.getStream().read(); + now = millis(); + } + } + + data[n++] = 0; + + // If the reply constains this string - it's invalid + if (strstr(data, "Invalid API key.") != NULL) + failed = true; + } + else + { + // In case there was another HTTP code, it failed + Serial.print("Error! HTTP Code: "); + Serial.println(httpCode); + failed = true; + } + + // End http + http.end(); + + // Free the memory + free(data); + + return !failed; +} + +void formatTime() +{ + struct tm timeInfo; + display.getNTPDateTime(&timeInfo); + time_t nowSec; + display.getNTPEpoch(&nowSec); + while(nowSec < 8 * 3600 * 2) + { + delay(500); + yield(); + display.getNTPEpoch(&nowSec); + } + gmtime_r(&nowSec, &timeInfo); + Serial.print(F("Current time: ")); + Serial.print(asctime(&timeInfo)); + + // Get seconds since 1.1.1970. + time_t nowSecs = time(nullptr); + + // Used to store time + struct tm timeinfo; + gmtime_r(&nowSecs, &timeinfo); + + // Copies time string into timeStr + strncpy(currentTime, asctime(&timeinfo) + 11, 5); + + // Setting time string timezone + int hr = 10 * (currentTime[0] - '0') + (currentTime[1] - '0') + timeZone; + + // Better defined modulo, in case timezone makes hours to go below 0 + hr = (hr % 24 + 24) % 24; + + // Adding time to '0' char makes it into whatever time char, for both digits + currentTime[0] = hr / 10 + '0'; + currentTime[1] = hr % 10 + '0'; +} + // Function for drawing weather info void drawWeather() { diff --git a/examples/Inkplate10/Projects/Inkplate10_Hourly_Weather_Station/Network.cpp b/examples/Inkplate10/Projects/Inkplate10_Hourly_Weather_Station/Network.cpp index ea65d7dd..87772472 100644 --- a/examples/Inkplate10/Projects/Inkplate10_Hourly_Weather_Station/Network.cpp +++ b/examples/Inkplate10/Projects/Inkplate10_Hourly_Weather_Station/Network.cpp @@ -53,9 +53,6 @@ void Network::begin(char *city) } Serial.println(F(" connected")); - // Find internet time - setTime(); - // reduce power by making WiFi module sleep WiFi.setSleep(1); } @@ -227,31 +224,6 @@ bool Network::getData(char *city, char *temp1, char *temp2, char *temp3, char *t return !f; } -void Network::setTime() -{ - // Used for setting correct time - configTime(0, 0, "pool.ntp.org", "time.nist.gov"); - - Serial.print(F("Waiting for NTP time sync: ")); - time_t nowSecs = time(nullptr); - while (nowSecs < 8 * 3600 * 2) - { - // Print a dot every half a second while time is not set - delay(500); - Serial.print(F(".")); - yield(); - nowSecs = time(nullptr); - } - - Serial.println(); - - // Used to store time info - struct tm timeinfo; - gmtime_r(&nowSecs, &timeinfo); - - Serial.print(F("Current time: ")); - Serial.print(asctime(&timeinfo)); -} void Network::getHours(char *hour1, char *hour2, char *hour3, char *hour4) { diff --git a/examples/Inkplate10/Projects/Inkplate10_News_API/Inkplate10_News_API.ino b/examples/Inkplate10/Projects/Inkplate10_News_API/Inkplate10_News_API.ino index e1ba4cda..71efb32e 100644 --- a/examples/Inkplate10/Projects/Inkplate10_News_API/Inkplate10_News_API.ino +++ b/examples/Inkplate10/Projects/Inkplate10_News_API/Inkplate10_News_API.ino @@ -33,6 +33,8 @@ int timeZone = 2; char ssid[] = ""; char pass[] = ""; char api_key_news[] = ""; //You can obtain one here: https://newsapi.org/ +// Also, declare the function to check if the API key is valid +bool checkIfAPIKeyIsValid(char *APIKEY); //---------------------------------- @@ -53,6 +55,8 @@ Inkplate display(INKPLATE_1BIT); // Delay between API calls in miliseconds (first 60 represents minutes so you can change to your need) #define DELAY_MS (uint32_t)60 * 60 * 1000 +#define DELAY_WIFI_RETRY_SECONDS 10 +#define DELAY_API_RETRY_SECONDS 10 // Variable for counting partial refreshes RTC_DATA_ATTR unsigned refreshes = 0; @@ -68,8 +72,43 @@ void setup() display.begin(); display.setTextWrap(false); - // Our begin function - network.begin(); + // Connect Inkplate to the WiFi network + // Try connecting to a WiFi network. + // Parameters are network SSID, password, timeout in seconds and whether to print to serial. + // If the Inkplate isn't able to connect to a network stop further code execution and print an error message. + if (!display.connectWiFi(ssid, pass, WIFI_TIMEOUT, true)) + { + //Can't connect to netowrk + // Clear display for the error message + display.clearDisplay(); + // Set the font size; + display.setTextSize(3); + // Set the cursor positions and print the text. + display.setCursor((display.width() / 2) - 200, display.height() / 2); + display.print(F("Unable to connect to ")); + display.println(F(ssid)); + display.setCursor((display.width() / 2) - 200, (display.height() / 2) + 30); + display.println(F("Please check SSID and PASS!")); + // Display the error message on the Inkplate and go to deep sleep + display.display(); + esp_sleep_enable_timer_wakeup(1000L * DELAY_WIFI_RETRY_SECONDS); + (void)esp_deep_sleep_start(); + } + + if(!checkIfAPIKeyIsValid(api_key_news)) + { + display.clearDisplay(); + display.setTextSize(3); + display.setCursor((display.width() / 2) - 200, display.height() / 2); + display.print(F("Your API key is invalid or incorrect.")); + display.setCursor((display.width() / 2) - 200, (display.height() / 2) + 30); + display.println(F("Please check your API key.")); + display.display(); + esp_sleep_enable_timer_wakeup(1000L * DELAY_API_RETRY_SECONDS); + (void)esp_deep_sleep_start(); + } + + setTime(); struct news *entities; @@ -150,6 +189,64 @@ void drawNews(struct news *entities) } +// Function for getting time from NTP server +void setTime() +{ + // Structure used to hold time information + struct tm timeInfo; + time_t nowSec; + // Fetch current time in epoch format and store it + display.getNTPEpoch(&nowSec); + // This loop ensures that the NTP time fetched is valid and beyond a certain threshold + while (nowSec < 8 * 3600 * 2) + { + delay(500); + yield(); + nowSec = time(nullptr); + } + gmtime_r(&nowSec, &timeInfo); + Serial.print(F("Current time: ")); + Serial.print(asctime(&timeInfo)); +} + +bool checkIfAPIKeyIsValid(char *APIKEY) +{ + bool failed = false; + // Create the buffer for holding the API response, make it large enough just in case + char *data; + data = (char *)ps_malloc(50000LL); + + // Http object used to make GET request + HTTPClient http; + http.getStream().setTimeout(10); + http.getStream().flush(); + http.getStream().setNoDelay(true); + + // Combine the base URL and the API key to do a test call + char *baseURL = "https://newsapi.org/v2/top-headlines?country=us&apiKey="; + char apiTestURL[200]; + sprintf(apiTestURL, "%s%s", baseURL, APIKEY); + + http.begin(apiTestURL); + delay(300); + + int httpCode = http.GET(); + + if(httpCode != 200) + { + Serial.println("Your API key is invalid or incorrect."); + failed = true; + } + else + { + Serial.println("API key OK."); + } + http.end(); + free(data); + + return !failed; +} + void loop() { // Never here diff --git a/examples/Inkplate10/Projects/Inkplate10_News_API/Network.cpp b/examples/Inkplate10/Projects/Inkplate10_News_API/Network.cpp index a1e810dc..3e322180 100644 --- a/examples/Inkplate10/Projects/Inkplate10_News_API/Network.cpp +++ b/examples/Inkplate10/Projects/Inkplate10_News_API/Network.cpp @@ -61,8 +61,6 @@ void Network::begin() } Serial.println(F(" connected")); - // Find internet time - setTime(); } // Gets time from ntp server @@ -212,28 +210,4 @@ struct news* Network::getData() return ent; } -// Function for initial time setting ovet the ntp server -void Network::setTime() -{ - // Used for setting correct time - configTime(0, 0, "pool.ntp.org", "time.nist.gov"); - - Serial.print(F("Waiting for NTP time sync: ")); - time_t nowSecs = time(nullptr); - while (nowSecs < 8 * 3600 * 2) - { - delay(500); - Serial.print(F(".")); - yield(); - nowSecs = time(nullptr); - } - Serial.println(); - - // Used to store time info - struct tm timeinfo; - gmtime_r(&nowSecs, &timeinfo); - - Serial.print(F("Current time: ")); - Serial.print(asctime(&timeinfo)); -} diff --git a/examples/Inkplate10/Projects/Inkplate10_News_API/Network.h b/examples/Inkplate10/Projects/Inkplate10_News_API/Network.h index c6fcba3b..0f950033 100644 --- a/examples/Inkplate10/Projects/Inkplate10_News_API/Network.h +++ b/examples/Inkplate10/Projects/Inkplate10_News_API/Network.h @@ -52,8 +52,7 @@ class Network struct news* getData(); private: - // Functions called from within our class - void setTime(); + }; #endif diff --git a/examples/Inkplate10/Projects/Inkplate10_OpenWeather_Station/Inkplate10_OpenWeather_Station.ino b/examples/Inkplate10/Projects/Inkplate10_OpenWeather_Station/Inkplate10_OpenWeather_Station.ino index 24e8349f..42ca4b78 100644 --- a/examples/Inkplate10/Projects/Inkplate10_OpenWeather_Station/Inkplate10_OpenWeather_Station.ino +++ b/examples/Inkplate10/Projects/Inkplate10_OpenWeather_Station/Inkplate10_OpenWeather_Station.ino @@ -77,7 +77,8 @@ bool metric = true; //<------------------------------TRUE is METRIC, FALSE is IM // Delay between API calls #define DELAY_MS 59000 - +#define DELAY_WIFI_RETRY_SECONDS 10 +#define DELAY_API_RETRY_SECONDS 10 // Inkplate object Inkplate display(INKPLATE_1BIT); @@ -124,41 +125,12 @@ char Output[200] = {0}; OpenWeatherOneCall OWOC; // Invoke OpenWeather Library time_t t = now(); -void connectWifi() -{ - - int ConnectCount = 20; - - if (WiFi.status() != WL_CONNECTED) - { - while (WiFi.status() != WL_CONNECTED) - { - if (ConnectCount++ == 20) - { - Serial.println("Connect WiFi"); - WiFi.begin(SSID, PASS); - Serial.print("Connecting."); - ConnectCount = 0; - } - Serial.print("."); - delay(1000); - } - Serial.print("\nConnected to: "); - Serial.println(SSID); - Serial.println("IP address: "); - Serial.println(WiFi.localIP()); - Serial.println("Connected WiFi"); - } -} //======================== END WIFI CONNECT ======================= - void GetCurrentWeather() { //================================= // Get the Weather Forecast //================================= - connectWifi(); - Serial.println("Getting weather"); OWOC.parseWeather(APIKEY, NULL, myLatitude, myLongitude, metric, NULL); setTime(OWOC.current.dt); @@ -252,6 +224,7 @@ void setup() } Serial.println("Serial Monitor Initialized"); + // display.begin(); // Initial cleaning of buffer and physical screen @@ -266,7 +239,27 @@ void setup() Serial.println("Welcome to Wol Inkplate 10 weather example!"); display.display(); - connectWifi(); + // Try connecting to a WiFi network. + // Parameters are network SSID, password, timeout in seconds and whether to print to serial. + // If the Inkplate isn't able to connect to a network stop further code execution and print an error message. + if (!display.connectWiFi(SSID, PASS, WIFI_TIMEOUT, true)) + { + //Can't connect to netowrk + // Clear display for the error message + display.clearDisplay(); + // Set the font size; + display.setTextSize(3); + // Set the cursor positions and print the text. + display.setCursor((display.width() / 2) - 200, display.height() / 2); + display.print(F("Unable to connect to ")); + display.println(F(SSID)); + display.setCursor((display.width() / 2) - 200, (display.height() / 2) + 30); + display.println(F("Please check SSID and PASS!")); + // Display the error message on the Inkplate and go to deep sleep + display.display(); + esp_sleep_enable_timer_wakeup(1000L * DELAY_WIFI_RETRY_SECONDS); + (void)esp_deep_sleep_start(); + } // Check if we have a valid API key: Serial.println("Checking if API key is valid..."); @@ -281,16 +274,13 @@ void setup() display.println("Only older API keys for OneCall 2.5 work in free tier."); display.println("See the code comments the example for more info."); display.display(); - while (1) - { - delay(100); - } + esp_sleep_enable_timer_wakeup(1000L * DELAY_API_RETRY_SECONDS); + (void)esp_deep_sleep_start(); } Serial.println("API key is valid!"); // Clear display t = now(); - if ((minute(t) % 30) == 0) // Also returns 0 when time isn't set { GetCurrentWeather(); diff --git a/examples/Inkplate10/Projects/Inkplate10_Quotables/Inkplate10_Quotables.ino b/examples/Inkplate10/Projects/Inkplate10_Quotables/Inkplate10_Quotables.ino index 37e16688..ab878ee5 100644 --- a/examples/Inkplate10/Projects/Inkplate10_Quotables/Inkplate10_Quotables.ino +++ b/examples/Inkplate10/Projects/Inkplate10_Quotables/Inkplate10_Quotables.ino @@ -30,8 +30,8 @@ //---------- CHANGE HERE -------------: // Put in your ssid and password -char ssid[] = ""; -char pass[] = ""; +char ssid[] = "Soldered"; +char pass[] = "dasduino"; //---------------------------------- @@ -52,7 +52,7 @@ Inkplate display(INKPLATE_1BIT); // Delay between API calls in seconds, 300 seconds is 5 minutes #define DELAY_S 300 - +#define DELAY_WIFI_RETRY_SECONDS 10 // Our functions declared below setup and loop void drawAll(); @@ -70,8 +70,28 @@ void setup() display.setTextColor(BLACK); display.setTextSize(3); - // Our begin function - network.begin(); + // Try connecting to a WiFi network. + // Parameters are network SSID, password, timeout in seconds and whether to print to serial. + // If the Inkplate isn't able to connect to a network stop further code execution and print an error message. + if (!display.connectWiFi(ssid, pass, WIFI_TIMEOUT, true)) + { + //Can't connect to netowrk + // Clear display for the error message + display.clearDisplay(); + // Set the font size; + display.setTextSize(3); + // Set the cursor positions and print the text. + display.setCursor((display.width() / 2) - 200, display.height() / 2); + display.print(F("Unable to connect to ")); + display.println(F(ssid)); + display.setCursor((display.width() / 2) - 200, (display.height() / 2) + 30); + display.println(F("Please check SSID and PASS!")); + // Display the error message on the Inkplate and go to deep sleep + display.display(); + esp_sleep_enable_timer_wakeup(1000L * DELAY_WIFI_RETRY_SECONDS); + (void)esp_deep_sleep_start(); + } + while (!network.getData(quote, author)) { Serial.print('.'); diff --git a/examples/Inkplate10/Projects/Inkplate10_Quotables/Network.cpp b/examples/Inkplate10/Projects/Inkplate10_Quotables/Network.cpp index 9277a2d0..3364f1c8 100644 --- a/examples/Inkplate10/Projects/Inkplate10_Quotables/Network.cpp +++ b/examples/Inkplate10/Projects/Inkplate10_Quotables/Network.cpp @@ -99,7 +99,7 @@ bool Network::getData(char* text, char* auth) http.getStream().flush(); // Initiate http - char link[] = "https://api.quotable.io/random"; + char link[] = "https://favqs.com/api/qotd"; http.begin(link); // Actually do request @@ -125,7 +125,7 @@ bool Network::getData(char* text, char* auth) Serial.println("Success"); - const char *buff1 = doc["content"]; + const char *buff1 = doc["body"]; strcpy(text, buff1); diff --git a/examples/Inkplate10/Projects/Inkplate10_Youtube_Subscriber_Counter/Inkplate10_Youtube_Subscriber_Counter.ino b/examples/Inkplate10/Projects/Inkplate10_Youtube_Subscriber_Counter/Inkplate10_Youtube_Subscriber_Counter.ino index 2bf7c50e..44a432f1 100644 --- a/examples/Inkplate10/Projects/Inkplate10_Youtube_Subscriber_Counter/Inkplate10_Youtube_Subscriber_Counter.ino +++ b/examples/Inkplate10/Projects/Inkplate10_Youtube_Subscriber_Counter/Inkplate10_Youtube_Subscriber_Counter.ino @@ -1,4 +1,4 @@ -/* + /* Inkplate10_Youtube_Subscriber_Counter example for Soldered Inkplate 10 For this example you will need only USB cable and Inkplate 10. Select "e-radionica Inkplate10" or "Soldered Inkplate10" from Tools -> Board menu. @@ -42,7 +42,7 @@ // Delay between API calls in miliseconds (10 minutes) #define DELAY_MS 10 * 60 * 1000 - +#define DELAY_WIFI_RETRY_SECONDS 10 // Create object with all networking functions Network network; @@ -125,7 +125,27 @@ void setup() } // Our begin function - network.begin(ssid, pass); + // Try connecting to a WiFi network. + // Parameters are network SSID, password, timeout in seconds and whether to print to serial. + // If the Inkplate isn't able to connect to a network stop further code execution and print an error message. + if (!display.connectWiFi(ssid, pass, WIFI_TIMEOUT, true)) + { + //Can't connect to netowrk + // Clear display for the error message + display.clearDisplay(); + // Set the font size; + display.setTextSize(3); + // Set the cursor positions and print the text. + display.setCursor((display.width() / 2) - 200, display.height() / 2); + display.print(F("Unable to connect to ")); + display.println(F(ssid)); + display.setCursor((display.width() / 2) - 200, (display.height() / 2) + 30); + display.println(F("Please check SSID and PASS!")); + // Display the error message on the Inkplate and go to deep sleep + display.display(); + esp_sleep_enable_timer_wakeup(1000L * DELAY_WIFI_RETRY_SECONDS); + (void)esp_deep_sleep_start(); + } while (!network.getData(&channel, channel_id, api_key, &display)) { diff --git a/examples/Inkplate10/Projects/Inkplate10_Youtube_Subscriber_Counter/Network.cpp b/examples/Inkplate10/Projects/Inkplate10_Youtube_Subscriber_Counter/Network.cpp index dbe91fe8..e6b6834f 100644 --- a/examples/Inkplate10/Projects/Inkplate10_Youtube_Subscriber_Counter/Network.cpp +++ b/examples/Inkplate10/Projects/Inkplate10_Youtube_Subscriber_Counter/Network.cpp @@ -40,12 +40,13 @@ void Network::begin(char *ssid, char *pass) } Serial.println(F(" connected")); - // Create JSON document - doc = new DynamicJsonDocument(20000); } bool Network::getData(channelInfo *channel, char *channel_id, char *api_key, Inkplate *display) { + // Create JSON document + doc = new DynamicJsonDocument(20000); + bool f = 0; // If not connected to wifi reconnect wifi diff --git a/examples/Inkplate2/Projects/Inkplate2_Clock/Inkplate2_Clock.ino b/examples/Inkplate2/Projects/Inkplate2_Clock/Inkplate2_Clock.ino index b4bea5d4..35210021 100644 --- a/examples/Inkplate2/Projects/Inkplate2_Clock/Inkplate2_Clock.ino +++ b/examples/Inkplate2/Projects/Inkplate2_Clock/Inkplate2_Clock.ino @@ -42,7 +42,7 @@ int timeZone = 2; // Put in your ssid and password char ssid[] = ""; char pass[] = ""; - +#define DELAY_WIFI_RETRY_SECONDS 10 //---------------------------------- // Bitmaps for 7 segment display. Converted using Inkplate Image Converter https://inkplate.io/home/image-converter/ @@ -63,6 +63,8 @@ char pass[] = ""; // Array for digital display 7 segment numbers bitmaps const uint8_t *numbers[] = {zero, one, two, three, four, five, six, seven, eight, nine}; +time_t timeEpoch; // Variable to store epoch + struct tm t; // Structure that contains time info void setup() @@ -76,8 +78,29 @@ void setup() display.setTextColor(INKPLATE2_BLACK, INKPLATE2_WHITE); // Our begin function - network.begin(ssid, pass); + if (!display.connectWiFi(ssid, pass, WIFI_TIMEOUT, true)) + { + //Can't connect to netowrk + // Clear display for the error message + display.clearDisplay(); + // Set the font size; + display.setTextSize(1); + // Set the cursor positions and print the text. + display.setCursor(0, 0); + display.print(F("Unable to connect to ")); + display.println(F(ssid)); + display.println(F("Please check SSID and PASS!")); + // Display the error message on the Inkplate and go to deep sleep + display.display(); + esp_sleep_enable_timer_wakeup(1000L * DELAY_WIFI_RETRY_SECONDS); + (void)esp_deep_sleep_start(); + } + // Get the correct epoch from NTP server + Serial.println("Getting time..."); + display.getNTPEpoch(&timeEpoch, timeZone); + Serial.println(gmtime_r(&timeEpoch, &t)); + drawTime(); // Call function drawTime display.display(); // Display content from buffer on Inkplate diff --git a/examples/Inkplate2/Projects/Inkplate2_Crypto_Currency_Tracker/Inkplate2_Crypto_Currency_Tracker.ino b/examples/Inkplate2/Projects/Inkplate2_Crypto_Currency_Tracker/Inkplate2_Crypto_Currency_Tracker.ino index fb8e6c85..efdc8cf3 100644 --- a/examples/Inkplate2/Projects/Inkplate2_Crypto_Currency_Tracker/Inkplate2_Crypto_Currency_Tracker.ino +++ b/examples/Inkplate2/Projects/Inkplate2_Crypto_Currency_Tracker/Inkplate2_Crypto_Currency_Tracker.ino @@ -37,6 +37,7 @@ // Delay between API calls in miliseconds #define DELAY_MS 3 * 60 * 1000 // Every 3 minutes, minute has 60 seconds and second has 1000 miliseconds +#define DELAY_WIFI_RETRY_SECONDS 10 Network network; // Create object with all networking functions @@ -113,10 +114,37 @@ void setup() display.setCursor(10, 10); // Set cursor, custom font uses different method for setting cursor // You can find more about that here https://learn.adafruit.com/adafruit-gfx-graphics-library/using-fonts - display.setTextSize(2); // Set size of font in comparison to original 5x7 font + display.setTextSize(1); // Set size of font in comparison to original 5x7 font - // Our begin function - network.begin(ssid, pass); + // Try connecting to a WiFi network. + // Parameters are network SSID, password, timeout in seconds and whether to print to serial. + // If the Inkplate isn't able to connect to a network stop further code execution and print an error message. + if (!display.connectWiFi(ssid, pass, WIFI_TIMEOUT, true)) + { + // Can't connect to netowrk + // Clear display for the error message + display.clearDisplay(); + // Set the font size; + display.setTextSize(1); + // Set the cursor positions and print the text. + display.setCursor(0, 0); + display.print(F("Unable to connect to ")); + display.println(F(ssid)); + display.println(F("Please check ssid and pass!")); + // Display the error message on the Inkplate and go to deep sleep + display.display(); + esp_sleep_enable_timer_wakeup(1000L * DELAY_WIFI_RETRY_SECONDS); + (void)esp_deep_sleep_start(); + } + + // After connecting to WiFi we need to get internet time from NTP server + time_t nowSec; + struct tm timeInfo; + // Fetch current time in epoch format and store it + display.getNTPEpoch(&nowSec); + gmtime_r(&nowSec, &timeInfo); + Serial.print(F("Current time: ")); + Serial.print(asctime(&timeInfo)); while (!network.getData(data, currency)) // Get data and check if data is successfully fetched { diff --git a/examples/Inkplate2/Projects/Inkplate2_Crypto_Currency_Tracker/Network.cpp b/examples/Inkplate2/Projects/Inkplate2_Crypto_Currency_Tracker/Network.cpp index d75be0ff..4de97afe 100644 --- a/examples/Inkplate2/Projects/Inkplate2_Crypto_Currency_Tracker/Network.cpp +++ b/examples/Inkplate2/Projects/Inkplate2_Crypto_Currency_Tracker/Network.cpp @@ -47,9 +47,6 @@ void Network::begin(char *ssid, char *pass) } } Serial.println(F(" connected")); - - // Find internet time - setTime(); } // Gets time from ntp server @@ -179,29 +176,3 @@ bool Network::getData(double *data, char *currency) return !f; } - -// Function for initial time setting ovet the ntp server -void Network::setTime() -{ - // Used for setting correct time - configTime(0, 0, "pool.ntp.org", "time.nist.gov"); - - Serial.print(F("Waiting for NTP time sync: ")); - time_t nowSecs = time(nullptr); - while (nowSecs < 8 * 3600 * 2) - { - delay(500); - Serial.print(F(".")); - yield(); - nowSecs = time(nullptr); - } - - Serial.println(); - - // Used to store time info - struct tm timeinfo; - gmtime_r(&nowSecs, &timeinfo); - - Serial.print(F("Current time: ")); - Serial.print(asctime(&timeinfo)); -} diff --git a/examples/Inkplate2/Projects/Inkplate2_Crypto_Currency_Tracker/Network.h b/examples/Inkplate2/Projects/Inkplate2_Crypto_Currency_Tracker/Network.h index d6967dde..6e91845f 100644 --- a/examples/Inkplate2/Projects/Inkplate2_Crypto_Currency_Tracker/Network.h +++ b/examples/Inkplate2/Projects/Inkplate2_Crypto_Currency_Tracker/Network.h @@ -34,8 +34,6 @@ class Network bool getData(double *data, char *currency); private: - // Functions called from within our class - void setTime(); }; #endif diff --git a/examples/Inkplate2/Projects/Inkplate2_Daily_Weather_Station/Inkplate2_Daily_Weather_Station.ino b/examples/Inkplate2/Projects/Inkplate2_Daily_Weather_Station/Inkplate2_Daily_Weather_Station.ino index 05462521..8f738188 100644 --- a/examples/Inkplate2/Projects/Inkplate2_Daily_Weather_Station/Inkplate2_Daily_Weather_Station.ino +++ b/examples/Inkplate2/Projects/Inkplate2_Daily_Weather_Station/Inkplate2_Daily_Weather_Station.ino @@ -40,6 +40,7 @@ String lon = "18.5947808"; String apiKey = ""; #define WAKEUP 30 * 60 * 1000 // The time that ESP will be in deep sleep - refresh weather every 30 minutes +#define DELAY_WIFI_RETRY_SECONDS 10 Inkplate display; Network network; @@ -72,14 +73,39 @@ void setup() // Start serial communication Serial.begin(115200); - // Connect Inkplate to the WiFi - network.begin(ssid, password); + // Try connecting to a WiFi network. + // Parameters are network SSID, password, timeout in seconds and whether to print to serial. + // If the Inkplate isn't able to connect to a network stop further code execution and print an error message. + if (!display.connectWiFi(ssid, password, WIFI_TIMEOUT, true)) + { + //Can't connect to netowrk + // Clear display for the error message + display.clearDisplay(); + // Set the font size; + display.setTextSize(1); + // Set the cursor positions and print the text. + display.setCursor(0, 0); + display.print(F("Unable to connect to ")); + display.println(F(ssid)); + display.println(F("Please check ssid and pass!")); + // Display the error message on the Inkplate and go to deep sleep + display.display(); + esp_sleep_enable_timer_wakeup(100L * DELAY_WIFI_RETRY_SECONDS); + (void)esp_deep_sleep_start(); + } + + // After connecting to WiFi we need to get internet time from NTP server + time_t nowSec; + struct tm timeInfo; + // Fetch current time in epoch format and store it + display.getNTPEpoch(&nowSec); + gmtime_r(&nowSec, &timeInfo); + Serial.print(F("Current time: ")); + Serial.print(asctime(&timeInfo)); // Get data from API cnt = network.getData(fw, lat, lon, apiKey, &timezone); - // Get the current time and store it to the variables declared on the top - network.setTime(timezone, &timeinfo); // Get the first day to display day1 = getFirstDay(); diff --git a/examples/Inkplate2/Projects/Inkplate2_Daily_Weather_Station/Network.cpp b/examples/Inkplate2/Projects/Inkplate2_Daily_Weather_Station/Network.cpp index 05446bd7..e04d1071 100644 --- a/examples/Inkplate2/Projects/Inkplate2_Daily_Weather_Station/Network.cpp +++ b/examples/Inkplate2/Projects/Inkplate2_Daily_Weather_Station/Network.cpp @@ -100,29 +100,4 @@ int Network::getData(struct forecastWeather *fw, String lat, String lon, String free(buf); doc.clear(); return cnt; -} - -void Network::setTime(int timezone, struct tm *timeinfo) -{ - // Used for setting correct time - configTime(0, 0, "pool.ntp.org", "time.nist.gov"); - - Serial.print(F("Waiting for NTP time sync: ")); - time_t nowSecs = time(nullptr); - while (nowSecs < 8 * 3600 * 2) - { - delay(500); - Serial.print(F(".")); - yield(); - nowSecs = time(nullptr); - } - - Serial.println(); - - // Used to store time info - nowSecs += timezone; - gmtime_r(&nowSecs, timeinfo); - - Serial.print(F("Current time: ")); - Serial.print(asctime(timeinfo)); -} +} \ No newline at end of file diff --git a/examples/Inkplate2/Projects/Inkplate2_Daily_Weather_Station/Network.h b/examples/Inkplate2/Projects/Inkplate2_Daily_Weather_Station/Network.h index 365e141a..cdca982a 100644 --- a/examples/Inkplate2/Projects/Inkplate2_Daily_Weather_Station/Network.h +++ b/examples/Inkplate2/Projects/Inkplate2_Daily_Weather_Station/Network.h @@ -37,7 +37,6 @@ class Network // Functions we can access in main file void begin(char *ssid, char *pass); int getData(struct forecastWeather *fw, String lat, String lon, String apiKey, int *timezone); - void setTime(int timezone, struct tm *timeinfo); }; #endif diff --git a/examples/Inkplate2/Projects/Inkplate2_Google_Calendar/Inkplate2_Google_Calendar.ino b/examples/Inkplate2/Projects/Inkplate2_Google_Calendar/Inkplate2_Google_Calendar.ino index 8db15f1b..da9e4cf3 100644 --- a/examples/Inkplate2/Projects/Inkplate2_Google_Calendar/Inkplate2_Google_Calendar.ino +++ b/examples/Inkplate2/Projects/Inkplate2_Google_Calendar/Inkplate2_Google_Calendar.ino @@ -41,6 +41,7 @@ // Delay between API calls #define DELAY_MS 4 * 60000 // 4 minutes times 60000 miliseconds in minute +#define DELAY_WIFI_RETRY_SECONDS 10 Inkplate display; // Initiate out Inkplate object @@ -107,7 +108,36 @@ void setup() display.setTextWrap(false); display.setTextColor(INKPLATE2_BLACK, INKPLATE2_WHITE); - network.begin(ssid, pass); // Connect to wifi and get data + // Connect Inkplate to the WiFi network + // Try connecting to a WiFi network. + // Parameters are network SSID, password, timeout in seconds and whether to print to serial. + // If the Inkplate isn't able to connect to a network stop further code execution and print an error message. + if (!display.connectWiFi(ssid, pass, WIFI_TIMEOUT, true)) + { + //Can't connect to netowrk + // Clear display for the error message + display.clearDisplay(); + // Set the font size; + display.setTextSize(1); + // Set the cursor positions and print the text. + display.setCursor(0, 0); + display.print(F("Unable to connect to ")); + display.println(F(ssid)); + display.println(F("Please check SSID and PASS!")); + // Display the error message on the Inkplate and go to deep sleep + display.display(); + esp_sleep_enable_timer_wakeup(1000L * DELAY_WIFI_RETRY_SECONDS); + (void)esp_deep_sleep_start(); + } + + // After connecting to WiFi we need to get internet time from NTP server + time_t nowSec; + struct tm timeInfo; + // Fetch current time in epoch format and store it + display.getNTPEpoch(&nowSec); + gmtime_r(&nowSec, &timeInfo); + Serial.print(F("Current time: ")); + Serial.print(asctime(&timeInfo)); // Get the data from Google Calendar // Repeat attempts until data is fully downloaded @@ -362,6 +392,10 @@ void getEvents() &entries[entriesNum].timeStampEnd, &entries[entriesNum].timeStampStart); } ++entriesNum; + if(entriesNum == 128) + { + break; + } } // Sort entries by time diff --git a/examples/Inkplate2/Projects/Inkplate2_Google_Calendar/Network.cpp b/examples/Inkplate2/Projects/Inkplate2_Google_Calendar/Network.cpp index 01d3731b..16154338 100644 --- a/examples/Inkplate2/Projects/Inkplate2_Google_Calendar/Network.cpp +++ b/examples/Inkplate2/Projects/Inkplate2_Google_Calendar/Network.cpp @@ -56,9 +56,6 @@ void Network::begin(char *ssid, char *pass) } } Serial.println(F(" connected")); - - // Find internet time - setTime(); } /** @@ -183,36 +180,6 @@ bool Network::getData(char *calendarURL, char *data) } -/** - * @brief Set the current time from NTP - * - * @return None - */ -void Network::setTime() -{ - // Used for setting correct time - configTime(0, 0, "pool.ntp.org", "time.nist.gov"); - - Serial.print(F("Waiting for NTP time sync: ")); - time_t nowSecs = time(nullptr); - while (nowSecs < 8 * 3600 * 2) - { - delay(500); - Serial.print(F(".")); - yield(); - nowSecs = time(nullptr); - } - - Serial.println(); - - // Used to store time info - struct tm timeinfo; - gmtime_r(&nowSecs, &timeinfo); - - Serial.print(F("Current time: ")); - Serial.print(asctime(&timeinfo)); -} - /** * @brief Get the current epoch * diff --git a/examples/Inkplate2/Projects/Inkplate2_Google_Calendar/Network.h b/examples/Inkplate2/Projects/Inkplate2_Google_Calendar/Network.h index 34a6c792..18d5b57f 100644 --- a/examples/Inkplate2/Projects/Inkplate2_Google_Calendar/Network.h +++ b/examples/Inkplate2/Projects/Inkplate2_Google_Calendar/Network.h @@ -37,8 +37,6 @@ class Network time_t getEpoch(); private: - // Functions called from within our class - void setTime(); bool getRequest(WiFiClientSecure * client, char * _api_root_url, char * _api_call_url); }; diff --git a/examples/Inkplate2/Projects/Inkplate2_Hourly_Weather_Station/Inkplate2_Hourly_Weather_Station.ino b/examples/Inkplate2/Projects/Inkplate2_Hourly_Weather_Station/Inkplate2_Hourly_Weather_Station.ino index 2df37b4f..59f5864a 100644 --- a/examples/Inkplate2/Projects/Inkplate2_Hourly_Weather_Station/Inkplate2_Hourly_Weather_Station.ino +++ b/examples/Inkplate2/Projects/Inkplate2_Hourly_Weather_Station/Inkplate2_Hourly_Weather_Station.ino @@ -34,6 +34,7 @@ // Delay between API calls, about 1000000 per month, which is the free tier limit #define DELAY_MS 267800L +#define DELAY_WIFI_RETRY_SECONDS 10 // Inkplate object Inkplate display; @@ -97,8 +98,36 @@ void setup() // Initial cleaning of buffer and physical screen display.clearDisplay(); - // Calling our begin from network.h file - network.begin(ssid, pass); + // Connect Inkplate to the WiFi network + // Try connecting to a WiFi network. + // Parameters are network SSID, password, timeout in seconds and whether to print to serial. + // If the Inkplate isn't able to connect to a network stop further code execution and print an error message. + if (!display.connectWiFi(ssid, pass, WIFI_TIMEOUT, true)) + { + //Can't connect to netowrk + // Clear display for the error message + display.clearDisplay(); + // Set the font size; + display.setTextSize(1); + // Set the cursor positions and print the text. + display.setCursor(0, 0); + display.print(F("Unable to connect to ")); + display.println(F(ssid)); + display.println(F("Please check SSID and PASS!")); + // Display the error message on the Inkplate and go to deep sleep + display.display(); + esp_sleep_enable_timer_wakeup(1000L * DELAY_WIFI_RETRY_SECONDS); + (void)esp_deep_sleep_start(); + } + + // After connecting to WiFi we need to get internet time from NTP server + time_t nowSec; + struct tm timeInfo; + // Fetch current time in epoch format and store it + display.getNTPEpoch(&nowSec); + gmtime_r(&nowSec, &timeInfo); + Serial.print(F("Current time: ")); + Serial.print(asctime(&timeInfo)); // Clear display display.clearDisplay(); diff --git a/examples/Inkplate2/Projects/Inkplate2_Hourly_Weather_Station/Network.cpp b/examples/Inkplate2/Projects/Inkplate2_Hourly_Weather_Station/Network.cpp index cf60d9cf..3dce9d00 100644 --- a/examples/Inkplate2/Projects/Inkplate2_Hourly_Weather_Station/Network.cpp +++ b/examples/Inkplate2/Projects/Inkplate2_Hourly_Weather_Station/Network.cpp @@ -48,9 +48,6 @@ void Network::begin(char *ssid, char *pass) } Serial.println(F(" connected")); - // Find internet time - setTime(); - // reduce power by making WiFi module sleep WiFi.setSleep(1); } @@ -202,32 +199,6 @@ bool Network::getData(char *lon, char *lat, char *apiKey, char *temp1, char *tem return !f; } -void Network::setTime() -{ - // Used for setting correct time - configTime(0, 0, "pool.ntp.org", "time.nist.gov"); - - Serial.print(F("Waiting for NTP time sync: ")); - time_t nowSecs = time(nullptr); - while (nowSecs < 8 * 3600 * 2) - { - // Print a dot every half a second while time is not set - delay(500); - Serial.print(F(".")); - yield(); - nowSecs = time(nullptr); - } - - Serial.println(); - - // Used to store time info - struct tm timeinfo; - gmtime_r(&nowSecs, &timeinfo); - - Serial.print(F("Current time: ")); - Serial.print(asctime(&timeinfo)); -} - void Network::getHours(char *hour1, char *hour2, char *hour3) { // Format hours info diff --git a/examples/Inkplate2/Projects/Inkplate2_Hourly_Weather_Station/Network.h b/examples/Inkplate2/Projects/Inkplate2_Hourly_Weather_Station/Network.h index 524b30fb..dcf6476b 100644 --- a/examples/Inkplate2/Projects/Inkplate2_Hourly_Weather_Station/Network.h +++ b/examples/Inkplate2/Projects/Inkplate2_Hourly_Weather_Station/Network.h @@ -39,8 +39,6 @@ class Network time_t dataEpoch = 0; private: - // Functions called from within our class - void setTime(); int timeZone; }; diff --git a/examples/Inkplate2/Projects/Inkplate2_Kickstarter_Campaign_Tracker/Inkplate2_Kickstarter_Campaign_Tracker.ino b/examples/Inkplate2/Projects/Inkplate2_Kickstarter_Campaign_Tracker/Inkplate2_Kickstarter_Campaign_Tracker.ino index 1ce72aaf..028284e8 100644 --- a/examples/Inkplate2/Projects/Inkplate2_Kickstarter_Campaign_Tracker/Inkplate2_Kickstarter_Campaign_Tracker.ino +++ b/examples/Inkplate2/Projects/Inkplate2_Kickstarter_Campaign_Tracker/Inkplate2_Kickstarter_Campaign_Tracker.ino @@ -26,6 +26,7 @@ Inkplate display; Network network; #define DELAY_MS 300000 // Delay in milliseconds between deep sleep and the next wake up -> 5 minutes +#define DELAY_WIFI_RETRY_SECONDS 10 char *ssid = ""; // your network SSID (name of wifi network) char *password = ""; // your network password @@ -47,11 +48,31 @@ void setup() // Initialize Inkplate display.begin(); - // Connect inkplate to wifi - network.begin(ssid, password); + // Connect Inkplate to the WiFi network + // Try connecting to a WiFi network. + // Parameters are network SSID, password, timeout in seconds and whether to print to serial. + // If the Inkplate isn't able to connect to a network stop further code execution and print an error message. + if (!display.connectWiFi(ssid, pass, WIFI_TIMEOUT, true)) + { + //Can't connect to netowrk + // Clear display for the error message + display.clearDisplay(); + // Set the font size; + display.setTextSize(3); + // Set the cursor positions and print the text. + display.setCursor((display.width() / 2) - 200, display.height() / 2); + display.print(F("Unable to connect to ")); + display.println(F(ssid)); + display.setCursor((display.width() / 2) - 200, (display.height() / 2) + 30); + display.println(F("Please check SSID and PASS!")); + // Display the error message on the Inkplate and go to deep sleep + display.display(); + esp_sleep_enable_timer_wakeup(1000L * DELAY_WIFI_RETRY_SECONDS); + (void)esp_deep_sleep_start(); + } + + setTime(); - // Set the current time - network.setTime(&timeinfo, timeZone); // Get data from the API Serial.println("Getting data from API"); @@ -74,6 +95,27 @@ void loop() // time. loop() must be empty! } +// Function for getting time from NTP server +void setTime() +{ + // Structure used to hold time information + struct tm timeInfo; + // Fetch current time from an NTP server and store it + time_t nowSec; + // Fetch current time in epoch format and store it + display.getNTPEpoch(&nowSec); + // This loop ensures that the NTP time fetched is valid and beyond a certain threshold + while(nowSec < 8 * 3600 * 2) + { + delay(500); + yield(); + display.getNTPEpoch(&nowSec); + } + gmtime_r(&nowSec, &timeInfo); + Serial.print(F("Current time: ")); + Serial.print(asctime(&timeInfo)); +} + void drawData(int backers, int pledged, struct tm *timeinfo) { // Print data in the frame buffer the Inkplate diff --git a/examples/Inkplate2/Projects/Inkplate2_Quotables/Inkplate2_Quotables.ino b/examples/Inkplate2/Projects/Inkplate2_Quotables/Inkplate2_Quotables.ino index 1a5e43c9..dd61bdb1 100644 --- a/examples/Inkplate2/Projects/Inkplate2_Quotables/Inkplate2_Quotables.ino +++ b/examples/Inkplate2/Projects/Inkplate2_Quotables/Inkplate2_Quotables.ino @@ -35,6 +35,7 @@ // Delay between API calls in seconds, 300 seconds is 5 minutes #define DELAY_S 300 +#define DELAY_WIFI_RETRY_SECONDS 10 // create object with all networking functions Network network; @@ -61,8 +62,26 @@ void setup() display.setTextWrap(true); // Set text wrapping to true display.setTextColor(INKPLATE2_BLACK, INKPLATE2_WHITE); - // Our begin function - network.begin(ssid, pass); + // Try connecting to a WiFi network. + // Parameters are network SSID, password, timeout in seconds and whether to print to serial. + // If the Inkplate isn't able to connect to a network stop further code execution and print an error message. + if (!display.connectWiFi(ssid, pass, WIFI_TIMEOUT, true)) + { + //Can't connect to netowrk + // Clear display for the error message + display.clearDisplay(); + // Set the font size; + display.setTextSize(1); + // Set the cursor positions and print the text. + display.setCursor(0, 0); + display.print(F("Unable to connect to ")); + display.println(F(ssid)); + display.println(F("Please check SSID and PASS!")); + // Display the error message on the Inkplate and go to deep sleep + display.display(); + esp_sleep_enable_timer_wakeup(1000L * DELAY_WIFI_RETRY_SECONDS); + (void)esp_deep_sleep_start(); + } // Try to get the new random quote from the Internet. while (!network.getData(quote, author, &len, &display)) diff --git a/examples/Inkplate2/Projects/Inkplate2_World_Clock/Inkplate2_World_Clock.ino b/examples/Inkplate2/Projects/Inkplate2_World_Clock/Inkplate2_World_Clock.ino index 84826e50..fe9f0e0a 100644 --- a/examples/Inkplate2/Projects/Inkplate2_World_Clock/Inkplate2_World_Clock.ino +++ b/examples/Inkplate2/Projects/Inkplate2_World_Clock/Inkplate2_World_Clock.ino @@ -34,6 +34,7 @@ #define uS_TO_S_FACTOR 1000000 // Conversion factor for micro seconds to seconds #define TIME_TO_SLEEP 300 // How long ESP32 will be in deep sleep (in seconds) +#define DELAY_WIFI_RETRY_SECONDS 10 // Create object with all networking functions Network network; @@ -66,8 +67,35 @@ void setup() // Init display display.begin(); - // Network begin function - network.begin(ssid, pass); + // Try connecting to a WiFi network. + // Parameters are network SSID, password, timeout in seconds and whether to print to serial. + // If the Inkplate isn't able to connect to a network stop further code execution and print an error message. + if (!display.connectWiFi(ssid, pass, WIFI_TIMEOUT, true)) + { + //Can't connect to netowrk + // Clear display for the error message + display.clearDisplay(); + // Set the font size; + display.setTextSize(1); + // Set the cursor positions and print the text. + display.setCursor(0, 0); + display.print(F("Unable to connect to ")); + display.println(F(ssid)); + display.println(F("Please check SSID and PASS!")); + // Display the error message on the Inkplate and go to deep sleep + display.display(); + esp_sleep_enable_timer_wakeup(1000L * DELAY_WIFI_RETRY_SECONDS); + (void)esp_deep_sleep_start(); + } + + // After connecting to WiFi we need to get internet time from NTP server + time_t nowSec; + struct tm timeInfo; + // Fetch current time in epoch format and store it + display.getNTPEpoch(&nowSec); + gmtime_r(&nowSec, &timeInfo); + Serial.print(F("Current time: ")); + Serial.print(asctime(&timeInfo)); // Get all the available timezones network.getAllCities(); diff --git a/examples/Inkplate2/Projects/Inkplate2_Youtube_Subscriber_Counter/Inkplate2_Youtube_Subscriber_Counter.ino b/examples/Inkplate2/Projects/Inkplate2_Youtube_Subscriber_Counter/Inkplate2_Youtube_Subscriber_Counter.ino index 7c1e9929..b69414df 100644 --- a/examples/Inkplate2/Projects/Inkplate2_Youtube_Subscriber_Counter/Inkplate2_Youtube_Subscriber_Counter.ino +++ b/examples/Inkplate2/Projects/Inkplate2_Youtube_Subscriber_Counter/Inkplate2_Youtube_Subscriber_Counter.ino @@ -40,6 +40,7 @@ // Delay between API calls in miliseconds (10 minutes) #define DELAY_MS 10 * 60 * 1000 +#define DELAY_WIFI_RETRY_SECONDS 10 // Create object with all networking functions Network network; @@ -122,8 +123,26 @@ void setup() delay(1000); } - // Our begin function - network.begin(ssid, pass); + // Try connecting to a WiFi network. + // Parameters are network SSID, password, timeout in seconds and whether to print to serial. + // If the Inkplate isn't able to connect to a network stop further code execution and print an error message. + if (!display.connectWiFi(ssid, pass, WIFI_TIMEOUT, true)) + { + //Can't connect to netowrk + // Clear display for the error message + display.clearDisplay(); + // Set the font size; + display.setTextSize(1); + // Set the cursor positions and print the text. + display.setCursor(0, 0); + display.print(F("Unable to connect to ")); + display.println(F(ssid)); + display.println(F("Please check SSID and PASS!")); + // Display the error message on the Inkplate and go to deep sleep + display.display(); + esp_sleep_enable_timer_wakeup(1000L * DELAY_WIFI_RETRY_SECONDS); + (void)esp_deep_sleep_start(); + } while (!network.getData(&channel, channel_id, api_key, &display)) { diff --git a/examples/Inkplate4/Projects/Inkplate4_Crypto_Currency_Tracker/Inkplate4_Crypto_Currency_Tracker.ino b/examples/Inkplate4/Projects/Inkplate4_Crypto_Currency_Tracker/Inkplate4_Crypto_Currency_Tracker.ino index 7b07b546..823b12ba 100644 --- a/examples/Inkplate4/Projects/Inkplate4_Crypto_Currency_Tracker/Inkplate4_Crypto_Currency_Tracker.ino +++ b/examples/Inkplate4/Projects/Inkplate4_Crypto_Currency_Tracker/Inkplate4_Crypto_Currency_Tracker.ino @@ -37,7 +37,7 @@ char pass[] = ""; // Delay between API calls in miliseconds (first 60 represents minutes so you can change to your need) #define DELAY_MS 3 * 60 * 1000 - +#define DELAY_WIFI_RETRY_SECONDS 10 // OPTIONAL: // Change to a different currency // Use lowercase letters @@ -129,8 +129,36 @@ void setup() display.setTextWrap(false); display.setTextColor(INKPLATE4_BLACK); - // Our begin function - network.begin(ssid, pass); + // Try connecting to a WiFi network. + // Parameters are network SSID, password, timeout in seconds and whether to print to serial. + // If the Inkplate isn't able to connect to a network stop further code execution and print an error message. + if (!display.connectWiFi(ssid, pass, WIFI_TIMEOUT, true)) + { + // Can't connect to netowrk + // Clear display for the error message + display.clearDisplay(); + // Set the font size; + display.setTextSize(3); + // Set the cursor positions and print the text. + display.setCursor((display.width() / 2) - 200, display.height() / 2); + display.print(F("Unable to connect to ")); + display.println(F(ssid)); + display.setCursor((display.width() / 2) - 200, (display.height() / 2) + 30); + display.println(F("Please check ssid and pass!")); + // Display the error message on the Inkplate and go to deep sleep + display.display(); + esp_sleep_enable_timer_wakeup(1000L * DELAY_WIFI_RETRY_SECONDS); + (void)esp_deep_sleep_start(); + } + + // After connecting to WiFi we need to get internet time from NTP server + time_t nowSec; + struct tm timeInfo; + // Fetch current time in epoch format and store it + display.getNTPEpoch(&nowSec); + gmtime_r(&nowSec, &timeInfo); + Serial.print(F("Current time: ")); + Serial.print(asctime(&timeInfo)); Serial.println("Retrying retriving data"); while (!network.getData(data, currency)) diff --git a/examples/Inkplate4/Projects/Inkplate4_Crypto_Currency_Tracker/Network.cpp b/examples/Inkplate4/Projects/Inkplate4_Crypto_Currency_Tracker/Network.cpp index f11503ad..097ee918 100644 --- a/examples/Inkplate4/Projects/Inkplate4_Crypto_Currency_Tracker/Network.cpp +++ b/examples/Inkplate4/Projects/Inkplate4_Crypto_Currency_Tracker/Network.cpp @@ -60,9 +60,6 @@ void Network::begin(char * ssid, char * pass) } } Serial.println(F(" connected")); - - // Find internet time - setTime(); } /** @@ -181,34 +178,4 @@ bool Network::getData(double *data, char * currency) WiFi.setSleep(sleep); return !f; -} - -/** - * @brief Set the internal time with data from NTP server - * - * @returns None -*/ -void Network::setTime() -{ - // Used for setting correct time - configTime(0, 0, "pool.ntp.org", "time.nist.gov"); - - Serial.print(F("Waiting for NTP time sync: ")); - time_t nowSecs = time(nullptr); - while (nowSecs < 8 * 3600 * 2) - { - delay(500); - Serial.print(F(".")); - yield(); - nowSecs = time(nullptr); - } - - Serial.println(); - - // Used to store time info - struct tm timeinfo; - gmtime_r(&nowSecs, &timeinfo); - - Serial.print(F("Current time: ")); - Serial.print(asctime(&timeinfo)); -} +} \ No newline at end of file diff --git a/examples/Inkplate4/Projects/Inkplate4_Crypto_Currency_Tracker/Network.h b/examples/Inkplate4/Projects/Inkplate4_Crypto_Currency_Tracker/Network.h index 39ad2e22..c40005a1 100644 --- a/examples/Inkplate4/Projects/Inkplate4_Crypto_Currency_Tracker/Network.h +++ b/examples/Inkplate4/Projects/Inkplate4_Crypto_Currency_Tracker/Network.h @@ -36,8 +36,6 @@ class Network bool getData(double *data, char * currency); private: - // Functions called from within our class - void setTime(); }; #endif diff --git a/examples/Inkplate4/Projects/Inkplate4_Daily_Weather_Station/Inkplate4_Daily_Weather_Station.ino b/examples/Inkplate4/Projects/Inkplate4_Daily_Weather_Station/Inkplate4_Daily_Weather_Station.ino index 76804766..dd677013 100644 --- a/examples/Inkplate4/Projects/Inkplate4_Daily_Weather_Station/Inkplate4_Daily_Weather_Station.ino +++ b/examples/Inkplate4/Projects/Inkplate4_Daily_Weather_Station/Inkplate4_Daily_Weather_Station.ino @@ -62,6 +62,7 @@ char apiKey[] = ""; // Delay between API calls #define DELAY_MS (uint32_t)10 * 60 * 1000 // 10 minute time +#define DELAY_WIFI_RETRY_SECONDS 10 // Inkplate object Inkplate display; @@ -114,8 +115,36 @@ void setup() Serial.begin(115200); display.begin(); - // Calling our begin from network.h file - network.begin(ssid, pass); + // Try connecting to a WiFi network. + // Parameters are network SSID, password, timeout in seconds and whether to print to serial. + // If the Inkplate isn't able to connect to a network stop further code execution and print an error message. + if (!display.connectWiFi(ssid, pass, WIFI_TIMEOUT, true)) + { + //Can't connect to netowrk + // Clear display for the error message + display.clearDisplay(); + // Set the font size; + display.setTextSize(3); + // Set the cursor positions and print the text. + display.setCursor((display.width() / 2) - 200, display.height() / 2); + display.print(F("Unable to connect to ")); + display.println(F(ssid)); + display.setCursor((display.width() / 2) - 200, (display.height() / 2) + 30); + display.println(F("Please check ssid and pass!")); + // Display the error message on the Inkplate and go to deep sleep + display.display(); + esp_sleep_enable_timer_wakeup(1000L * DELAY_WIFI_RETRY_SECONDS); + (void)esp_deep_sleep_start(); + } + + // After connecting to WiFi we need to get internet time from NTP server + time_t nowSec; + struct tm timeInfo; + // Fetch current time in epoch format and store it + display.getNTPEpoch(&nowSec); + gmtime_r(&nowSec, &timeInfo); + Serial.print(F("Current time: ")); + Serial.print(asctime(&timeInfo)); // Get all relevant data, see Network.cpp for info Serial.print("Fetching data"); diff --git a/examples/Inkplate4/Projects/Inkplate4_Daily_Weather_Station/Network.cpp b/examples/Inkplate4/Projects/Inkplate4_Daily_Weather_Station/Network.cpp index b87ee821..b96f1b2c 100644 --- a/examples/Inkplate4/Projects/Inkplate4_Daily_Weather_Station/Network.cpp +++ b/examples/Inkplate4/Projects/Inkplate4_Daily_Weather_Station/Network.cpp @@ -40,9 +40,6 @@ void Network::begin(char *ssid, char *pass) } } Serial.println(F(" connected")); - - // Find internet time - setTime(); } // Gets time from ntp server @@ -222,30 +219,4 @@ bool Network::getData(char *lat, char *lon, char *apiKey, char *city, char *temp WiFi.setSleep(sleep); return 1; -} - -void Network::setTime() -{ - // Used for setting correct time - configTime(0, 0, "pool.ntp.org", "time.nist.gov"); - - Serial.print(F("Waiting for NTP time sync: ")); - time_t nowSecs = time(nullptr); - while (nowSecs < 8 * 3600 * 2) - { - // Print a dot every half a second while time is not set - delay(500); - Serial.print(F(".")); - yield(); - nowSecs = time(nullptr); - } - - Serial.println(); - - // Used to store time info - struct tm timeinfo; - gmtime_r(&nowSecs, &timeinfo); - - Serial.print(F("Current time: ")); - Serial.print(asctime(&timeinfo)); -} +} \ No newline at end of file diff --git a/examples/Inkplate4/Projects/Inkplate4_Daily_Weather_Station/Network.h b/examples/Inkplate4/Projects/Inkplate4_Daily_Weather_Station/Network.h index 03a0c8f0..d28b8384 100644 --- a/examples/Inkplate4/Projects/Inkplate4_Daily_Weather_Station/Network.h +++ b/examples/Inkplate4/Projects/Inkplate4_Daily_Weather_Station/Network.h @@ -43,8 +43,6 @@ class Network char *currentWeatherAbbr, char *abbr1, char *abbr2, char *abbr3, char *abbr4, uint8_t *hours); private: - // Functions called from within our class - void setTime(); int timeZone = 0; }; diff --git a/examples/Inkplate4/Projects/Inkplate4_Google_Calendar/Inkplate4_Google_Calendar.ino b/examples/Inkplate4/Projects/Inkplate4_Google_Calendar/Inkplate4_Google_Calendar.ino index 6c9363a4..b48dab71 100644 --- a/examples/Inkplate4/Projects/Inkplate4_Google_Calendar/Inkplate4_Google_Calendar.ino +++ b/examples/Inkplate4/Projects/Inkplate4_Google_Calendar/Inkplate4_Google_Calendar.ino @@ -57,6 +57,7 @@ int timeZone = 2; // 2 means UTC+2 // Delay between API calls in seconds #define DELAY_SECS 4 * 60 // Every 4 minutes +#define DELAY_WIFI_RETRY_SECONDS 10 //---------------------------------------------- @@ -106,7 +107,36 @@ void setup() display.setTextColor(INKPLATE4_BLACK, INKPLATE4_WHITE); // Connect Inkplate to the WiFi network - network.begin(ssid, pass); + // Try connecting to a WiFi network. + // Parameters are network SSID, password, timeout in seconds and whether to print to serial. + // If the Inkplate isn't able to connect to a network stop further code execution and print an error message. + if (!display.connectWiFi(ssid, pass, WIFI_TIMEOUT, true)) + { + //Can't connect to netowrk + // Clear display for the error message + display.clearDisplay(); + // Set the font size; + display.setTextSize(3); + // Set the cursor positions and print the text. + display.setCursor((display.width() / 2) - 200, display.height() / 2); + display.print(F("Unable to connect to ")); + display.println(F(ssid)); + display.setCursor((display.width() / 2) - 200, (display.height() / 2) + 30); + display.println(F("Please check SSID and PASS!")); + // Display the error message on the Inkplate and go to deep sleep + display.display(); + esp_sleep_enable_timer_wakeup(1000L * DELAY_WIFI_RETRY_SECONDS); + (void)esp_deep_sleep_start(); + } + + // After connecting to WiFi we need to get internet time from NTP server + time_t nowSec; + struct tm timeInfo; + // Fetch current time in epoch format and store it + display.getNTPEpoch(&nowSec); + gmtime_r(&nowSec, &timeInfo); + Serial.print(F("Current time: ")); + Serial.print(asctime(&timeInfo)); // Get the data from Google Calendar // Repeat attempts until data is fully downloaded @@ -456,6 +486,10 @@ void drawData() &entries[entriesNum].timeStamp); } ++entriesNum; + if(entriesNum == 128) + { + break; + } } // Sort entries by time diff --git a/examples/Inkplate4/Projects/Inkplate4_Google_Calendar/Network.cpp b/examples/Inkplate4/Projects/Inkplate4_Google_Calendar/Network.cpp index f219f983..1cc72f49 100644 --- a/examples/Inkplate4/Projects/Inkplate4_Google_Calendar/Network.cpp +++ b/examples/Inkplate4/Projects/Inkplate4_Google_Calendar/Network.cpp @@ -42,9 +42,6 @@ void Network::begin(char *ssid, char *pass) } } Serial.println(F(" connected")); - - // Find and print internet time, the timezone will be added later - setTime(); } // Gets time from ntp server @@ -142,29 +139,3 @@ bool Network::getData(char *calendarURL, char *data) return !f; } - -// Find internet time -void Network::setTime() -{ - // Used for setting correct time - configTime(0, 0, "pool.ntp.org", "time.nist.gov"); - - Serial.print(F("Waiting for NTP time sync: ")); - time_t nowSecs = time(nullptr); - while (nowSecs < 8 * 3600 * 2) - { - delay(500); - Serial.print(F(".")); - yield(); - nowSecs = time(nullptr); - } - Serial.println(); - - // Used to store time info - struct tm timeinfo; - gmtime_r(&nowSecs, &timeinfo); - - // Print the current time without adding a timezone - Serial.print(F("Current time: ")); - Serial.print(asctime(&timeinfo)); -} diff --git a/examples/Inkplate4/Projects/Inkplate4_Google_Calendar/Network.h b/examples/Inkplate4/Projects/Inkplate4_Google_Calendar/Network.h index 38df6957..1da10c0f 100644 --- a/examples/Inkplate4/Projects/Inkplate4_Google_Calendar/Network.h +++ b/examples/Inkplate4/Projects/Inkplate4_Google_Calendar/Network.h @@ -35,8 +35,6 @@ class Network bool getData(char *calendarURL, char *data); private: - // Functions called from within our class - void setTime(); }; #endif diff --git a/examples/Inkplate4/Projects/Inkplate4_Hourly_Weather_Station/Inkplate4_Hourly_Weather_Station.ino b/examples/Inkplate4/Projects/Inkplate4_Hourly_Weather_Station/Inkplate4_Hourly_Weather_Station.ino index 138d50c9..3c137c06 100644 --- a/examples/Inkplate4/Projects/Inkplate4_Hourly_Weather_Station/Inkplate4_Hourly_Weather_Station.ino +++ b/examples/Inkplate4/Projects/Inkplate4_Hourly_Weather_Station/Inkplate4_Hourly_Weather_Station.ino @@ -67,6 +67,7 @@ char apiKey[] = ""; // Delay between API calls, about 1000 per month, which is the free tier limit #define DELAY_MS 267800L +#define DELAY_WIFI_RETRY_SECONDS 10 // Inkplate object Inkplate display; @@ -133,8 +134,37 @@ void setup() Serial.begin(115200); display.begin(); - // Calling our begin from network.h file - network.begin(ssid, pass); + // Connect Inkplate to the WiFi network + // Try connecting to a WiFi network. + // Parameters are network SSID, password, timeout in seconds and whether to print to serial. + // If the Inkplate isn't able to connect to a network stop further code execution and print an error message. + if (!display.connectWiFi(ssid, pass, WIFI_TIMEOUT, true)) + { + //Can't connect to netowrk + // Clear display for the error message + display.clearDisplay(); + // Set the font size; + display.setTextSize(3); + // Set the cursor positions and print the text. + display.setCursor((display.width() / 2) - 200, display.height() / 2); + display.print(F("Unable to connect to ")); + display.println(F(ssid)); + display.setCursor((display.width() / 2) - 200, (display.height() / 2) + 30); + display.println(F("Please check SSID and PASS!")); + // Display the error message on the Inkplate and go to deep sleep + display.display(); + esp_sleep_enable_timer_wakeup(1000L * DELAY_WIFI_RETRY_SECONDS); + (void)esp_deep_sleep_start(); + } + + // After connecting to WiFi we need to get internet time from NTP server + time_t nowSec; + struct tm timeInfo; + // Fetch current time in epoch format and store it + display.getNTPEpoch(&nowSec); + gmtime_r(&nowSec, &timeInfo); + Serial.print(F("Current time: ")); + Serial.print(asctime(&timeInfo)); // Get all relevant data, see Network.cpp for info Serial.print("Fetching data"); diff --git a/examples/Inkplate4/Projects/Inkplate4_Hourly_Weather_Station/Network.cpp b/examples/Inkplate4/Projects/Inkplate4_Hourly_Weather_Station/Network.cpp index 7d7b41ae..5781e045 100644 --- a/examples/Inkplate4/Projects/Inkplate4_Hourly_Weather_Station/Network.cpp +++ b/examples/Inkplate4/Projects/Inkplate4_Hourly_Weather_Station/Network.cpp @@ -44,9 +44,6 @@ void Network::begin(char *ssid, char *pass) } Serial.println(F(" connected")); - // Find internet time - setTime(); - // Reduce power by making WiFi module sleep WiFi.setSleep(1); } @@ -208,31 +205,6 @@ bool Network::getData(char *lat, char *lon, char *apiKey, char *temp1, char *tem return !f; } -void Network::setTime() -{ - // Used for setting correct time - configTime(0, 0, "pool.ntp.org", "time.nist.gov"); - - Serial.print(F("Waiting for NTP time sync: ")); - time_t nowSecs = time(nullptr); - while (nowSecs < 8 * 3600 * 2) - { - // Print a dot every half a second while time is not set - delay(500); - Serial.print(F(".")); - yield(); - nowSecs = time(nullptr); - } - Serial.println(); - - // Used to store time info - struct tm timeinfo; - gmtime_r(&nowSecs, &timeinfo); - - Serial.print(F("Current time: ")); - Serial.print(asctime(&timeinfo)); -} - void Network::getHours(char *hour1, char *hour2, char *hour3, char *hour4, int timeZone) { // Format hours info diff --git a/examples/Inkplate4/Projects/Inkplate4_Hourly_Weather_Station/Network.h b/examples/Inkplate4/Projects/Inkplate4_Hourly_Weather_Station/Network.h index 2bb9d4a2..cc0bb98f 100644 --- a/examples/Inkplate4/Projects/Inkplate4_Hourly_Weather_Station/Network.h +++ b/examples/Inkplate4/Projects/Inkplate4_Hourly_Weather_Station/Network.h @@ -38,8 +38,6 @@ class Network void getHours(char *hour1, char *hour2, char *hour3, char *hour4, int timeZone); private: - // Functions called from within our class - void setTime(); // Used for storing retrieved data timestamp time_t dataEpoch = 0; diff --git a/examples/Inkplate4/Projects/Inkplate4_News_API/Inkplate4_News_API.ino b/examples/Inkplate4/Projects/Inkplate4_News_API/Inkplate4_News_API.ino index 963494ec..0a261cc2 100644 --- a/examples/Inkplate4/Projects/Inkplate4_News_API/Inkplate4_News_API.ino +++ b/examples/Inkplate4/Projects/Inkplate4_News_API/Inkplate4_News_API.ino @@ -36,7 +36,7 @@ char apiKey[] = ""; // You can obtain one here: https://newsapi.org/ // Here is set to 1 call per hour, but if you want to change it, have in mind that in the free plan there are only 100 // free API calls #define DELAY_MS 60 * 60 * 1000 - +#define DELAY_WIFI_RETRY_SECONDS 5 //------------------------------------- // Include Inkplate library to the sketch @@ -74,8 +74,28 @@ void setup() display.begin(); // Init Inkplate library (you should call this function ONLY ONCE) display.setTextWrap(false); - // Our begin function - network.begin(ssid, pass); + // Connect Inkplate to the WiFi network + // Try connecting to a WiFi network. + // Parameters are network SSID, password, timeout in seconds and whether to print to serial. + // If the Inkplate isn't able to connect to a network stop further code execution and print an error message. + if (!display.connectWiFi(ssid, pass, WIFI_TIMEOUT, true)) + { + //Can't connect to netowrk + // Clear display for the error message + display.clearDisplay(); + // Set the font size; + display.setTextSize(3); + // Set the cursor positions and print the text. + display.setCursor((display.width() / 2) - 200, display.height() / 2); + display.print(F("Unable to connect to ")); + display.println(F(ssid)); + display.setCursor((display.width() / 2) - 200, (display.height() / 2) + 30); + display.println(F("Please check SSID and PASS!")); + // Display the error message on the Inkplate and go to deep sleep + display.display(); + esp_sleep_enable_timer_wakeup(1000L * DELAY_WIFI_RETRY_SECONDS); + (void)esp_deep_sleep_start(); + } // Pointer to the struct that will hold all news data struct news *entities = NULL; diff --git a/examples/Inkplate4/Projects/Inkplate4_OpenWeather_Station/Inkplate4_OpenWeather_Station.ino b/examples/Inkplate4/Projects/Inkplate4_OpenWeather_Station/Inkplate4_OpenWeather_Station.ino index 438157e8..8fe99f60 100644 --- a/examples/Inkplate4/Projects/Inkplate4_OpenWeather_Station/Inkplate4_OpenWeather_Station.ino +++ b/examples/Inkplate4/Projects/Inkplate4_OpenWeather_Station/Inkplate4_OpenWeather_Station.ino @@ -43,6 +43,8 @@ // Change to your wifi ssid and password #define SSID "" #define PASS "" +// Variable to store WiFi connection timeout +int timeoutSeconds = 100; // Openweather API key /** @@ -65,6 +67,8 @@ bool metric = true; // <--- true is METRIC, false is IMPERIAL // Delay between API calls #define DELAY_MS 59000 +#define DELAY_WIFI_RETRY_SECONDS 5 +#define DELAY_API_RETRY_SECONDS 5 // --------------------------------------- @@ -113,36 +117,6 @@ char Output[200] = {0}; OpenWeatherOneCall OWOC; // Invoke OpenWeather Library time_t t = now(); -void connectWifi() -{ - if (WiFi.status() == WL_CONNECTED) - return; - - // Initiating wifi, like in BasicHttpClient example - WiFi.mode(WIFI_STA); - WiFi.begin(SSID, PASS); - - int cnt = 0; - Serial.print("Waiting for WiFi to connect "); - while ((WiFi.status() != WL_CONNECTED)) - { - Serial.print("."); - delay(1000); - ++cnt; - - if (cnt == 20) - { - Serial.println("Can't connect to WIFI, restarting"); - delay(100); - ESP.restart(); - } - } - Serial.print("\nConnected to "); - Serial.println(SSID); - Serial.print("IP address: "); - Serial.println(WiFi.localIP()); -} //======================== END WIFI CONNECT ======================= - void GetCurrentWeather() { //================================= @@ -204,7 +178,27 @@ void setup() // Init Inkplate library (you should call this function ONLY ONCE) display.begin(); - connectWifi(); + // Try connecting to a WiFi network. + // Parameters are network SSID, password, timeout in seconds and whether to print to serial. + // If the Inkplate isn't able to connect to a network stop further code execution and print an error message. + if (!display.connectWiFi(SSID, PASS, timeoutSeconds, true)) + { + //Can't connect to netowrk + // Clear display for the error message + display.clearDisplay(); + // Set the font size; + display.setTextSize(3); + // Set the cursor positions and print the text. + display.setCursor((display.width() / 2) - 200, display.height() / 2); + display.print(F("Unable to connect to ")); + display.println(F(SSID)); + display.setCursor((display.width() / 2) - 200, (display.height() / 2) + 30); + display.println(F("Please check SSID and PASS!")); + // Display the error message on the Inkplate and go to deep sleep + display.display(); + esp_sleep_enable_timer_wakeup(1000L * DELAY_WIFI_RETRY_SECONDS); + (void)esp_deep_sleep_start(); + } // Check if we have a valid API key: Serial.println("Checking if API key is valid..."); @@ -218,11 +212,10 @@ void setup() display.println("Can't get data from OpenWeatherMaps! Check your API key!"); display.println("Only older API keys for OneCall 2.5 work in free tier."); display.println("See the code comments the example for more info."); + // Display the error message on the Inkplate and go to deep sleep display.display(); - while(1) - { - delay(100); - } + esp_sleep_enable_timer_wakeup(1000L * DELAY_API_RETRY_SECONDS); + (void)esp_deep_sleep_start(); } Serial.println("API key is valid!"); @@ -590,4 +583,3 @@ bool checkIfAPIKeyIsValid(char *APIKEY) return !failed; } - diff --git a/examples/Inkplate4/Projects/Inkplate4_Quotables/Inkplate4_Quotables.ino b/examples/Inkplate4/Projects/Inkplate4_Quotables/Inkplate4_Quotables.ino index 0fcb2b7c..7f8f9e66 100644 --- a/examples/Inkplate4/Projects/Inkplate4_Quotables/Inkplate4_Quotables.ino +++ b/examples/Inkplate4/Projects/Inkplate4_Quotables/Inkplate4_Quotables.ino @@ -35,6 +35,7 @@ // Delay between API calls in seconds, 300 seconds is 5 minutes #define DELAY_S 300 +#define DELAY_WIFI_RETRY_SECONDS 5 // create object with all networking functions Network network; @@ -61,8 +62,27 @@ void setup() display.setTextWrap(true); // Set text wrapping to true display.setTextColor(INKPLATE4_BLACK, INKPLATE4_WHITE); - // Our begin function - network.begin(ssid, pass); + // Try connecting to a WiFi network. + // Parameters are network SSID, password, timeout in seconds and whether to print to serial. + // If the Inkplate isn't able to connect to a network stop further code execution and print an error message. + if (!display.connectWiFi(ssid, pass, WIFI_TIMEOUT, true)) + { + //Can't connect to netowrk + // Clear display for the error message + display.clearDisplay(); + // Set the font size; + display.setTextSize(3); + // Set the cursor positions and print the text. + display.setCursor((display.width() / 2) - 200, display.height() / 2); + display.print(F("Unable to connect to ")); + display.println(F(ssid)); + display.setCursor((display.width() / 2) - 200, (display.height() / 2) + 30); + display.println(F("Please check SSID and PASS!")); + // Display the error message on the Inkplate and go to deep sleep + display.display(); + esp_sleep_enable_timer_wakeup(1000L * DELAY_WIFI_RETRY_SECONDS); + (void)esp_deep_sleep_start(); + } // Try to get the new random quote from the Internet. while (!network.getData(quote, author, &len, &display)) diff --git a/examples/Inkplate4/Projects/Inkplate4_Youtube_Subscriber_Counter/Inkplate4_Youtube_Subscriber_Counter.ino b/examples/Inkplate4/Projects/Inkplate4_Youtube_Subscriber_Counter/Inkplate4_Youtube_Subscriber_Counter.ino index 8adeaa68..0b0ba5fc 100644 --- a/examples/Inkplate4/Projects/Inkplate4_Youtube_Subscriber_Counter/Inkplate4_Youtube_Subscriber_Counter.ino +++ b/examples/Inkplate4/Projects/Inkplate4_Youtube_Subscriber_Counter/Inkplate4_Youtube_Subscriber_Counter.ino @@ -41,6 +41,7 @@ // Delay between API calls in miliseconds (10 minutes) #define DELAY_MS 10 * 60 * 1000 +#define DELAY_WIFI_RETRY_SECONDS 5 // Create object with all networking functions Network network; @@ -123,8 +124,27 @@ void setup() delay(1000); } - // Our begin function - network.begin(ssid, pass); + // Try connecting to a WiFi network. + // Parameters are network SSID, password, timeout in seconds and whether to print to serial. + // If the Inkplate isn't able to connect to a network stop further code execution and print an error message. + if (!display.connectWiFi(ssid, pass, WIFI_TIMEOUT, true)) + { + //Can't connect to netowrk + // Clear display for the error message + display.clearDisplay(); + // Set the font size; + display.setTextSize(3); + // Set the cursor positions and print the text. + display.setCursor((display.width() / 2) - 200, display.height() / 2); + display.print(F("Unable to connect to ")); + display.println(F(ssid)); + display.setCursor((display.width() / 2) - 200, (display.height() / 2) + 30); + display.println(F("Please check SSID and PASS!")); + // Display the error message on the Inkplate and go to deep sleep + display.display(); + esp_sleep_enable_timer_wakeup(1000L * DELAY_WIFI_RETRY_SECONDS); + (void)esp_deep_sleep_start(); + } while (!network.getData(&channel, channel_id, api_key, &display)) { diff --git a/examples/Inkplate4TEMPERA/Projects/Inkplate4TEMPERA_Crypto_Currency_Checker/Inkplate4TEMPERA_Crypto_Currency_Checker.ino b/examples/Inkplate4TEMPERA/Projects/Inkplate4TEMPERA_Crypto_Currency_Checker/Inkplate4TEMPERA_Crypto_Currency_Checker.ino index 555bfe68..225e7f95 100644 --- a/examples/Inkplate4TEMPERA/Projects/Inkplate4TEMPERA_Crypto_Currency_Checker/Inkplate4TEMPERA_Crypto_Currency_Checker.ino +++ b/examples/Inkplate4TEMPERA/Projects/Inkplate4TEMPERA_Crypto_Currency_Checker/Inkplate4TEMPERA_Crypto_Currency_Checker.ino @@ -58,6 +58,8 @@ int timeZone = 2; char ssid[] = ""; char pass[] = ""; +#define DELAY_WIFI_RETRY_SECONDS 5 + // OPTIONAL: // change to a different currency char currency[32] = "bitcoin"; @@ -91,8 +93,37 @@ void setup() Serial.println("Touchscreen init failed!"); } - // Get the data from the API - network.begin(); + // Try connecting to a WiFi network. + // Parameters are network SSID, password, timeout in seconds and whether to print to serial. + // If the Inkplate isn't able to connect to a network stop further code execution and print an error message. + if (!display.connectWiFi(ssid, pass, WIFI_TIMEOUT, true)) + { + // Can't connect to netowrk + // Clear display for the error message + display.clearDisplay(); + // Set the font size; + display.setTextSize(3); + // Set the cursor positions and print the text. + display.setCursor((display.width() / 2) - 200, display.height() / 2); + display.print(F("Unable to connect to ")); + display.println(F(ssid)); + display.setCursor((display.width() / 2) - 200, (display.height() / 2) + 30); + display.println(F("Please check ssid and pass!")); + // Display the error message on the Inkplate and go to deep sleep + display.display(); + esp_sleep_enable_timer_wakeup(1000L * DELAY_WIFI_RETRY_SECONDS); + (void)esp_deep_sleep_start(); + } + + // After connecting to WiFi we need to get internet time from NTP server + time_t nowSec; + struct tm timeInfo; + // Fetch current time in epoch format and store it + display.getNTPEpoch(&nowSec); + gmtime_r(&nowSec, &timeInfo); + Serial.print(F("Current time: ")); + Serial.print(asctime(&timeInfo)); + getCoinPrices(); // Draw the coin price data which changes diff --git a/examples/Inkplate4TEMPERA/Projects/Inkplate4TEMPERA_Crypto_Currency_Checker/Network.cpp b/examples/Inkplate4TEMPERA/Projects/Inkplate4TEMPERA_Crypto_Currency_Checker/Network.cpp index 411d1064..77c44425 100644 --- a/examples/Inkplate4TEMPERA/Projects/Inkplate4TEMPERA_Crypto_Currency_Checker/Network.cpp +++ b/examples/Inkplate4TEMPERA/Projects/Inkplate4TEMPERA_Crypto_Currency_Checker/Network.cpp @@ -58,9 +58,6 @@ void Network::begin() } } Serial.println(F(" connected")); - - // Find internet time - setTime(); } // Gets time from ntp server @@ -185,29 +182,3 @@ bool Network::getData(double *data) return !f; } - -// Function for initial time setting ovet the ntp server -void Network::setTime() -{ - // Used for setting correct time - configTime(0, 0, "pool.ntp.org", "time.nist.gov"); - - Serial.print(F("Waiting for NTP time sync: ")); - time_t nowSecs = time(nullptr); - while (nowSecs < 8 * 3600 * 2) - { - delay(500); - Serial.print(F(".")); - yield(); - nowSecs = time(nullptr); - } - - Serial.println(); - - // Used to store time info - struct tm timeinfo; - gmtime_r(&nowSecs, &timeinfo); - - Serial.print(F("Current time: ")); - Serial.print(asctime(&timeinfo)); -} diff --git a/examples/Inkplate4TEMPERA/Projects/Inkplate4TEMPERA_Crypto_Currency_Checker/Network.h b/examples/Inkplate4TEMPERA/Projects/Inkplate4TEMPERA_Crypto_Currency_Checker/Network.h index 976c27ce..543adc82 100644 --- a/examples/Inkplate4TEMPERA/Projects/Inkplate4TEMPERA_Crypto_Currency_Checker/Network.h +++ b/examples/Inkplate4TEMPERA/Projects/Inkplate4TEMPERA_Crypto_Currency_Checker/Network.h @@ -41,8 +41,6 @@ class Network bool getData(double *data); private: - // Functions called from within our class - void setTime(); }; #endif diff --git a/examples/Inkplate4TEMPERA/Projects/Inkplate4TEMPERA_Crypto_Currency_Tracker/Inkplate4TEMPERA_Crypto_Currency_Tracker.ino b/examples/Inkplate4TEMPERA/Projects/Inkplate4TEMPERA_Crypto_Currency_Tracker/Inkplate4TEMPERA_Crypto_Currency_Tracker.ino index 483c7309..00de3c87 100644 --- a/examples/Inkplate4TEMPERA/Projects/Inkplate4TEMPERA_Crypto_Currency_Tracker/Inkplate4TEMPERA_Crypto_Currency_Tracker.ino +++ b/examples/Inkplate4TEMPERA/Projects/Inkplate4TEMPERA_Crypto_Currency_Tracker/Inkplate4TEMPERA_Crypto_Currency_Tracker.ino @@ -35,7 +35,7 @@ char pass[] = ""; // Delay between API calls in miliseconds #define DELAY_MS 3 * 60 * 1000 - +#define DELAY_WIFI_RETRY_SECONDS 5 // OPTIONAL: // change to a different currency char currency[] = "bitcoin"; @@ -116,7 +116,6 @@ textElement elements[] = { // Our functions declared below setup and loop void drawGraph(); void drawAll(); - void setup() { // Begin serial communication, sed for debugging @@ -133,8 +132,36 @@ void setup() display.setTextWrap(false); display.setTextColor(0, 7); - // Our begin function - network.begin(); + // Try connecting to a WiFi network. + // Parameters are network SSID, password, timeout in seconds and whether to print to serial. + // If the Inkplate isn't able to connect to a network stop further code execution and print an error message. + if (!display.connectWiFi(ssid, pass, WIFI_TIMEOUT, true)) + { + // Can't connect to netowrk + // Clear display for the error message + display.clearDisplay(); + // Set the font size; + display.setTextSize(3); + // Set the cursor positions and print the text. + display.setCursor((display.width() / 2) - 200, display.height() / 2); + display.print(F("Unable to connect to ")); + display.println(F(ssid)); + display.setCursor((display.width() / 2) - 200, (display.height() / 2) + 30); + display.println(F("Please check ssid and pass!")); + // Display the error message on the Inkplate and go to deep sleep + display.display(); + esp_sleep_enable_timer_wakeup(1000L * DELAY_WIFI_RETRY_SECONDS); + (void)esp_deep_sleep_start(); + } + + // After connecting to WiFi we need to get internet time from NTP server + time_t nowSec; + struct tm timeInfo; + // Fetch current time in epoch format and store it + display.getNTPEpoch(&nowSec); + gmtime_r(&nowSec, &timeInfo); + Serial.print(F("Current time: ")); + Serial.print(asctime(&timeInfo)); // Do a new network request Serial.print("Retrying retriving data "); diff --git a/examples/Inkplate4TEMPERA/Projects/Inkplate4TEMPERA_Crypto_Currency_Tracker/Network.cpp b/examples/Inkplate4TEMPERA/Projects/Inkplate4TEMPERA_Crypto_Currency_Tracker/Network.cpp index e409b929..b7eef617 100644 --- a/examples/Inkplate4TEMPERA/Projects/Inkplate4TEMPERA_Crypto_Currency_Tracker/Network.cpp +++ b/examples/Inkplate4TEMPERA/Projects/Inkplate4TEMPERA_Crypto_Currency_Tracker/Network.cpp @@ -194,28 +194,3 @@ bool Network::getData(double *data) return !f; } -// Function for initial time setting ovet the ntp server -void Network::setTime() -{ - // Used for setting correct time - configTime(0, 0, "pool.ntp.org", "time.nist.gov"); - - Serial.print(F("Waiting for NTP time sync: ")); - time_t nowSecs = time(nullptr); - while (nowSecs < 8 * 3600 * 2) - { - delay(500); - Serial.print(F(".")); - yield(); - nowSecs = time(nullptr); - } - - Serial.println(); - - // Used to store time info - struct tm timeinfo; - gmtime_r(&nowSecs, &timeinfo); - - Serial.print(F("Current time: ")); - Serial.print(asctime(&timeinfo)); -} diff --git a/examples/Inkplate4TEMPERA/Projects/Inkplate4TEMPERA_Crypto_Currency_Tracker/Network.h b/examples/Inkplate4TEMPERA/Projects/Inkplate4TEMPERA_Crypto_Currency_Tracker/Network.h index 976c27ce..543adc82 100644 --- a/examples/Inkplate4TEMPERA/Projects/Inkplate4TEMPERA_Crypto_Currency_Tracker/Network.h +++ b/examples/Inkplate4TEMPERA/Projects/Inkplate4TEMPERA_Crypto_Currency_Tracker/Network.h @@ -41,8 +41,6 @@ class Network bool getData(double *data); private: - // Functions called from within our class - void setTime(); }; #endif diff --git a/examples/Inkplate4TEMPERA/Projects/Inkplate4TEMPERA_Daily_Weather_Station/Inkplate4TEMPERA_Daily_Weather_Station.ino b/examples/Inkplate4TEMPERA/Projects/Inkplate4TEMPERA_Daily_Weather_Station/Inkplate4TEMPERA_Daily_Weather_Station.ino index feb7a500..63eb9aeb 100644 --- a/examples/Inkplate4TEMPERA/Projects/Inkplate4TEMPERA_Daily_Weather_Station/Inkplate4TEMPERA_Daily_Weather_Station.ino +++ b/examples/Inkplate4TEMPERA/Projects/Inkplate4TEMPERA_Daily_Weather_Station/Inkplate4TEMPERA_Daily_Weather_Station.ino @@ -63,6 +63,7 @@ char apiKey[] = ""; // Delay between API calls, about 1000 per month, which is the free tier limit #define DELAY_MS 267800L +#define DELAY_WIFI_RETRY_SECONDS 5 // Inkplate object Inkplate display(INKPLATE_1BIT); @@ -127,8 +128,36 @@ void setup() Serial.begin(115200); display.begin(); - // Calling our begin from network.h file - network.begin(ssid, pass); + // Try connecting to a WiFi network. + // Parameters are network SSID, password, timeout in seconds and whether to print to serial. + // If the Inkplate isn't able to connect to a network stop further code execution and print an error message. + if (!display.connectWiFi(ssid, pass, WIFI_TIMEOUT, true)) + { + //Can't connect to netowrk + // Clear display for the error message + display.clearDisplay(); + // Set the font size; + display.setTextSize(3); + // Set the cursor positions and print the text. + display.setCursor((display.width() / 2) - 200, display.height() / 2); + display.print(F("Unable to connect to ")); + display.println(F(ssid)); + display.setCursor((display.width() / 2) - 200, (display.height() / 2) + 30); + display.println(F("Please check ssid and pass!")); + // Display the error message on the Inkplate and go to deep sleep + display.display(); + esp_sleep_enable_timer_wakeup(100L * DELAY_WIFI_RETRY_SECONDS); + (void)esp_deep_sleep_start(); + } + + // After connecting to WiFi we need to get internet time from NTP server + time_t nowSec; + struct tm timeInfo; + // Fetch current time in epoch format and store it + display.getNTPEpoch(&nowSec); + gmtime_r(&nowSec, &timeInfo); + Serial.print(F("Current time: ")); + Serial.print(asctime(&timeInfo)); // Get all relevant data, see Network.cpp for info if (refreshes % fullRefresh == 0) diff --git a/examples/Inkplate4TEMPERA/Projects/Inkplate4TEMPERA_Daily_Weather_Station/Network.cpp b/examples/Inkplate4TEMPERA/Projects/Inkplate4TEMPERA_Daily_Weather_Station/Network.cpp index cf8759db..e7aea3d9 100644 --- a/examples/Inkplate4TEMPERA/Projects/Inkplate4TEMPERA_Daily_Weather_Station/Network.cpp +++ b/examples/Inkplate4TEMPERA/Projects/Inkplate4TEMPERA_Daily_Weather_Station/Network.cpp @@ -43,9 +43,6 @@ void Network::begin(char *ssid, char *pass) } } Serial.println(F(" connected")); - - // Find internet time - setTime(); } // Gets time from ntp server @@ -227,32 +224,6 @@ bool Network::getData(char *lat, char *lon, char *apiKey, char *city, char *temp return 1; } -void Network::setTime() -{ - // Used for setting correct time - configTime(0, 0, "pool.ntp.org", "time.nist.gov"); - - Serial.print(F("Waiting for NTP time sync: ")); - time_t nowSecs = time(nullptr); - while (nowSecs < 8 * 3600 * 2) - { - // Print a dot every half a second while time is not set - delay(500); - Serial.print(F(".")); - yield(); - nowSecs = time(nullptr); - } - - Serial.println(); - - // Used to store time info - struct tm timeinfo; - gmtime_r(&nowSecs, &timeinfo); - - Serial.print(F("Current time: ")); - Serial.print(asctime(&timeinfo)); -} - void Network::getDays(char *day, char *day1, char *day2, char *day3) { // Seconds since 1.1.1970. diff --git a/examples/Inkplate4TEMPERA/Projects/Inkplate4TEMPERA_Daily_Weather_Station/Network.h b/examples/Inkplate4TEMPERA/Projects/Inkplate4TEMPERA_Daily_Weather_Station/Network.h index df05ca18..e9c17ae6 100644 --- a/examples/Inkplate4TEMPERA/Projects/Inkplate4TEMPERA_Daily_Weather_Station/Network.h +++ b/examples/Inkplate4TEMPERA/Projects/Inkplate4TEMPERA_Daily_Weather_Station/Network.h @@ -49,8 +49,6 @@ class Network int location = -1; private: - // Functions called from within our class - void setTime(); int timeZone = 0; }; diff --git a/examples/Inkplate4TEMPERA/Projects/Inkplate4TEMPERA_Google_Calendar/Inkplate4TEMPERA_Google_Calendar.ino b/examples/Inkplate4TEMPERA/Projects/Inkplate4TEMPERA_Google_Calendar/Inkplate4TEMPERA_Google_Calendar.ino index 1b3b71b4..7c2fcf71 100644 --- a/examples/Inkplate4TEMPERA/Projects/Inkplate4TEMPERA_Google_Calendar/Inkplate4TEMPERA_Google_Calendar.ino +++ b/examples/Inkplate4TEMPERA/Projects/Inkplate4TEMPERA_Google_Calendar/Inkplate4TEMPERA_Google_Calendar.ino @@ -55,7 +55,7 @@ int timeZone = 2; // Delay between API calls #define DELAY_MS 4 * 60000 - +#define DELAY_WIFI_RETRY_SECONDS 5 // Variables to keep count of when to get new data, and when to just update time RTC_DATA_ATTR unsigned int refreshes = 0; const int refreshesToGet = 10; @@ -92,6 +92,7 @@ void getToFrom(char *dst, char *from, char *to, int *day, int *timeStamp); bool drawEvent(entry *event, int day, int beginY, int maxHeigth, int *heigthNeeded); int cmp(const void *a, const void *b); void drawData(); +void setTime(); void setup() { @@ -105,7 +106,30 @@ void setup() display.setTextColor(0, 7); delay(5000); - network.begin(ssid, pass); + // Connect Inkplate to the WiFi network + // Try connecting to a WiFi network. + // Parameters are network SSID, password, timeout in seconds and whether to print to serial. + // If the Inkplate isn't able to connect to a network stop further code execution and print an error message. + if (!display.connectWiFi(ssid, pass, WIFI_TIMEOUT, true)) + { + //Can't connect to netowrk + // Clear display for the error message + display.clearDisplay(); + // Set the font size; + display.setTextSize(3); + // Set the cursor positions and print the text. + display.setCursor((display.width() / 2) - 200, display.height() / 2); + display.print(F("Unable to connect to ")); + display.println(F(ssid)); + display.setCursor((display.width() / 2) - 200, (display.height() / 2) + 30); + display.println(F("Please check SSID and PASS!")); + // Display the error message on the Inkplate and go to deep sleep + display.display(); + esp_sleep_enable_timer_wakeup(1000L * DELAY_WIFI_RETRY_SECONDS); + (void)esp_deep_sleep_start(); + } + + setTime(); // Keep trying to get data if it fails the first time Serial.println("Getting data... "); @@ -139,6 +163,27 @@ void loop() // Never here } +// Function for getting time from NTP server +void setTime() +{ + // Structure used to hold time information + struct tm timeInfo; + // Fetch current time from an NTP server and store it + time_t nowSec; + // Fetch current time in epoch format and store it + display.getNTPEpoch(&nowSec); + // This loop ensures that the NTP time fetched is valid and beyond a certain threshold + while(nowSec < 8 * 3600 * 2) + { + delay(500); + yield(); + display.getNTPEpoch(&nowSec); + } + gmtime_r(&nowSec, &timeInfo); + Serial.print(F("Current time: ")); + Serial.print(asctime(&timeInfo)); +} + // Function for drawing calendar info void drawInfo() { @@ -455,6 +500,10 @@ void drawData() &entries[entriesNum].timeStamp); } ++entriesNum; + if(entriesNum == 128) + { + break; + } } // Sort entries by time diff --git a/examples/Inkplate4TEMPERA/Projects/Inkplate4TEMPERA_Google_Calendar/Network.cpp b/examples/Inkplate4TEMPERA/Projects/Inkplate4TEMPERA_Google_Calendar/Network.cpp index f219f983..6dcf423c 100644 --- a/examples/Inkplate4TEMPERA/Projects/Inkplate4TEMPERA_Google_Calendar/Network.cpp +++ b/examples/Inkplate4TEMPERA/Projects/Inkplate4TEMPERA_Google_Calendar/Network.cpp @@ -43,8 +43,6 @@ void Network::begin(char *ssid, char *pass) } Serial.println(F(" connected")); - // Find and print internet time, the timezone will be added later - setTime(); } // Gets time from ntp server @@ -142,29 +140,3 @@ bool Network::getData(char *calendarURL, char *data) return !f; } - -// Find internet time -void Network::setTime() -{ - // Used for setting correct time - configTime(0, 0, "pool.ntp.org", "time.nist.gov"); - - Serial.print(F("Waiting for NTP time sync: ")); - time_t nowSecs = time(nullptr); - while (nowSecs < 8 * 3600 * 2) - { - delay(500); - Serial.print(F(".")); - yield(); - nowSecs = time(nullptr); - } - Serial.println(); - - // Used to store time info - struct tm timeinfo; - gmtime_r(&nowSecs, &timeinfo); - - // Print the current time without adding a timezone - Serial.print(F("Current time: ")); - Serial.print(asctime(&timeinfo)); -} diff --git a/examples/Inkplate4TEMPERA/Projects/Inkplate4TEMPERA_Google_Calendar/Network.h b/examples/Inkplate4TEMPERA/Projects/Inkplate4TEMPERA_Google_Calendar/Network.h index 38df6957..1da10c0f 100644 --- a/examples/Inkplate4TEMPERA/Projects/Inkplate4TEMPERA_Google_Calendar/Network.h +++ b/examples/Inkplate4TEMPERA/Projects/Inkplate4TEMPERA_Google_Calendar/Network.h @@ -35,8 +35,6 @@ class Network bool getData(char *calendarURL, char *data); private: - // Functions called from within our class - void setTime(); }; #endif diff --git a/examples/Inkplate4TEMPERA/Projects/Inkplate4TEMPERA_Hourly_Weather_Station/Inkplate4TEMPERA_Hourly_Weather_Station.ino b/examples/Inkplate4TEMPERA/Projects/Inkplate4TEMPERA_Hourly_Weather_Station/Inkplate4TEMPERA_Hourly_Weather_Station.ino index 3c157ded..39974670 100644 --- a/examples/Inkplate4TEMPERA/Projects/Inkplate4TEMPERA_Hourly_Weather_Station/Inkplate4TEMPERA_Hourly_Weather_Station.ino +++ b/examples/Inkplate4TEMPERA/Projects/Inkplate4TEMPERA_Hourly_Weather_Station/Inkplate4TEMPERA_Hourly_Weather_Station.ino @@ -67,7 +67,7 @@ char apiKey[] = ""; // Delay between API calls, about 1000 per month, which is the free tier limit #define DELAY_MS 267800L - +#define DELAY_WIFI_RETRY_SECONDS 5 // Inkplate object Inkplate display(INKPLATE_1BIT); @@ -133,8 +133,37 @@ void setup() Serial.begin(115200); display.begin(); - // Calling our begin from network.h file - network.begin(ssid, pass); + // Connect Inkplate to the WiFi network + // Try connecting to a WiFi network. + // Parameters are network SSID, password, timeout in seconds and whether to print to serial. + // If the Inkplate isn't able to connect to a network stop further code execution and print an error message. + if (!display.connectWiFi(ssid, pass, WIFI_TIMEOUT, true)) + { + //Can't connect to netowrk + // Clear display for the error message + display.clearDisplay(); + // Set the font size; + display.setTextSize(3); + // Set the cursor positions and print the text. + display.setCursor((display.width() / 2) - 200, display.height() / 2); + display.print(F("Unable to connect to ")); + display.println(F(ssid)); + display.setCursor((display.width() / 2) - 200, (display.height() / 2) + 30); + display.println(F("Please check SSID and PASS!")); + // Display the error message on the Inkplate and go to deep sleep + display.display(); + esp_sleep_enable_timer_wakeup(1000L * DELAY_WIFI_RETRY_SECONDS); + (void)esp_deep_sleep_start(); + } + + // After connecting to WiFi we need to get internet time from NTP server + time_t nowSec; + struct tm timeInfo; + // Fetch current time in epoch format and store it + display.getNTPEpoch(&nowSec); + gmtime_r(&nowSec, &timeInfo); + Serial.print(F("Current time: ")); + Serial.print(asctime(&timeInfo)); // Get all relevant data, see Network.cpp for info Serial.print("Fetching data"); diff --git a/examples/Inkplate4TEMPERA/Projects/Inkplate4TEMPERA_Hourly_Weather_Station/Network.cpp b/examples/Inkplate4TEMPERA/Projects/Inkplate4TEMPERA_Hourly_Weather_Station/Network.cpp index 7d7b41ae..5781e045 100644 --- a/examples/Inkplate4TEMPERA/Projects/Inkplate4TEMPERA_Hourly_Weather_Station/Network.cpp +++ b/examples/Inkplate4TEMPERA/Projects/Inkplate4TEMPERA_Hourly_Weather_Station/Network.cpp @@ -44,9 +44,6 @@ void Network::begin(char *ssid, char *pass) } Serial.println(F(" connected")); - // Find internet time - setTime(); - // Reduce power by making WiFi module sleep WiFi.setSleep(1); } @@ -208,31 +205,6 @@ bool Network::getData(char *lat, char *lon, char *apiKey, char *temp1, char *tem return !f; } -void Network::setTime() -{ - // Used for setting correct time - configTime(0, 0, "pool.ntp.org", "time.nist.gov"); - - Serial.print(F("Waiting for NTP time sync: ")); - time_t nowSecs = time(nullptr); - while (nowSecs < 8 * 3600 * 2) - { - // Print a dot every half a second while time is not set - delay(500); - Serial.print(F(".")); - yield(); - nowSecs = time(nullptr); - } - Serial.println(); - - // Used to store time info - struct tm timeinfo; - gmtime_r(&nowSecs, &timeinfo); - - Serial.print(F("Current time: ")); - Serial.print(asctime(&timeinfo)); -} - void Network::getHours(char *hour1, char *hour2, char *hour3, char *hour4, int timeZone) { // Format hours info diff --git a/examples/Inkplate4TEMPERA/Projects/Inkplate4TEMPERA_Hourly_Weather_Station/Network.h b/examples/Inkplate4TEMPERA/Projects/Inkplate4TEMPERA_Hourly_Weather_Station/Network.h index 2bb9d4a2..cc0bb98f 100644 --- a/examples/Inkplate4TEMPERA/Projects/Inkplate4TEMPERA_Hourly_Weather_Station/Network.h +++ b/examples/Inkplate4TEMPERA/Projects/Inkplate4TEMPERA_Hourly_Weather_Station/Network.h @@ -38,8 +38,6 @@ class Network void getHours(char *hour1, char *hour2, char *hour3, char *hour4, int timeZone); private: - // Functions called from within our class - void setTime(); // Used for storing retrieved data timestamp time_t dataEpoch = 0; diff --git a/examples/Inkplate4TEMPERA/Projects/Inkplate4TEMPERA_News_API/Inkplate4TEMPERA_News_API.ino b/examples/Inkplate4TEMPERA/Projects/Inkplate4TEMPERA_News_API/Inkplate4TEMPERA_News_API.ino index 81374363..0a993aef 100644 --- a/examples/Inkplate4TEMPERA/Projects/Inkplate4TEMPERA_News_API/Inkplate4TEMPERA_News_API.ino +++ b/examples/Inkplate4TEMPERA/Projects/Inkplate4TEMPERA_News_API/Inkplate4TEMPERA_News_API.ino @@ -53,13 +53,15 @@ Inkplate display(INKPLATE_1BIT); // Delay between API calls in miliseconds (first 60 represents minutes so you can change to your need) #define DELAY_MS (uint32_t)60 * 60 * 1000 - +#define DELAY_WIFI_RETRY_SECONDS 5 // Variable for counting partial refreshes RTC_DATA_ATTR unsigned refreshes = 0; // Constant to determine when to full update const int fullRefresh = 20; +void setTime(); + void setup() { // Begin serial communitcation, sed for debugging @@ -68,8 +70,29 @@ void setup() display.begin(); display.setTextWrap(false); - // Our begin function - network.begin(); + // Connect Inkplate to the WiFi network + // Try connecting to a WiFi network. + // Parameters are network SSID, password, timeout in seconds and whether to print to serial. + // If the Inkplate isn't able to connect to a network stop further code execution and print an error message. + if (!display.connectWiFi(ssid, pass, WIFI_TIMEOUT, true)) + { + //Can't connect to netowrk + // Clear display for the error message + display.clearDisplay(); + // Set the font size; + display.setTextSize(3); + // Set the cursor positions and print the text. + display.setCursor((display.width() / 2) - 200, display.height() / 2); + display.print(F("Unable to connect to ")); + display.println(F(ssid)); + display.setCursor((display.width() / 2) - 200, (display.height() / 2) + 30); + display.println(F("Please check SSID and PASS!")); + // Display the error message on the Inkplate and go to deep sleep + display.display(); + esp_sleep_enable_timer_wakeup(1000L * DELAY_WIFI_RETRY_SECONDS); + (void)esp_deep_sleep_start(); + } + struct news *entities; @@ -153,6 +176,26 @@ void drawNews(struct news *entities) } +// Function for getting time from NTP server +void setTime() +{ + // Structure used to hold time information + struct tm timeInfo; + time_t nowSec; + // Fetch current time in epoch format and store it + display.getNTPEpoch(&nowSec); + // This loop ensures that the NTP time fetched is valid and beyond a certain threshold + while (nowSec < 8 * 3600 * 2) + { + delay(500); + yield(); + nowSec = time(nullptr); + } + gmtime_r(&nowSec, &timeInfo); + Serial.print(F("Current time: ")); + Serial.print(asctime(&timeInfo)); +} + void loop() { // Never here diff --git a/examples/Inkplate4TEMPERA/Projects/Inkplate4TEMPERA_News_API/Network.cpp b/examples/Inkplate4TEMPERA/Projects/Inkplate4TEMPERA_News_API/Network.cpp index c92997d4..f7d304ba 100644 --- a/examples/Inkplate4TEMPERA/Projects/Inkplate4TEMPERA_News_API/Network.cpp +++ b/examples/Inkplate4TEMPERA/Projects/Inkplate4TEMPERA_News_API/Network.cpp @@ -60,9 +60,6 @@ void Network::begin() } } Serial.println(F(" connected")); - - // Find internet time - setTime(); } // Gets time from ntp server @@ -210,32 +207,4 @@ struct news* Network::getData() WiFi.setSleep(sleep); return ent; -} - -// Function for initial time setting ovet the ntp server -void Network::setTime() -{ - // Used for setting correct time - configTime(0, 0, "pool.ntp.org", "time.nist.gov"); - - Serial.print(F("Waiting for NTP time sync: ")); - time_t nowSecs = time(nullptr); - delay(3000); - while (nowSecs < 8 * 3600 * 2) - { - delay(500); - Serial.print(F(".")); - yield(); - nowSecs = time(nullptr); - break; - } - - Serial.println(); - - // Used to store time info - struct tm timeinfo; - gmtime_r(&nowSecs, &timeinfo); - - Serial.print(F("Current time: ")); - Serial.print(asctime(&timeinfo)); -} +} \ No newline at end of file diff --git a/examples/Inkplate4TEMPERA/Projects/Inkplate4TEMPERA_News_API/Network.h b/examples/Inkplate4TEMPERA/Projects/Inkplate4TEMPERA_News_API/Network.h index c6fcba3b..990c855b 100644 --- a/examples/Inkplate4TEMPERA/Projects/Inkplate4TEMPERA_News_API/Network.h +++ b/examples/Inkplate4TEMPERA/Projects/Inkplate4TEMPERA_News_API/Network.h @@ -52,8 +52,6 @@ class Network struct news* getData(); private: - // Functions called from within our class - void setTime(); }; #endif diff --git a/examples/Inkplate4TEMPERA/Projects/Inkplate4TEMPERA_OpenWeather_Station/Inkplate4TEMPERA_OpenWeather_Station.ino b/examples/Inkplate4TEMPERA/Projects/Inkplate4TEMPERA_OpenWeather_Station/Inkplate4TEMPERA_OpenWeather_Station.ino index b0fe026f..cc396079 100644 --- a/examples/Inkplate4TEMPERA/Projects/Inkplate4TEMPERA_OpenWeather_Station/Inkplate4TEMPERA_OpenWeather_Station.ino +++ b/examples/Inkplate4TEMPERA/Projects/Inkplate4TEMPERA_OpenWeather_Station/Inkplate4TEMPERA_OpenWeather_Station.ino @@ -43,6 +43,8 @@ // Change to your wifi ssid and password #define SSID "" #define PASS "" +// Variable to store WiFi connection timeout +int timeoutSeconds = 100; // Openweather API key /** @@ -65,7 +67,8 @@ bool metric = true; // <--- true is METRIC, false is IMPERIAL // Delay between API calls #define DELAY_MS 59000 - +#define DELAY_WIFI_RETRY_SECONDS 5 +#define DELAY_API_RETRY_SECONDS 5 // --------------------------------------- // Including fonts used @@ -113,36 +116,6 @@ char Output[200] = {0}; OpenWeatherOneCall OWOC; // Invoke OpenWeather Library time_t t = now(); -void connectWifi() -{ - if (WiFi.status() == WL_CONNECTED) - return; - - // Initiating wifi, like in BasicHttpClient example - WiFi.mode(WIFI_STA); - WiFi.begin(SSID, PASS); - - int cnt = 0; - Serial.print("Waiting for WiFi to connect "); - while ((WiFi.status() != WL_CONNECTED)) - { - Serial.print("."); - delay(1000); - ++cnt; - - if (cnt == 20) - { - Serial.println("Can't connect to WIFI, restarting"); - delay(100); - ESP.restart(); - } - } - Serial.print("\nConnected to "); - Serial.println(SSID); - Serial.print("IP address: "); - Serial.println(WiFi.localIP()); -} //======================== END WIFI CONNECT ======================= - void GetCurrentWeather() { //================================= @@ -204,7 +177,27 @@ void setup() // Init Inkplate library (you should call this function ONLY ONCE) display.begin(); - connectWifi(); + // Try connecting to a WiFi network. + // Parameters are network SSID, password, timeout in seconds and whether to print to serial. + // If the Inkplate isn't able to connect to a network stop further code execution and print an error message. + if (!display.connectWiFi(SSID, PASS, timeoutSeconds, true)) + { + //Can't connect to netowrk + // Clear display for the error message + display.clearDisplay(); + // Set the font size; + display.setTextSize(3); + // Set the cursor positions and print the text. + display.setCursor((display.width() / 2) - 200, display.height() / 2); + display.print(F("Unable to connect to ")); + display.println(F(SSID)); + display.setCursor((display.width() / 2) - 200, (display.height() / 2) + 30); + display.println(F("Please check SSID and PASS!")); + // Display the error message on the Inkplate and go to deep sleep + display.display(); + esp_sleep_enable_timer_wakeup(1000L * DELAY_WIFI_RETRY_SECONDS); + (void)esp_deep_sleep_start(); + } // Check if we have a valid API key: Serial.println("Checking if API key is valid..."); @@ -218,11 +211,10 @@ void setup() display.println("Can't get data from OpenWeatherMaps! Check your API key!"); display.println("Only older API keys for OneCall 2.5 work in free tier."); display.println("See the code comments the example for more info."); + // Display the error message on the Inkplate and go to deep sleep display.display(); - while(1) - { - delay(100); - } + esp_sleep_enable_timer_wakeup(1000L * DELAY_API_RETRY_SECONDS); + (void)esp_deep_sleep_start(); } Serial.println("API key is valid!"); @@ -590,4 +582,3 @@ bool checkIfAPIKeyIsValid(char *APIKEY) return !failed; } - diff --git a/examples/Inkplate4TEMPERA/Projects/Inkplate4TEMPERA_Quotables/Inkplate4TEMPERA_Quotables.ino b/examples/Inkplate4TEMPERA/Projects/Inkplate4TEMPERA_Quotables/Inkplate4TEMPERA_Quotables.ino index 0b574ebe..0cb96073 100644 --- a/examples/Inkplate4TEMPERA/Projects/Inkplate4TEMPERA_Quotables/Inkplate4TEMPERA_Quotables.ino +++ b/examples/Inkplate4TEMPERA/Projects/Inkplate4TEMPERA_Quotables/Inkplate4TEMPERA_Quotables.ino @@ -35,6 +35,7 @@ // Delay between API calls in seconds, 300 seconds is 5 minutes #define DELAY_S 300 +#define DELAY_WIFI_RETRY_SECONDS 5 // create object with all networking functions Network network; @@ -43,8 +44,8 @@ Network network; Inkplate display(INKPLATE_1BIT); // Put in your ssid and password -char ssid[] = "Soldered"; -char pass[] = "dasduino"; +char ssid[] = ""; +char pass[] = ""; // Buffers to store quote, author name and quote length char quote[256]; @@ -60,8 +61,27 @@ void setup() display.begin(); display.setTextWrap(true); // Set text wrapping to true - // Our begin function - network.begin(ssid, pass); + // Try connecting to a WiFi network. + // Parameters are network SSID, password, timeout in seconds and whether to print to serial. + // If the Inkplate isn't able to connect to a network stop further code execution and print an error message. + if (!display.connectWiFi(ssid, pass, WIFI_TIMEOUT, true)) + { + //Can't connect to netowrk + // Clear display for the error message + display.clearDisplay(); + // Set the font size; + display.setTextSize(3); + // Set the cursor positions and print the text. + display.setCursor((display.width() / 2) - 200, display.height() / 2); + display.print(F("Unable to connect to ")); + display.println(F(ssid)); + display.setCursor((display.width() / 2) - 200, (display.height() / 2) + 30); + display.println(F("Please check SSID and PASS!")); + // Display the error message on the Inkplate and go to deep sleep + display.display(); + esp_sleep_enable_timer_wakeup(1000L * DELAY_WIFI_RETRY_SECONDS); + (void)esp_deep_sleep_start(); + } // Try to get the new random quote from the Internet. while (!network.getData(quote, author, &len, &display)) diff --git a/examples/Inkplate4TEMPERA/Projects/Inkplate4TEMPERA_Youtube_Subscriber_Counter/Inkplate4TEMPERA_Youtube_Subscriber_Counter.ino b/examples/Inkplate4TEMPERA/Projects/Inkplate4TEMPERA_Youtube_Subscriber_Counter/Inkplate4TEMPERA_Youtube_Subscriber_Counter.ino index 7488927a..46ad57d1 100644 --- a/examples/Inkplate4TEMPERA/Projects/Inkplate4TEMPERA_Youtube_Subscriber_Counter/Inkplate4TEMPERA_Youtube_Subscriber_Counter.ino +++ b/examples/Inkplate4TEMPERA/Projects/Inkplate4TEMPERA_Youtube_Subscriber_Counter/Inkplate4TEMPERA_Youtube_Subscriber_Counter.ino @@ -42,7 +42,7 @@ // Delay between API calls in miliseconds (10 minutes) #define DELAY_MS 10 * 60 * 1000 - +#define DELAY_WIFI_RETRY_SECONDS 5 // Create object with all networking functions Network network; @@ -124,8 +124,27 @@ void setup() delay(1000); } - // Our begin function - network.begin(ssid, pass); + // Try connecting to a WiFi network. + // Parameters are network SSID, password, timeout in seconds and whether to print to serial. + // If the Inkplate isn't able to connect to a network stop further code execution and print an error message. + if (!display.connectWiFi(ssid, pass, WIFI_TIMEOUT, true)) + { + //Can't connect to netowrk + // Clear display for the error message + display.clearDisplay(); + // Set the font size; + display.setTextSize(3); + // Set the cursor positions and print the text. + display.setCursor((display.width() / 2) - 200, display.height() / 2); + display.print(F("Unable to connect to ")); + display.println(F(ssid)); + display.setCursor((display.width() / 2) - 200, (display.height() / 2) + 30); + display.println(F("Please check SSID and PASS!")); + // Display the error message on the Inkplate and go to deep sleep + display.display(); + esp_sleep_enable_timer_wakeup(1000L * DELAY_WIFI_RETRY_SECONDS); + (void)esp_deep_sleep_start(); + } while (!network.getData(&channel, channel_id, api_key, &display)) { diff --git a/examples/Inkplate5/Projects/Inkplate5_Crypto_Currency_Tracker/Inkplate5_Crypto_Currency_Tracker.ino b/examples/Inkplate5/Projects/Inkplate5_Crypto_Currency_Tracker/Inkplate5_Crypto_Currency_Tracker.ino index 5c632ee7..9789b0de 100644 --- a/examples/Inkplate5/Projects/Inkplate5_Crypto_Currency_Tracker/Inkplate5_Crypto_Currency_Tracker.ino +++ b/examples/Inkplate5/Projects/Inkplate5_Crypto_Currency_Tracker/Inkplate5_Crypto_Currency_Tracker.ino @@ -37,6 +37,7 @@ char pass[] = ""; // Delay between API calls in miliseconds (first 60 represents minutes so you can change to your need) #define DELAY_MS 3 * 60 * 1000 +#define DELAY_WIFI_RETRY_SECONDS 5 // OPTIONAL: // Change to a different currency @@ -128,8 +129,36 @@ void setup() display.setTextWrap(false); display.setTextColor(0, 7); - // Our begin function - network.begin(ssid, pass); + // Try connecting to a WiFi network. + // Parameters are network SSID, password, timeout in seconds and whether to print to serial. + // If the Inkplate isn't able to connect to a network stop further code execution and print an error message. + if (!display.connectWiFi(ssid, pass, WIFI_TIMEOUT, true)) + { + // Can't connect to netowrk + // Clear display for the error message + display.clearDisplay(); + // Set the font size; + display.setTextSize(3); + // Set the cursor positions and print the text. + display.setCursor((display.width() / 2) - 200, display.height() / 2); + display.print(F("Unable to connect to ")); + display.println(F(ssid)); + display.setCursor((display.width() / 2) - 200, (display.height() / 2) + 30); + display.println(F("Please check ssid and pass!")); + // Display the error message on the Inkplate and go to deep sleep + display.display(); + esp_sleep_enable_timer_wakeup(1000L * DELAY_WIFI_RETRY_SECONDS); + (void)esp_deep_sleep_start(); + } + + // After connecting to WiFi we need to get internet time from NTP server + time_t nowSec; + struct tm timeInfo; + // Fetch current time in epoch format and store it + display.getNTPEpoch(&nowSec); + gmtime_r(&nowSec, &timeInfo); + Serial.print(F("Current time: ")); + Serial.print(asctime(&timeInfo)); Serial.println("Retrying retriving data"); while (!network.getData(data, currency)) diff --git a/examples/Inkplate5/Projects/Inkplate5_Crypto_Currency_Tracker/Network.cpp b/examples/Inkplate5/Projects/Inkplate5_Crypto_Currency_Tracker/Network.cpp index 546f287c..f90e8173 100644 --- a/examples/Inkplate5/Projects/Inkplate5_Crypto_Currency_Tracker/Network.cpp +++ b/examples/Inkplate5/Projects/Inkplate5_Crypto_Currency_Tracker/Network.cpp @@ -60,9 +60,6 @@ void Network::begin(char * ssid, char * pass) } } Serial.println(F(" connected")); - - // Find internet time - setTime(); } /** @@ -182,33 +179,3 @@ bool Network::getData(double *data, char * currency) return !f; } - -/** - * @brief Set the internal time with data from NTP server - * - * @returns None -*/ -void Network::setTime() -{ - // Used for setting correct time - configTime(0, 0, "pool.ntp.org", "time.nist.gov"); - - Serial.print(F("Waiting for NTP time sync: ")); - time_t nowSecs = time(nullptr); - while (nowSecs < 8 * 3600 * 2) - { - delay(500); - Serial.print(F(".")); - yield(); - nowSecs = time(nullptr); - } - - Serial.println(); - - // Used to store time info - struct tm timeinfo; - gmtime_r(&nowSecs, &timeinfo); - - Serial.print(F("Current time: ")); - Serial.print(asctime(&timeinfo)); -} diff --git a/examples/Inkplate5/Projects/Inkplate5_Crypto_Currency_Tracker/Network.h b/examples/Inkplate5/Projects/Inkplate5_Crypto_Currency_Tracker/Network.h index 87aa49b2..0746b716 100644 --- a/examples/Inkplate5/Projects/Inkplate5_Crypto_Currency_Tracker/Network.h +++ b/examples/Inkplate5/Projects/Inkplate5_Crypto_Currency_Tracker/Network.h @@ -36,8 +36,6 @@ class Network bool getData(double *data, char * currency); private: - // Functions called from within our class - void setTime(); }; #endif diff --git a/examples/Inkplate5/Projects/Inkplate5_Daily_Weather_Station/Inkplate5_Daily_Weather_Station.ino b/examples/Inkplate5/Projects/Inkplate5_Daily_Weather_Station/Inkplate5_Daily_Weather_Station.ino index 1562d1bc..4715d8ab 100644 --- a/examples/Inkplate5/Projects/Inkplate5_Daily_Weather_Station/Inkplate5_Daily_Weather_Station.ino +++ b/examples/Inkplate5/Projects/Inkplate5_Daily_Weather_Station/Inkplate5_Daily_Weather_Station.ino @@ -65,6 +65,7 @@ char apiKey[] = ""; // Delay between API calls, about 1000 per month, which is the free tier limit #define DELAY_MS 267800L +#define DELAY_WIFI_RETRY_SECONDS 5 // Inkplate object Inkplate display(INKPLATE_1BIT); @@ -132,8 +133,36 @@ void setup() Serial.begin(115200); display.begin(); - // Calling our begin from network.h file - network.begin(ssid, pass); + // Try connecting to a WiFi network. + // Parameters are network SSID, password, timeout in seconds and whether to print to serial. + // If the Inkplate isn't able to connect to a network stop further code execution and print an error message. + if (!display.connectWiFi(ssid, pass, WIFI_TIMEOUT, true)) + { + //Can't connect to netowrk + // Clear display for the error message + display.clearDisplay(); + // Set the font size; + display.setTextSize(3); + // Set the cursor positions and print the text. + display.setCursor((display.width() / 2) - 200, display.height() / 2); + display.print(F("Unable to connect to ")); + display.println(F(ssid)); + display.setCursor((display.width() / 2) - 200, (display.height() / 2) + 30); + display.println(F("Please check ssid and pass!")); + // Display the error message on the Inkplate and go to deep sleep + display.display(); + esp_sleep_enable_timer_wakeup(100L * DELAY_WIFI_RETRY_SECONDS); + (void)esp_deep_sleep_start(); + } + + // After connecting to WiFi we need to get internet time from NTP server + time_t nowSec; + struct tm timeInfo; + // Fetch current time in epoch format and store it + display.getNTPEpoch(&nowSec); + gmtime_r(&nowSec, &timeInfo); + Serial.print(F("Current time: ")); + Serial.print(asctime(&timeInfo)); // Get all relevant data, see Network.cpp for info if (refreshes % fullRefresh == 0) diff --git a/examples/Inkplate5/Projects/Inkplate5_Daily_Weather_Station/Network.cpp b/examples/Inkplate5/Projects/Inkplate5_Daily_Weather_Station/Network.cpp index 52421210..2268848b 100644 --- a/examples/Inkplate5/Projects/Inkplate5_Daily_Weather_Station/Network.cpp +++ b/examples/Inkplate5/Projects/Inkplate5_Daily_Weather_Station/Network.cpp @@ -39,9 +39,6 @@ void Network::begin(char *ssid, char *pass) } } Serial.println(F(" connected")); - - // Find internet time - setTime(); } // Gets time from ntp server @@ -187,30 +184,4 @@ bool Network::getData(char *lat, char *lon, char *apiKey, char *city, char *temp http.end(); return 1; -} - -void Network::setTime() -{ - // Used for setting correct time - configTime(0, 0, "pool.ntp.org", "time.nist.gov"); - - Serial.print(F("Waiting for NTP time sync: ")); - time_t nowSecs = time(nullptr); - while (nowSecs < 8 * 3600 * 2) - { - // Print a dot every half a second while time is not set - delay(500); - Serial.print(F(".")); - yield(); - nowSecs = time(nullptr); - } - - Serial.println(); - - // Used to store time info - struct tm timeinfo; - gmtime_r(&nowSecs, &timeinfo); - - Serial.print(F("Current time: ")); - Serial.print(asctime(&timeinfo)); -} +} \ No newline at end of file diff --git a/examples/Inkplate5/Projects/Inkplate5_Daily_Weather_Station/Network.h b/examples/Inkplate5/Projects/Inkplate5_Daily_Weather_Station/Network.h index a3219fda..5cc77f65 100644 --- a/examples/Inkplate5/Projects/Inkplate5_Daily_Weather_Station/Network.h +++ b/examples/Inkplate5/Projects/Inkplate5_Daily_Weather_Station/Network.h @@ -42,8 +42,6 @@ class Network char *currentWeatherAbbr, char *abbr1, char *abbr2, char *abbr3, char *abbr4, uint8_t *hours, int *timezone); private: - // Functions called from within our class - void setTime(); }; #endif diff --git a/examples/Inkplate5/Projects/Inkplate5_Google_Calendar/Inkplate5_Google_Calendar.ino b/examples/Inkplate5/Projects/Inkplate5_Google_Calendar/Inkplate5_Google_Calendar.ino index 313d66a1..1e1c3fc6 100644 --- a/examples/Inkplate5/Projects/Inkplate5_Google_Calendar/Inkplate5_Google_Calendar.ino +++ b/examples/Inkplate5/Projects/Inkplate5_Google_Calendar/Inkplate5_Google_Calendar.ino @@ -56,6 +56,7 @@ int timeZone = 2; // 2 means UTC+2 // Delay between API calls in seconds #define DELAY_SECS 4 * 60 // Every 4 minutes +#define DELAY_WIFI_RETRY_SECONDS 5 //---------------------------------------------- @@ -104,7 +105,36 @@ void setup() display.setTextColor(BLACK); // Connect Inkplate to the WiFi network - network.begin(ssid, pass); + // Try connecting to a WiFi network. + // Parameters are network SSID, password, timeout in seconds and whether to print to serial. + // If the Inkplate isn't able to connect to a network stop further code execution and print an error message. + if (!display.connectWiFi(ssid, pass, WIFI_TIMEOUT, true)) + { + //Can't connect to netowrk + // Clear display for the error message + display.clearDisplay(); + // Set the font size; + display.setTextSize(3); + // Set the cursor positions and print the text. + display.setCursor((display.width() / 2) - 200, display.height() / 2); + display.print(F("Unable to connect to ")); + display.println(F(ssid)); + display.setCursor((display.width() / 2) - 200, (display.height() / 2) + 30); + display.println(F("Please check SSID and PASS!")); + // Display the error message on the Inkplate and go to deep sleep + display.display(); + esp_sleep_enable_timer_wakeup(1000L * DELAY_WIFI_RETRY_SECONDS); + (void)esp_deep_sleep_start(); + } + + // After connecting to WiFi we need to get internet time from NTP server + time_t nowSec; + struct tm timeInfo; + // Fetch current time in epoch format and store it + display.getNTPEpoch(&nowSec); + gmtime_r(&nowSec, &timeInfo); + Serial.print(F("Current time: ")); + Serial.print(asctime(&timeInfo)); // Get the data from Google Calendar // Repeat attempts until data is fully downloaded @@ -455,6 +485,10 @@ void drawData() &entries[entriesNum].timeStamp); } ++entriesNum; + if(entriesNum == 128) + { + break; + } } // Sort entries by time diff --git a/examples/Inkplate5/Projects/Inkplate5_Google_Calendar/Network.cpp b/examples/Inkplate5/Projects/Inkplate5_Google_Calendar/Network.cpp index d9250450..2c2aee49 100644 --- a/examples/Inkplate5/Projects/Inkplate5_Google_Calendar/Network.cpp +++ b/examples/Inkplate5/Projects/Inkplate5_Google_Calendar/Network.cpp @@ -42,9 +42,6 @@ void Network::begin(char *ssid, char *pass) } } Serial.println(F(" connected")); - - // Find and print internet time, the timezone will be added later - setTime(); } // Gets time from ntp server @@ -142,29 +139,3 @@ bool Network::getData(char *calendarURL, char *data) return !f; } - -// Find internet time -void Network::setTime() -{ - // Used for setting correct time - configTime(0, 0, "pool.ntp.org", "time.nist.gov"); - - Serial.print(F("Waiting for NTP time sync: ")); - time_t nowSecs = time(nullptr); - while (nowSecs < 8 * 3600 * 2) - { - delay(500); - Serial.print(F(".")); - yield(); - nowSecs = time(nullptr); - } - Serial.println(); - - // Used to store time info - struct tm timeinfo; - gmtime_r(&nowSecs, &timeinfo); - - // Print the current time without adding a timezone - Serial.print(F("Current time: ")); - Serial.print(asctime(&timeinfo)); -} diff --git a/examples/Inkplate5/Projects/Inkplate5_Google_Calendar/Network.h b/examples/Inkplate5/Projects/Inkplate5_Google_Calendar/Network.h index 38df6957..1da10c0f 100644 --- a/examples/Inkplate5/Projects/Inkplate5_Google_Calendar/Network.h +++ b/examples/Inkplate5/Projects/Inkplate5_Google_Calendar/Network.h @@ -35,8 +35,6 @@ class Network bool getData(char *calendarURL, char *data); private: - // Functions called from within our class - void setTime(); }; #endif diff --git a/examples/Inkplate5/Projects/Inkplate5_Hourly_Weather_Station/Inkplate5_Hourly_Weather_Station.ino b/examples/Inkplate5/Projects/Inkplate5_Hourly_Weather_Station/Inkplate5_Hourly_Weather_Station.ino index 96d23354..50cb7503 100644 --- a/examples/Inkplate5/Projects/Inkplate5_Hourly_Weather_Station/Inkplate5_Hourly_Weather_Station.ino +++ b/examples/Inkplate5/Projects/Inkplate5_Hourly_Weather_Station/Inkplate5_Hourly_Weather_Station.ino @@ -67,6 +67,7 @@ char apiKey[] = ""; // Delay between API calls, about 1000 per month, which is the free tier limit #define DELAY_MS 267800L +#define DELAY_WIFI_RETRY_SECONDS 5 // Inkplate object Inkplate display(INKPLATE_1BIT); @@ -139,8 +140,37 @@ void setup() Serial.begin(115200); display.begin(); - // Calling our begin from network.h file - network.begin(ssid, pass); + // Connect Inkplate to the WiFi network + // Try connecting to a WiFi network. + // Parameters are network SSID, password, timeout in seconds and whether to print to serial. + // If the Inkplate isn't able to connect to a network stop further code execution and print an error message. + if (!display.connectWiFi(ssid, pass, WIFI_TIMEOUT, true)) + { + //Can't connect to netowrk + // Clear display for the error message + display.clearDisplay(); + // Set the font size; + display.setTextSize(3); + // Set the cursor positions and print the text. + display.setCursor((display.width() / 2) - 200, display.height() / 2); + display.print(F("Unable to connect to ")); + display.println(F(ssid)); + display.setCursor((display.width() / 2) - 200, (display.height() / 2) + 30); + display.println(F("Please check SSID and PASS!")); + // Display the error message on the Inkplate and go to deep sleep + display.display(); + esp_sleep_enable_timer_wakeup(1000L * DELAY_WIFI_RETRY_SECONDS); + (void)esp_deep_sleep_start(); + } + + // After connecting to WiFi we need to get internet time from NTP server + time_t nowSec; + struct tm timeInfo; + // Fetch current time in epoch format and store it + display.getNTPEpoch(&nowSec); + gmtime_r(&nowSec, &timeInfo); + Serial.print(F("Current time: ")); + Serial.print(asctime(&timeInfo)); // Get all relevant data, see Network.cpp for info if (refreshes % fullRefresh == 0) diff --git a/examples/Inkplate5/Projects/Inkplate5_Hourly_Weather_Station/Network.cpp b/examples/Inkplate5/Projects/Inkplate5_Hourly_Weather_Station/Network.cpp index b77e0aef..240a15fd 100644 --- a/examples/Inkplate5/Projects/Inkplate5_Hourly_Weather_Station/Network.cpp +++ b/examples/Inkplate5/Projects/Inkplate5_Hourly_Weather_Station/Network.cpp @@ -44,9 +44,6 @@ void Network::begin(char *ssid, char *pass) } Serial.println(F(" connected")); - // Find internet time - setTime(); - // Reduce power by making WiFi module sleep WiFi.setSleep(1); } @@ -209,32 +206,6 @@ bool Network::getData(char *lat, char *lon, char *apiKey, char *temp1, char *tem return !f; } -void Network::setTime() -{ - // Used for setting correct time - configTime(0, 0, "pool.ntp.org", "time.nist.gov"); - - Serial.print(F("Waiting for NTP time sync: ")); - time_t nowSecs = time(nullptr); - while (nowSecs < 8 * 3600 * 2) - { - // Print a dot every half a second while time is not set - delay(500); - Serial.print(F(".")); - yield(); - nowSecs = time(nullptr); - } - - Serial.println(); - - // Used to store time info - struct tm timeinfo; - gmtime_r(&nowSecs, &timeinfo); - - Serial.print(F("Current time: ")); - Serial.print(asctime(&timeinfo)); -} - void Network::getHours(char *hour1, char *hour2, char *hour3, char *hour4, int timeZone) { // Format hours info diff --git a/examples/Inkplate5/Projects/Inkplate5_Hourly_Weather_Station/Network.h b/examples/Inkplate5/Projects/Inkplate5_Hourly_Weather_Station/Network.h index 2a499aed..6a493eed 100644 --- a/examples/Inkplate5/Projects/Inkplate5_Hourly_Weather_Station/Network.h +++ b/examples/Inkplate5/Projects/Inkplate5_Hourly_Weather_Station/Network.h @@ -38,8 +38,6 @@ class Network void getHours(char *hour1, char *hour2, char *hour3, char *hour4, int timeZone); private: - // Functions called from within our class - void setTime(); // Used for storing retrieved data timestamp time_t dataEpoch = 0; diff --git a/examples/Inkplate5/Projects/Inkplate5_News_API/Inkplate5_News_API.ino b/examples/Inkplate5/Projects/Inkplate5_News_API/Inkplate5_News_API.ino index d0636ce4..7351130d 100644 --- a/examples/Inkplate5/Projects/Inkplate5_News_API/Inkplate5_News_API.ino +++ b/examples/Inkplate5/Projects/Inkplate5_News_API/Inkplate5_News_API.ino @@ -36,6 +36,7 @@ char apiKey[] = ""; // You can obtain one here: https://newsapi.org/ // Here is set to 1 call per hour, but if you want to change it, have in mind that in the free plan there are only 100 // free API calls #define DELAY_MS 60 * 60 * 1000 +#define DELAY_WIFI_RETRY_SECONDS 5 //------------------------------------- @@ -64,8 +65,29 @@ void setup() display.begin(); // Init Inkplate library (you should call this function ONLY ONCE) display.setTextWrap(false); - // Our begin function - network.begin(ssid, pass); + // Connect Inkplate to the WiFi network + // Try connecting to a WiFi network. + // Parameters are network SSID, password, timeout in seconds and whether to print to serial. + // If the Inkplate isn't able to connect to a network stop further code execution and print an error message. + if (!display.connectWiFi(ssid, pass, WIFI_TIMEOUT, true)) + { + //Can't connect to netowrk + // Clear display for the error message + display.clearDisplay(); + // Set the font size; + display.setTextSize(3); + // Set the cursor positions and print the text. + display.setCursor((display.width() / 2) - 200, display.height() / 2); + display.print(F("Unable to connect to ")); + display.println(F(ssid)); + display.setCursor((display.width() / 2) - 200, (display.height() / 2) + 30); + display.println(F("Please check SSID and PASS!")); + // Display the error message on the Inkplate and go to deep sleep + display.display(); + esp_sleep_enable_timer_wakeup(1000L * DELAY_WIFI_RETRY_SECONDS); + (void)esp_deep_sleep_start(); + } + // Pointer to the struct that will hold all news data struct news *entities = NULL; diff --git a/examples/Inkplate5/Projects/Inkplate5_OpenWeather_Station/Inkplate5_OpenWeather_Station.ino b/examples/Inkplate5/Projects/Inkplate5_OpenWeather_Station/Inkplate5_OpenWeather_Station.ino index 4ba70a08..6e06b95f 100644 --- a/examples/Inkplate5/Projects/Inkplate5_OpenWeather_Station/Inkplate5_OpenWeather_Station.ino +++ b/examples/Inkplate5/Projects/Inkplate5_OpenWeather_Station/Inkplate5_OpenWeather_Station.ino @@ -44,6 +44,8 @@ // Change to your wifi ssid and password #define SSID "" #define PASS "" +// Variable to store WiFi connection timeout +int timeoutSeconds = 100; // Openweather API key /** @@ -66,7 +68,8 @@ bool metric = true; // <--- true is METRIC, false is IMPERIAL // Delay between API calls #define DELAY_MS 59000 - +#define DELAY_WIFI_RETRY_SECONDS 5 +#define DELAY_API_RETRY_SECONDS 5 // --------------------------------------- // Including fonts used @@ -119,36 +122,6 @@ char Output[200] = {0}; OpenWeatherOneCall OWOC; // Invoke OpenWeather Library time_t t = now(); -void connectWifi() -{ - if (WiFi.status() == WL_CONNECTED) - return; - - // Initiating wifi, like in BasicHttpClient example - WiFi.mode(WIFI_STA); - WiFi.begin(SSID, PASS); - - int cnt = 0; - Serial.print("Waiting for WiFi to connect "); - while ((WiFi.status() != WL_CONNECTED)) - { - Serial.print("."); - delay(1000); - ++cnt; - - if (cnt == 20) - { - Serial.println("Can't connect to WIFI, restarting"); - delay(100); - ESP.restart(); - } - } - Serial.print("\nConnected to "); - Serial.println(SSID); - Serial.print("IP address: "); - Serial.println(WiFi.localIP()); -} //======================== END WIFI CONNECT ======================= - void GetCurrentWeather() { //================================= @@ -206,75 +179,80 @@ void setup() { ; } + Serial.println("Serial Monitor Initialized"); - // Init Inkplate library (you should call this function ONLY ONCE) + // display.begin(); - connectWifi(); + // Initial cleaning of buffer and physical screen + display.clearDisplay(); + display.display(); + + // Welcome screen + display.setCursor(215, 400); + display.setTextSize(3); + display.print(F("Welcome to Wol Inkplate 5 weather example!")); + display.display(); + Serial.println("Welcome to Wol Inkplate 5 weather example!"); + display.display(); + + // Try connecting to a WiFi network. + // Parameters are network SSID, password, timeout in seconds and whether to print to serial. + // If the Inkplate isn't able to connect to a network stop further code execution and print an error message. + if (!display.connectWiFi(SSID, PASS, timeoutSeconds, true)) + { + //Can't connect to netowrk + // Clear display for the error message + display.clearDisplay(); + // Set the font size; + display.setTextSize(3); + // Set the cursor positions and print the text. + display.setCursor((display.width() / 2) - 200, display.height() / 2); + display.print(F("Unable to connect to ")); + display.println(F(SSID)); + display.setCursor((display.width() / 2) - 200, (display.height() / 2) + 30); + display.println(F("Please check SSID and PASS!")); + // Display the error message on the Inkplate and go to deep sleep + display.display(); + esp_sleep_enable_timer_wakeup(1000L * DELAY_WIFI_RETRY_SECONDS); + (void)esp_deep_sleep_start(); + } // Check if we have a valid API key: Serial.println("Checking if API key is valid..."); - if(!checkIfAPIKeyIsValid(APIKEY)) + if (!checkIfAPIKeyIsValid(APIKEY)) { // If we don't, notify the user Serial.println("API key is invalid!"); display.clearDisplay(); - display.setCursor(0,0); + display.setCursor(0, 0); display.setTextSize(2); display.println("Can't get data from OpenWeatherMaps! Check your API key!"); display.println("Only older API keys for OneCall 2.5 work in free tier."); display.println("See the code comments the example for more info."); display.display(); - while(1) - { - delay(100); - } + esp_sleep_enable_timer_wakeup(1000L * DELAY_API_RETRY_SECONDS); + (void)esp_deep_sleep_start(); } Serial.println("API key is valid!"); - // Set the temp. unit - tempUnit = (metric == 1 ? 'C' : 'F'); -} - -void loop() -{ // Clear display t = now(); - if ((minute(t) % 30) == 0) // Also returns 0 when time isn't set { GetCurrentWeather(); - Serial.println("got weather data"); display.clearDisplay(); - drawCurrent(); - Serial.println("draw current"); drawForecast(); - Serial.println("draw forecast"); + drawCurrent(); drawHourly(); - Serial.println("draw hourly"); drawTime(); - Serial.println("draw time"); drawMoon(); - Serial.println("draw moon"); display.display(); } else { drawTime(); - - // Do a refresh - if (refreshes >= fullRefresh) - { - // Full refresh - display.display(); - refreshes = 0; - } - else - { - // Partial refresh (only content that was changed) - display.partialUpdate(); - ++refreshes; - } + display.partialUpdate(); } // wait for the turn of the minute before sleeping @@ -283,8 +261,13 @@ void loop() } // Go to sleep before checking again + ++refreshes; esp_sleep_enable_timer_wakeup(1000L * DELAY_MS); - (void)esp_light_sleep_start(); + (void)esp_deep_sleep_start(); +} + +void loop() +{ } // Function for drawing weather info diff --git a/examples/Inkplate5/Projects/Inkplate5_Quotables/Inkplate5_Quotables.ino b/examples/Inkplate5/Projects/Inkplate5_Quotables/Inkplate5_Quotables.ino index dc930e93..eb0f3893 100644 --- a/examples/Inkplate5/Projects/Inkplate5_Quotables/Inkplate5_Quotables.ino +++ b/examples/Inkplate5/Projects/Inkplate5_Quotables/Inkplate5_Quotables.ino @@ -35,6 +35,7 @@ char pass[] = ""; // Delay between API calls in seconds, 300 seconds is 5 minutes #define DELAY_S 300 +#define DELAY_WIFI_RETRY_SECONDS 5 //------------------------------------- @@ -71,8 +72,27 @@ void setup() display.begin(); display.setTextWrap(false); // Set text wrapping to true - // Our begin function - network.begin(ssid, pass); + // Try connecting to a WiFi network. + // Parameters are network SSID, password, timeout in seconds and whether to print to serial. + // If the Inkplate isn't able to connect to a network stop further code execution and print an error message. + if (!display.connectWiFi(ssid, pass, WIFI_TIMEOUT, true)) + { + //Can't connect to netowrk + // Clear display for the error message + display.clearDisplay(); + // Set the font size; + display.setTextSize(3); + // Set the cursor positions and print the text. + display.setCursor((display.width() / 2) - 200, display.height() / 2); + display.print(F("Unable to connect to ")); + display.println(F(ssid)); + display.setCursor((display.width() / 2) - 200, (display.height() / 2) + 30); + display.println(F("Please check SSID and PASS!")); + // Display the error message on the Inkplate and go to deep sleep + display.display(); + esp_sleep_enable_timer_wakeup(1000L * DELAY_WIFI_RETRY_SECONDS); + (void)esp_deep_sleep_start(); + } Serial.print("Retrying retriving data"); int n = 0; // For counting tries diff --git a/examples/Inkplate5/Projects/Inkplate5_Youtube_Subscriber_Counter/Inkplate5_Youtube_Subscriber_Counter.ino b/examples/Inkplate5/Projects/Inkplate5_Youtube_Subscriber_Counter/Inkplate5_Youtube_Subscriber_Counter.ino index 7d7c8bc9..da2d2cfe 100644 --- a/examples/Inkplate5/Projects/Inkplate5_Youtube_Subscriber_Counter/Inkplate5_Youtube_Subscriber_Counter.ino +++ b/examples/Inkplate5/Projects/Inkplate5_Youtube_Subscriber_Counter/Inkplate5_Youtube_Subscriber_Counter.ino @@ -42,7 +42,7 @@ // Delay between API calls in miliseconds (10 minutes) #define DELAY_MS 10 * 60 * 1000 - +#define DELAY_WIFI_RETRY_SECONDS 5 // Create object with all networking functions Network network; @@ -124,8 +124,27 @@ void setup() delay(1000); } - // Our begin function - network.begin(ssid, pass); + // Try connecting to a WiFi network. + // Parameters are network SSID, password, timeout in seconds and whether to print to serial. + // If the Inkplate isn't able to connect to a network stop further code execution and print an error message. + if (!display.connectWiFi(ssid, pass, WIFI_TIMEOUT, true)) + { + //Can't connect to netowrk + // Clear display for the error message + display.clearDisplay(); + // Set the font size; + display.setTextSize(3); + // Set the cursor positions and print the text. + display.setCursor((display.width() / 2) - 200, display.height() / 2); + display.print(F("Unable to connect to ")); + display.println(F(ssid)); + display.setCursor((display.width() / 2) - 200, (display.height() / 2) + 30); + display.println(F("Please check SSID and PASS!")); + // Display the error message on the Inkplate and go to deep sleep + display.display(); + esp_sleep_enable_timer_wakeup(1000L * DELAY_WIFI_RETRY_SECONDS); + (void)esp_deep_sleep_start(); + } while (!network.getData(&channel, channel_id, api_key, &display)) { diff --git a/examples/Inkplate5V2/Projects/Inkplate5V2_Crypto_Currency_Tracker/Inkplate5V2_Crypto_Currency_Tracker.ino b/examples/Inkplate5V2/Projects/Inkplate5V2_Crypto_Currency_Tracker/Inkplate5V2_Crypto_Currency_Tracker.ino index aeae106c..c2e4fd8c 100644 --- a/examples/Inkplate5V2/Projects/Inkplate5V2_Crypto_Currency_Tracker/Inkplate5V2_Crypto_Currency_Tracker.ino +++ b/examples/Inkplate5V2/Projects/Inkplate5V2_Crypto_Currency_Tracker/Inkplate5V2_Crypto_Currency_Tracker.ino @@ -37,6 +37,7 @@ char pass[] = ""; // Delay between API calls in miliseconds (first 60 represents minutes so you can change to your need) #define DELAY_MS 3 * 60 * 1000 +#define DELAY_WIFI_RETRY_SECONDS 5 // OPTIONAL: // Change to a different currency @@ -117,6 +118,7 @@ textElement elements[] = { // Our functions declared below setup and loop void drawGraph(); void drawAll(); +void setTime(); void setup() { @@ -128,8 +130,30 @@ void setup() display.setTextWrap(false); display.setTextColor(0, 7); - // Our begin function - network.begin(ssid, pass); + // Try connecting to a WiFi network. + // Parameters are network SSID, password, timeout in seconds and whether to print to serial. + // If the Inkplate isn't able to connect to a network stop further code execution and print an error message. + if (!display.connectWiFi(ssid, pass, WIFI_TIMEOUT, true)) + { + // Can't connect to netowrk + // Clear display for the error message + display.clearDisplay(); + // Set the font size; + display.setTextSize(3); + // Set the cursor positions and print the text. + display.setCursor((display.width() / 2) - 200, display.height() / 2); + display.print(F("Unable to connect to ")); + display.println(F(ssid)); + display.setCursor((display.width() / 2) - 200, (display.height() / 2) + 30); + display.println(F("Please check ssid and pass!")); + // Display the error message on the Inkplate and go to deep sleep + display.display(); + esp_sleep_enable_timer_wakeup(1000L * DELAY_WIFI_RETRY_SECONDS); + (void)esp_deep_sleep_start(); + } + + // After connecting to WiFi we need to get internet time from NTP server + setTime(); Serial.println("Retrying retriving data"); while (!network.getData(data, currency)) @@ -155,6 +179,26 @@ void loop() // Never here } +// Function for getting time from NTP server +void setTime() +{ + // Structure used to hold time information + struct tm timeInfo; + time_t nowSec; + // Fetch current time in epoch format and store it + display.getNTPEpoch(&nowSec); + // This loop ensures that the NTP time fetched is valid and beyond a certain threshold + while (nowSec < 8 * 3600 * 2) + { + delay(500); + yield(); + display.getNTPEpoch(&nowSec); + } + gmtime_r(&nowSec, &timeInfo); + Serial.print(F("Current time: ")); + Serial.print(asctime(&timeInfo)); +} + // Function to draw our graph void drawGraph() { diff --git a/examples/Inkplate5V2/Projects/Inkplate5V2_Crypto_Currency_Tracker/Network.cpp b/examples/Inkplate5V2/Projects/Inkplate5V2_Crypto_Currency_Tracker/Network.cpp index 546f287c..94dd4de0 100644 --- a/examples/Inkplate5V2/Projects/Inkplate5V2_Crypto_Currency_Tracker/Network.cpp +++ b/examples/Inkplate5V2/Projects/Inkplate5V2_Crypto_Currency_Tracker/Network.cpp @@ -61,8 +61,6 @@ void Network::begin(char * ssid, char * pass) } Serial.println(F(" connected")); - // Find internet time - setTime(); } /** @@ -181,34 +179,4 @@ bool Network::getData(double *data, char * currency) WiFi.setSleep(sleep); return !f; -} - -/** - * @brief Set the internal time with data from NTP server - * - * @returns None -*/ -void Network::setTime() -{ - // Used for setting correct time - configTime(0, 0, "pool.ntp.org", "time.nist.gov"); - - Serial.print(F("Waiting for NTP time sync: ")); - time_t nowSecs = time(nullptr); - while (nowSecs < 8 * 3600 * 2) - { - delay(500); - Serial.print(F(".")); - yield(); - nowSecs = time(nullptr); - } - - Serial.println(); - - // Used to store time info - struct tm timeinfo; - gmtime_r(&nowSecs, &timeinfo); - - Serial.print(F("Current time: ")); - Serial.print(asctime(&timeinfo)); -} +} \ No newline at end of file diff --git a/examples/Inkplate5V2/Projects/Inkplate5V2_Crypto_Currency_Tracker/Network.h b/examples/Inkplate5V2/Projects/Inkplate5V2_Crypto_Currency_Tracker/Network.h index 87aa49b2..0746b716 100644 --- a/examples/Inkplate5V2/Projects/Inkplate5V2_Crypto_Currency_Tracker/Network.h +++ b/examples/Inkplate5V2/Projects/Inkplate5V2_Crypto_Currency_Tracker/Network.h @@ -36,8 +36,6 @@ class Network bool getData(double *data, char * currency); private: - // Functions called from within our class - void setTime(); }; #endif diff --git a/examples/Inkplate5V2/Projects/Inkplate5V2_Daily_Weather_Station/Inkplate5V2_Daily_Weather_Station.ino b/examples/Inkplate5V2/Projects/Inkplate5V2_Daily_Weather_Station/Inkplate5V2_Daily_Weather_Station.ino index db3a496e..5985f073 100644 --- a/examples/Inkplate5V2/Projects/Inkplate5V2_Daily_Weather_Station/Inkplate5V2_Daily_Weather_Station.ino +++ b/examples/Inkplate5V2/Projects/Inkplate5V2_Daily_Weather_Station/Inkplate5V2_Daily_Weather_Station.ino @@ -65,6 +65,7 @@ char apiKey[] = ""; // Delay between API calls, about 1000 per month, which is the free tier limit #define DELAY_MS 267800L +#define DELAY_WIFI_RETRY_SECONDS 5 // Inkplate object Inkplate display(INKPLATE_1BIT); @@ -125,6 +126,7 @@ void drawCurrent(); void drawTemps(); void drawCity(); void drawTime(); +void setTime(); void setup() { @@ -132,8 +134,29 @@ void setup() Serial.begin(115200); display.begin(); - // Calling our begin from network.h file - network.begin(ssid, pass); + // Try connecting to a WiFi network. + // Parameters are network SSID, password, timeout in seconds and whether to print to serial. + // If the Inkplate isn't able to connect to a network stop further code execution and print an error message. + if (!display.connectWiFi(ssid, pass, WIFI_TIMEOUT, true)) + { + //Can't connect to netowrk + // Clear display for the error message + display.clearDisplay(); + // Set the font size; + display.setTextSize(3); + // Set the cursor positions and print the text. + display.setCursor((display.width() / 2) - 200, display.height() / 2); + display.print(F("Unable to connect to ")); + display.println(F(ssid)); + display.setCursor((display.width() / 2) - 200, (display.height() / 2) + 30); + display.println(F("Please check ssid and pass!")); + // Display the error message on the Inkplate and go to deep sleep + display.display(); + esp_sleep_enable_timer_wakeup(100L * DELAY_WIFI_RETRY_SECONDS); + (void)esp_deep_sleep_start(); + } + + setTime(); // Get all relevant data, see Network.cpp for info if (refreshes % fullRefresh == 0) @@ -173,6 +196,27 @@ void loop() // time. loop() must be empty! } +// Function for getting time from NTP server +void setTime() +{ + // Structure used to hold time information + struct tm timeInfo; + display.getNTPDateTime(&timeInfo); + time_t nowSec; + // Fetch current time in epoch format and store it + display.getNTPEpoch(&nowSec); + // This loop ensures that the NTP time fetched is valid and beyond a certain threshold + while (nowSec < 8 * 3600 * 2) + { + delay(500); + yield(); + display.getNTPEpoch(&nowSec); + } + gmtime_r(&nowSec, &timeInfo); + Serial.print(F("Current time: ")); + Serial.print(asctime(&timeInfo)); +} + // Function for drawing weather info void drawWeather() { diff --git a/examples/Inkplate5V2/Projects/Inkplate5V2_Daily_Weather_Station/Network.cpp b/examples/Inkplate5V2/Projects/Inkplate5V2_Daily_Weather_Station/Network.cpp index 52421210..d0b94d04 100644 --- a/examples/Inkplate5V2/Projects/Inkplate5V2_Daily_Weather_Station/Network.cpp +++ b/examples/Inkplate5V2/Projects/Inkplate5V2_Daily_Weather_Station/Network.cpp @@ -39,9 +39,6 @@ void Network::begin(char *ssid, char *pass) } } Serial.println(F(" connected")); - - // Find internet time - setTime(); } // Gets time from ntp server @@ -188,29 +185,3 @@ bool Network::getData(char *lat, char *lon, char *apiKey, char *city, char *temp return 1; } - -void Network::setTime() -{ - // Used for setting correct time - configTime(0, 0, "pool.ntp.org", "time.nist.gov"); - - Serial.print(F("Waiting for NTP time sync: ")); - time_t nowSecs = time(nullptr); - while (nowSecs < 8 * 3600 * 2) - { - // Print a dot every half a second while time is not set - delay(500); - Serial.print(F(".")); - yield(); - nowSecs = time(nullptr); - } - - Serial.println(); - - // Used to store time info - struct tm timeinfo; - gmtime_r(&nowSecs, &timeinfo); - - Serial.print(F("Current time: ")); - Serial.print(asctime(&timeinfo)); -} diff --git a/examples/Inkplate5V2/Projects/Inkplate5V2_Daily_Weather_Station/Network.h b/examples/Inkplate5V2/Projects/Inkplate5V2_Daily_Weather_Station/Network.h index a3219fda..5cc77f65 100644 --- a/examples/Inkplate5V2/Projects/Inkplate5V2_Daily_Weather_Station/Network.h +++ b/examples/Inkplate5V2/Projects/Inkplate5V2_Daily_Weather_Station/Network.h @@ -42,8 +42,6 @@ class Network char *currentWeatherAbbr, char *abbr1, char *abbr2, char *abbr3, char *abbr4, uint8_t *hours, int *timezone); private: - // Functions called from within our class - void setTime(); }; #endif diff --git a/examples/Inkplate5V2/Projects/Inkplate5V2_Google_Calendar/Inkplate5V2_Google_Calendar.ino b/examples/Inkplate5V2/Projects/Inkplate5V2_Google_Calendar/Inkplate5V2_Google_Calendar.ino index 237d063e..f276fa39 100644 --- a/examples/Inkplate5V2/Projects/Inkplate5V2_Google_Calendar/Inkplate5V2_Google_Calendar.ino +++ b/examples/Inkplate5V2/Projects/Inkplate5V2_Google_Calendar/Inkplate5V2_Google_Calendar.ino @@ -56,6 +56,7 @@ int timeZone = 2; // 2 means UTC+2 // Delay between API calls in seconds #define DELAY_SECS 4 * 60 // Every 4 minutes +#define DELAY_WIFI_RETRY_SECONDS 5 //---------------------------------------------- @@ -83,6 +84,8 @@ struct entry int entriesNum = 0; entry entries[128]; +void setTime(); + void setup() { // Init serial communication @@ -104,7 +107,29 @@ void setup() display.setTextColor(BLACK); // Connect Inkplate to the WiFi network - network.begin(ssid, pass); + // Try connecting to a WiFi network. + // Parameters are network SSID, password, timeout in seconds and whether to print to serial. + // If the Inkplate isn't able to connect to a network stop further code execution and print an error message. + if (!display.connectWiFi(ssid, pass, WIFI_TIMEOUT, true)) + { + //Can't connect to netowrk + // Clear display for the error message + display.clearDisplay(); + // Set the font size; + display.setTextSize(3); + // Set the cursor positions and print the text. + display.setCursor((display.width() / 2) - 200, display.height() / 2); + display.print(F("Unable to connect to ")); + display.println(F(ssid)); + display.setCursor((display.width() / 2) - 200, (display.height() / 2) + 30); + display.println(F("Please check SSID and PASS!")); + // Display the error message on the Inkplate and go to deep sleep + display.display(); + esp_sleep_enable_timer_wakeup(1000L * DELAY_WIFI_RETRY_SECONDS); + (void)esp_deep_sleep_start(); + } + + setTime(); // Get the data from Google Calendar // Repeat attempts until data is fully downloaded @@ -137,6 +162,27 @@ void loop() // time. loop() must be empty! } +// Function for getting time from NTP server +void setTime() +{ + // Structure used to hold time information + struct tm timeInfo; + // Fetch current time from an NTP server and store it + time_t nowSec; + // Fetch current time in epoch format and store it + display.getNTPEpoch(&nowSec); + // This loop ensures that the NTP time fetched is valid and beyond a certain threshold + while(nowSec < 8 * 3600 * 2) + { + delay(500); + yield(); + display.getNTPEpoch(&nowSec); + } + gmtime_r(&nowSec, &timeInfo); + Serial.print(F("Current time: ")); + Serial.print(asctime(&timeInfo)); +} + // Function for drawing calendar info void drawInfo() { @@ -455,6 +501,10 @@ void drawData() &entries[entriesNum].timeStamp); } ++entriesNum; + if(entriesNum == 128) + { + break; + } } // Sort entries by time diff --git a/examples/Inkplate5V2/Projects/Inkplate5V2_Google_Calendar/Network.cpp b/examples/Inkplate5V2/Projects/Inkplate5V2_Google_Calendar/Network.cpp index d9250450..b4201d79 100644 --- a/examples/Inkplate5V2/Projects/Inkplate5V2_Google_Calendar/Network.cpp +++ b/examples/Inkplate5V2/Projects/Inkplate5V2_Google_Calendar/Network.cpp @@ -43,8 +43,6 @@ void Network::begin(char *ssid, char *pass) } Serial.println(F(" connected")); - // Find and print internet time, the timezone will be added later - setTime(); } // Gets time from ntp server @@ -141,30 +139,4 @@ bool Network::getData(char *calendarURL, char *data) http.end(); return !f; -} - -// Find internet time -void Network::setTime() -{ - // Used for setting correct time - configTime(0, 0, "pool.ntp.org", "time.nist.gov"); - - Serial.print(F("Waiting for NTP time sync: ")); - time_t nowSecs = time(nullptr); - while (nowSecs < 8 * 3600 * 2) - { - delay(500); - Serial.print(F(".")); - yield(); - nowSecs = time(nullptr); - } - Serial.println(); - - // Used to store time info - struct tm timeinfo; - gmtime_r(&nowSecs, &timeinfo); - - // Print the current time without adding a timezone - Serial.print(F("Current time: ")); - Serial.print(asctime(&timeinfo)); -} +} \ No newline at end of file diff --git a/examples/Inkplate5V2/Projects/Inkplate5V2_Google_Calendar/Network.h b/examples/Inkplate5V2/Projects/Inkplate5V2_Google_Calendar/Network.h index 38df6957..1da10c0f 100644 --- a/examples/Inkplate5V2/Projects/Inkplate5V2_Google_Calendar/Network.h +++ b/examples/Inkplate5V2/Projects/Inkplate5V2_Google_Calendar/Network.h @@ -35,8 +35,6 @@ class Network bool getData(char *calendarURL, char *data); private: - // Functions called from within our class - void setTime(); }; #endif diff --git a/examples/Inkplate5V2/Projects/Inkplate5V2_Hourly_Weather_Station/Inkplate5V2_Hourly_Weather_Station.ino b/examples/Inkplate5V2/Projects/Inkplate5V2_Hourly_Weather_Station/Inkplate5V2_Hourly_Weather_Station.ino index e3ba8f9e..6be19ce3 100644 --- a/examples/Inkplate5V2/Projects/Inkplate5V2_Hourly_Weather_Station/Inkplate5V2_Hourly_Weather_Station.ino +++ b/examples/Inkplate5V2/Projects/Inkplate5V2_Hourly_Weather_Station/Inkplate5V2_Hourly_Weather_Station.ino @@ -67,6 +67,7 @@ char apiKey[] = ""; // Delay between API calls, about 1000 per month, which is the free tier limit #define DELAY_MS 267800L +#define DELAY_WIFI_RETRY_SECONDS 5 // Inkplate object Inkplate display(INKPLATE_1BIT); @@ -132,6 +133,7 @@ void drawCurrent(); void drawTemps(); void drawCity(); void drawTime(); +void setTime(); void setup() { @@ -139,8 +141,30 @@ void setup() Serial.begin(115200); display.begin(); - // Calling our begin from network.h file - network.begin(ssid, pass); + // Connect Inkplate to the WiFi network + // Try connecting to a WiFi network. + // Parameters are network SSID, password, timeout in seconds and whether to print to serial. + // If the Inkplate isn't able to connect to a network stop further code execution and print an error message. + if (!display.connectWiFi(ssid, pass, WIFI_TIMEOUT, true)) + { + //Can't connect to netowrk + // Clear display for the error message + display.clearDisplay(); + // Set the font size; + display.setTextSize(3); + // Set the cursor positions and print the text. + display.setCursor((display.width() / 2) - 200, display.height() / 2); + display.print(F("Unable to connect to ")); + display.println(F(ssid)); + display.setCursor((display.width() / 2) - 200, (display.height() / 2) + 30); + display.println(F("Please check SSID and PASS!")); + // Display the error message on the Inkplate and go to deep sleep + display.display(); + esp_sleep_enable_timer_wakeup(1000L * DELAY_WIFI_RETRY_SECONDS); + (void)esp_deep_sleep_start(); + } + + setTime(); // Get all relevant data, see Network.cpp for info if (refreshes % fullRefresh == 0) @@ -183,6 +207,26 @@ void loop() // time. loop() must be empty! } +// Function for getting time from NTP server +void setTime() +{ + // Structure used to hold time information + struct tm timeInfo; + time_t nowSec; + // Fetch current time in epoch format and store it + display.getNTPEpoch(&nowSec); + // This loop ensures that the NTP time fetched is valid and beyond a certain threshold + while (nowSec < 8 * 3600 * 2) + { + delay(500); + yield(); + display.getNTPEpoch(&nowSec); + } + gmtime_r(&nowSec, &timeInfo); + Serial.print(F("Current time: ")); + Serial.print(asctime(&timeInfo)); +} + // Function for drawing weather info void drawWeather() { diff --git a/examples/Inkplate5V2/Projects/Inkplate5V2_Hourly_Weather_Station/Network.cpp b/examples/Inkplate5V2/Projects/Inkplate5V2_Hourly_Weather_Station/Network.cpp index b77e0aef..240a15fd 100644 --- a/examples/Inkplate5V2/Projects/Inkplate5V2_Hourly_Weather_Station/Network.cpp +++ b/examples/Inkplate5V2/Projects/Inkplate5V2_Hourly_Weather_Station/Network.cpp @@ -44,9 +44,6 @@ void Network::begin(char *ssid, char *pass) } Serial.println(F(" connected")); - // Find internet time - setTime(); - // Reduce power by making WiFi module sleep WiFi.setSleep(1); } @@ -209,32 +206,6 @@ bool Network::getData(char *lat, char *lon, char *apiKey, char *temp1, char *tem return !f; } -void Network::setTime() -{ - // Used for setting correct time - configTime(0, 0, "pool.ntp.org", "time.nist.gov"); - - Serial.print(F("Waiting for NTP time sync: ")); - time_t nowSecs = time(nullptr); - while (nowSecs < 8 * 3600 * 2) - { - // Print a dot every half a second while time is not set - delay(500); - Serial.print(F(".")); - yield(); - nowSecs = time(nullptr); - } - - Serial.println(); - - // Used to store time info - struct tm timeinfo; - gmtime_r(&nowSecs, &timeinfo); - - Serial.print(F("Current time: ")); - Serial.print(asctime(&timeinfo)); -} - void Network::getHours(char *hour1, char *hour2, char *hour3, char *hour4, int timeZone) { // Format hours info diff --git a/examples/Inkplate5V2/Projects/Inkplate5V2_Hourly_Weather_Station/Network.h b/examples/Inkplate5V2/Projects/Inkplate5V2_Hourly_Weather_Station/Network.h index 2a499aed..374c94f1 100644 --- a/examples/Inkplate5V2/Projects/Inkplate5V2_Hourly_Weather_Station/Network.h +++ b/examples/Inkplate5V2/Projects/Inkplate5V2_Hourly_Weather_Station/Network.h @@ -38,9 +38,6 @@ class Network void getHours(char *hour1, char *hour2, char *hour3, char *hour4, int timeZone); private: - // Functions called from within our class - void setTime(); - // Used for storing retrieved data timestamp time_t dataEpoch = 0; }; diff --git a/examples/Inkplate5V2/Projects/Inkplate5V2_News_API/Inkplate5V2_News_API.ino b/examples/Inkplate5V2/Projects/Inkplate5V2_News_API/Inkplate5V2_News_API.ino index 4ded79dc..aba56296 100644 --- a/examples/Inkplate5V2/Projects/Inkplate5V2_News_API/Inkplate5V2_News_API.ino +++ b/examples/Inkplate5V2/Projects/Inkplate5V2_News_API/Inkplate5V2_News_API.ino @@ -36,6 +36,7 @@ char apiKey[] = ""; // You can obtain one here: https://newsapi.org/ // Here is set to 1 call per hour, but if you want to change it, have in mind that in the free plan there are only 100 // free API calls #define DELAY_MS 60 * 60 * 1000 +#define DELAY_WIFI_RETRY_SECONDS 5 //------------------------------------- @@ -64,8 +65,28 @@ void setup() display.begin(); // Init Inkplate library (you should call this function ONLY ONCE) display.setTextWrap(false); - // Our begin function - network.begin(ssid, pass); + // Connect Inkplate to the WiFi network + // Try connecting to a WiFi network. + // Parameters are network SSID, password, timeout in seconds and whether to print to serial. + // If the Inkplate isn't able to connect to a network stop further code execution and print an error message. + if (!display.connectWiFi(ssid, pass, WIFI_TIMEOUT, true)) + { + //Can't connect to netowrk + // Clear display for the error message + display.clearDisplay(); + // Set the font size; + display.setTextSize(3); + // Set the cursor positions and print the text. + display.setCursor((display.width() / 2) - 200, display.height() / 2); + display.print(F("Unable to connect to ")); + display.println(F(ssid)); + display.setCursor((display.width() / 2) - 200, (display.height() / 2) + 30); + display.println(F("Please check SSID and PASS!")); + // Display the error message on the Inkplate and go to deep sleep + display.display(); + esp_sleep_enable_timer_wakeup(1000L * DELAY_WIFI_RETRY_SECONDS); + (void)esp_deep_sleep_start(); + } // Pointer to the struct that will hold all news data struct news *entities = NULL; diff --git a/examples/Inkplate5V2/Projects/Inkplate5V2_OpenWeather_Station/Inkplate5V2_OpenWeather_Station.ino b/examples/Inkplate5V2/Projects/Inkplate5V2_OpenWeather_Station/Inkplate5V2_OpenWeather_Station.ino index b771f4ba..24b9de70 100644 --- a/examples/Inkplate5V2/Projects/Inkplate5V2_OpenWeather_Station/Inkplate5V2_OpenWeather_Station.ino +++ b/examples/Inkplate5V2/Projects/Inkplate5V2_OpenWeather_Station/Inkplate5V2_OpenWeather_Station.ino @@ -44,6 +44,8 @@ // Change to your wifi ssid and password #define SSID "" #define PASS "" +// Variable to store WiFi connection timeout +int timeoutSeconds = 100; // Openweather API key /** @@ -66,6 +68,8 @@ bool metric = true; // <--- true is METRIC, false is IMPERIAL // Delay between API calls #define DELAY_MS 59000 +#define DELAY_WIFI_RETRY_SECONDS 5 +#define DELAY_API_RETRY_SECONDS 5 // --------------------------------------- @@ -206,75 +210,80 @@ void setup() { ; } + Serial.println("Serial Monitor Initialized"); - // Init Inkplate library (you should call this function ONLY ONCE) + // display.begin(); - connectWifi(); + // Initial cleaning of buffer and physical screen + display.clearDisplay(); + display.display(); + + // Welcome screen + display.setCursor(215, 400); + display.setTextSize(3); + display.print(F("Welcome to Wol Inkplate 5V2 weather example!")); + display.display(); + Serial.println("Welcome to Wol Inkplate 5V2 weather example!"); + display.display(); + + // Try connecting to a WiFi network. + // Parameters are network SSID, password, timeout in seconds and whether to print to serial. + // If the Inkplate isn't able to connect to a network stop further code execution and print an error message. + if (!display.connectWiFi(SSID, PASS, timeoutSeconds, true)) + { + //Can't connect to netowrk + // Clear display for the error message + display.clearDisplay(); + // Set the font size; + display.setTextSize(3); + // Set the cursor positions and print the text. + display.setCursor((display.width() / 2) - 200, display.height() / 2); + display.print(F("Unable to connect to ")); + display.println(F(SSID)); + display.setCursor((display.width() / 2) - 200, (display.height() / 2) + 30); + display.println(F("Please check SSID and PASS!")); + // Display the error message on the Inkplate and go to deep sleep + display.display(); + esp_sleep_enable_timer_wakeup(1000L * DELAY_WIFI_RETRY_SECONDS); + (void)esp_deep_sleep_start(); + } // Check if we have a valid API key: Serial.println("Checking if API key is valid..."); - if(!checkIfAPIKeyIsValid(APIKEY)) + if (!checkIfAPIKeyIsValid(APIKEY)) { // If we don't, notify the user Serial.println("API key is invalid!"); display.clearDisplay(); - display.setCursor(0,0); + display.setCursor(0, 0); display.setTextSize(2); display.println("Can't get data from OpenWeatherMaps! Check your API key!"); display.println("Only older API keys for OneCall 2.5 work in free tier."); display.println("See the code comments the example for more info."); display.display(); - while(1) - { - delay(100); - } + esp_sleep_enable_timer_wakeup(1000L * DELAY_API_RETRY_SECONDS); + (void)esp_deep_sleep_start(); } Serial.println("API key is valid!"); - // Set the temp. unit - tempUnit = (metric == 1 ? 'C' : 'F'); -} - -void loop() -{ // Clear display t = now(); - if ((minute(t) % 30) == 0) // Also returns 0 when time isn't set { GetCurrentWeather(); - Serial.println("got weather data"); display.clearDisplay(); - drawCurrent(); - Serial.println("draw current"); drawForecast(); - Serial.println("draw forecast"); + drawCurrent(); drawHourly(); - Serial.println("draw hourly"); drawTime(); - Serial.println("draw time"); drawMoon(); - Serial.println("draw moon"); display.display(); } else { drawTime(); - - // Do a refresh - if (refreshes >= fullRefresh) - { - // Full refresh - display.display(); - refreshes = 0; - } - else - { - // Partial refresh (only content that was changed) - display.partialUpdate(); - ++refreshes; - } + display.partialUpdate(); } // wait for the turn of the minute before sleeping @@ -283,10 +292,16 @@ void loop() } // Go to sleep before checking again + ++refreshes; esp_sleep_enable_timer_wakeup(1000L * DELAY_MS); - (void)esp_light_sleep_start(); + (void)esp_deep_sleep_start(); +} + +void loop() +{ } + // Function for drawing weather info void alignText(const char align, const char *text, int16_t x, int16_t y) { diff --git a/examples/Inkplate5V2/Projects/Inkplate5V2_Quotables/Inkplate5V2_Quotables.ino b/examples/Inkplate5V2/Projects/Inkplate5V2_Quotables/Inkplate5V2_Quotables.ino index 05b5cbab..1a938941 100644 --- a/examples/Inkplate5V2/Projects/Inkplate5V2_Quotables/Inkplate5V2_Quotables.ino +++ b/examples/Inkplate5V2/Projects/Inkplate5V2_Quotables/Inkplate5V2_Quotables.ino @@ -55,6 +55,7 @@ Inkplate display(INKPLATE_1BIT); // Define the size of the buffer for storing quotes #define BUFFER_SIZE 256 +#define DELAY_WIFI_RETRY_SECONDS 5 // Our functions declared below setup and loop void drawAll(); @@ -71,8 +72,27 @@ void setup() display.begin(); display.setTextWrap(false); // Set text wrapping to true - // Our begin function - network.begin(ssid, pass); + // Try connecting to a WiFi network. + // Parameters are network SSID, password, timeout in seconds and whether to print to serial. + // If the Inkplate isn't able to connect to a network stop further code execution and print an error message. + if (!display.connectWiFi(ssid, pass, WIFI_TIMEOUT, true)) + { + //Can't connect to netowrk + // Clear display for the error message + display.clearDisplay(); + // Set the font size; + display.setTextSize(3); + // Set the cursor positions and print the text. + display.setCursor((display.width() / 2) - 200, display.height() / 2); + display.print(F("Unable to connect to ")); + display.println(F(ssid)); + display.setCursor((display.width() / 2) - 200, (display.height() / 2) + 30); + display.println(F("Please check SSID and PASS!")); + // Display the error message on the Inkplate and go to deep sleep + display.display(); + esp_sleep_enable_timer_wakeup(1000L * DELAY_WIFI_RETRY_SECONDS); + (void)esp_deep_sleep_start(); + } Serial.print("Retrying retriving data"); int n = 0; // For counting tries diff --git a/examples/Inkplate5V2/Projects/Inkplate5V2_Youtube_Subscriber_Counter/Inkplate5V2_Youtube_Subscriber_Counter.ino b/examples/Inkplate5V2/Projects/Inkplate5V2_Youtube_Subscriber_Counter/Inkplate5V2_Youtube_Subscriber_Counter.ino index fb9e95bf..3035b736 100644 --- a/examples/Inkplate5V2/Projects/Inkplate5V2_Youtube_Subscriber_Counter/Inkplate5V2_Youtube_Subscriber_Counter.ino +++ b/examples/Inkplate5V2/Projects/Inkplate5V2_Youtube_Subscriber_Counter/Inkplate5V2_Youtube_Subscriber_Counter.ino @@ -42,6 +42,7 @@ // Delay between API calls in miliseconds (10 minutes) #define DELAY_MS 10 * 60 * 1000 +#define DELAY_WIFI_RETRY_SECONDS 5 // Create object with all networking functions Network network; @@ -124,8 +125,27 @@ void setup() delay(1000); } - // Our begin function - network.begin(ssid, pass); + // Try connecting to a WiFi network. + // Parameters are network SSID, password, timeout in seconds and whether to print to serial. + // If the Inkplate isn't able to connect to a network stop further code execution and print an error message. + if (!display.connectWiFi(ssid, pass, WIFI_TIMEOUT, true)) + { + //Can't connect to netowrk + // Clear display for the error message + display.clearDisplay(); + // Set the font size; + display.setTextSize(3); + // Set the cursor positions and print the text. + display.setCursor((display.width() / 2) - 200, display.height() / 2); + display.print(F("Unable to connect to ")); + display.println(F(ssid)); + display.setCursor((display.width() / 2) - 200, (display.height() / 2) + 30); + display.println(F("Please check SSID and PASS!")); + // Display the error message on the Inkplate and go to deep sleep + display.display(); + esp_sleep_enable_timer_wakeup(1000L * DELAY_WIFI_RETRY_SECONDS); + (void)esp_deep_sleep_start(); + } while (!network.getData(&channel, channel_id, api_key, &display)) { diff --git a/examples/Inkplate6/Projects/Inkplate6_Crypto_Currency_Tracker/Inkplate6_Crypto_Currency_Tracker.ino b/examples/Inkplate6/Projects/Inkplate6_Crypto_Currency_Tracker/Inkplate6_Crypto_Currency_Tracker.ino index 82699ebe..c40007b0 100644 --- a/examples/Inkplate6/Projects/Inkplate6_Crypto_Currency_Tracker/Inkplate6_Crypto_Currency_Tracker.ino +++ b/examples/Inkplate6/Projects/Inkplate6_Crypto_Currency_Tracker/Inkplate6_Crypto_Currency_Tracker.ino @@ -1,4 +1,4 @@ -/* + /* Inkplate6_Crypto_Currency_Tracker example for Soldered Inkplate 6 For this example you will need only USB cable and Inkplate 6. Select "e-radionica Inkplate6" or "Soldered Inkplate6" from Tools -> Board menu. @@ -37,6 +37,7 @@ char pass[] = ""; // Delay between API calls in miliseconds (first 60 represents minutes so you can change to your need) #define DELAY_MS 3 * 60 * 1000 +#define DELAY_WIFI_RETRY_SECONDS 5 // OPTIONAL: // change to a different currency @@ -116,6 +117,7 @@ textElement elements[] = { // Our functions declared below setup and loop void drawGraph(); void drawAll(); +void setTime(); void setup() { @@ -127,8 +129,30 @@ void setup() display.setTextWrap(false); display.setTextColor(0, 7); - // Our begin function - network.begin(); + // Try connecting to a WiFi network. + // Parameters are network SSID, password, timeout in seconds and whether to print to serial. + // If the Inkplate isn't able to connect to a network stop further code execution and print an error message. + if (!display.connectWiFi(ssid, pass, WIFI_TIMEOUT, true)) + { + // Can't connect to netowrk + // Clear display for the error message + display.clearDisplay(); + // Set the font size; + display.setTextSize(3); + // Set the cursor positions and print the text. + display.setCursor((display.width() / 2) - 200, display.height() / 2); + display.print(F("Unable to connect to ")); + display.println(F(ssid)); + display.setCursor((display.width() / 2) - 200, (display.height() / 2) + 30); + display.println(F("Please check ssid and pass!")); + // Display the error message on the Inkplate and go to deep sleep + display.display(); + esp_sleep_enable_timer_wakeup(1000L * DELAY_WIFI_RETRY_SECONDS); + (void)esp_deep_sleep_start(); + } + + // After connecting to WiFi we need to get internet time from NTP server + setTime(); Serial.print("Retrying retriving data"); while (!network.getData(data)) @@ -154,6 +178,26 @@ void loop() // Never here } +// Function for getting time from NTP server +void setTime() +{ + // Structure used to hold time information + struct tm timeInfo; + time_t nowSec; + // Fetch current time in epoch format and store it + display.getNTPEpoch(&nowSec); + // This loop ensures that the NTP time fetched is valid and beyond a certain threshold + while (nowSec < 8 * 3600 * 2) + { + delay(500); + yield(); + display.getNTPEpoch(&nowSec); + } + gmtime_r(&nowSec, &timeInfo); + Serial.print(F("Current time: ")); + Serial.print(asctime(&timeInfo)); +} + // Function to draw our graph void drawGraph() { diff --git a/examples/Inkplate6/Projects/Inkplate6_Crypto_Currency_Tracker/Network.cpp b/examples/Inkplate6/Projects/Inkplate6_Crypto_Currency_Tracker/Network.cpp index 17807707..f45d7298 100644 --- a/examples/Inkplate6/Projects/Inkplate6_Crypto_Currency_Tracker/Network.cpp +++ b/examples/Inkplate6/Projects/Inkplate6_Crypto_Currency_Tracker/Network.cpp @@ -59,8 +59,6 @@ void Network::begin() } Serial.println(F(" connected")); - // Find internet time - setTime(); } // Gets time from ntp server @@ -189,29 +187,3 @@ bool Network::getData(double *data) return !f; } - -// Function for initial time setting ovet the ntp server -void Network::setTime() -{ - // Used for setting correct time - configTime(0, 0, "pool.ntp.org", "time.nist.gov"); - - Serial.print(F("Waiting for NTP time sync: ")); - time_t nowSecs = time(nullptr); - while (nowSecs < 8 * 3600 * 2) - { - delay(500); - Serial.print(F(".")); - yield(); - nowSecs = time(nullptr); - } - - Serial.println(); - - // Used to store time info - struct tm timeinfo; - gmtime_r(&nowSecs, &timeinfo); - - Serial.print(F("Current time: ")); - Serial.print(asctime(&timeinfo)); -} diff --git a/examples/Inkplate6/Projects/Inkplate6_Crypto_Currency_Tracker/Network.h b/examples/Inkplate6/Projects/Inkplate6_Crypto_Currency_Tracker/Network.h index 976c27ce..543adc82 100644 --- a/examples/Inkplate6/Projects/Inkplate6_Crypto_Currency_Tracker/Network.h +++ b/examples/Inkplate6/Projects/Inkplate6_Crypto_Currency_Tracker/Network.h @@ -41,8 +41,6 @@ class Network bool getData(double *data); private: - // Functions called from within our class - void setTime(); }; #endif diff --git a/examples/Inkplate6/Projects/Inkplate6_Daily_Weather_Station/Inkplate6_Daily_Weather_Station.ino b/examples/Inkplate6/Projects/Inkplate6_Daily_Weather_Station/Inkplate6_Daily_Weather_Station.ino index e3e4063b..f32f52a9 100644 --- a/examples/Inkplate6/Projects/Inkplate6_Daily_Weather_Station/Inkplate6_Daily_Weather_Station.ino +++ b/examples/Inkplate6/Projects/Inkplate6_Daily_Weather_Station/Inkplate6_Daily_Weather_Station.ino @@ -64,6 +64,7 @@ char apiKey[] = ""; // Delay between API calls, about 1000 per month, which is the free tier limit #define DELAY_MS 267800L +#define DELAY_WIFI_RETRY_SECONDS 5 // Inkplate object Inkplate display(INKPLATE_1BIT); @@ -121,6 +122,7 @@ void drawCurrent(); void drawTemps(); void drawCity(); void drawTime(); +void setTime(); void setup() { @@ -129,7 +131,29 @@ void setup() display.begin(); // Calling our begin from network.h file - network.begin(ssid, pass); + // Try connecting to a WiFi network. + // Parameters are network SSID, password, timeout in seconds and whether to print to serial. + // If the Inkplate isn't able to connect to a network stop further code execution and print an error message. + if (!display.connectWiFi(ssid, pass, WIFI_TIMEOUT, true)) + { + //Can't connect to netowrk + // Clear display for the error message + display.clearDisplay(); + // Set the font size; + display.setTextSize(3); + // Set the cursor positions and print the text. + display.setCursor((display.width() / 2) - 200, display.height() / 2); + display.print(F("Unable to connect to ")); + display.println(F(ssid)); + display.setCursor((display.width() / 2) - 200, (display.height() / 2) + 30); + display.println(F("Please check ssid and pass!")); + // Display the error message on the Inkplate and go to deep sleep + display.display(); + esp_sleep_enable_timer_wakeup(100L * DELAY_WIFI_RETRY_SECONDS); + (void)esp_deep_sleep_start(); + } + + setTime(); // Get all relevant data, see Network.cpp for info if (refreshes % fullRefresh == 0) @@ -166,6 +190,27 @@ void loop() // nothing here } +// Function for getting time from NTP server +void setTime() +{ + // Structure used to hold time information + struct tm timeInfo; + display.getNTPDateTime(&timeInfo); + time_t nowSec; + // Fetch current time in epoch format and store it + display.getNTPEpoch(&nowSec); + // This loop ensures that the NTP time fetched is valid and beyond a certain threshold + while (nowSec < 8 * 3600 * 2) + { + delay(500); + yield(); + display.getNTPEpoch(&nowSec); + } + gmtime_r(&nowSec, &timeInfo); + Serial.print(F("Current time: ")); + Serial.print(asctime(&timeInfo)); +} + // Function for drawing weather info void drawWeather() { diff --git a/examples/Inkplate6/Projects/Inkplate6_Daily_Weather_Station/Network.cpp b/examples/Inkplate6/Projects/Inkplate6_Daily_Weather_Station/Network.cpp index fc0e840c..2180328a 100644 --- a/examples/Inkplate6/Projects/Inkplate6_Daily_Weather_Station/Network.cpp +++ b/examples/Inkplate6/Projects/Inkplate6_Daily_Weather_Station/Network.cpp @@ -44,8 +44,6 @@ void Network::begin(char *ssid, char *pass) } Serial.println(F(" connected")); - // Find internet time - setTime(); } // Gets time from ntp server @@ -227,31 +225,7 @@ bool Network::getData(char *lat, char *lon, char *apiKey, char *city, char *temp return 1; } -void Network::setTime() -{ - // Used for setting correct time - configTime(0, 0, "pool.ntp.org", "time.nist.gov"); - - Serial.print(F("Waiting for NTP time sync: ")); - time_t nowSecs = time(nullptr); - while (nowSecs < 8 * 3600 * 2) - { - // Print a dot every half a second while time is not set - delay(500); - Serial.print(F(".")); - yield(); - nowSecs = time(nullptr); - } - Serial.println(); - - // Used to store time info - struct tm timeinfo; - gmtime_r(&nowSecs, &timeinfo); - - Serial.print(F("Current time: ")); - Serial.print(asctime(&timeinfo)); -} void Network::getDays(char *day, char *day1, char *day2, char *day3) { diff --git a/examples/Inkplate6/Projects/Inkplate6_Daily_Weather_Station/Network.h b/examples/Inkplate6/Projects/Inkplate6_Daily_Weather_Station/Network.h index df05ca18..e9c17ae6 100644 --- a/examples/Inkplate6/Projects/Inkplate6_Daily_Weather_Station/Network.h +++ b/examples/Inkplate6/Projects/Inkplate6_Daily_Weather_Station/Network.h @@ -49,8 +49,6 @@ class Network int location = -1; private: - // Functions called from within our class - void setTime(); int timeZone = 0; }; diff --git a/examples/Inkplate6/Projects/Inkplate6_Google_Calendar/Inkplate6_Google_Calendar.ino b/examples/Inkplate6/Projects/Inkplate6_Google_Calendar/Inkplate6_Google_Calendar.ino index 8307b1c1..a6ad8630 100644 --- a/examples/Inkplate6/Projects/Inkplate6_Google_Calendar/Inkplate6_Google_Calendar.ino +++ b/examples/Inkplate6/Projects/Inkplate6_Google_Calendar/Inkplate6_Google_Calendar.ino @@ -55,6 +55,7 @@ int timeZone = 2; // Delay between API calls #define DELAY_MS 4 * 60000 +#define DELAY_WIFI_RETRY_SECONDS 5 // Variables to keep count of when to get new data, and when to just update time RTC_DATA_ATTR unsigned int refreshes = 0; @@ -92,6 +93,7 @@ void getToFrom(char *dst, char *from, char *to, int *day, int *timeStamp); bool drawEvent(entry *event, int day, int beginY, int maxHeigth, int *heigthNeeded); int cmp(const void *a, const void *b); void drawData(); +void setTime(); void setup() { @@ -106,8 +108,30 @@ void setup() display.setTextWrap(false); display.setTextColor(0, 7); - delay(5000); - network.begin(ssid, pass); + // Connect Inkplate to the WiFi network + // Try connecting to a WiFi network. + // Parameters are network SSID, password, timeout in seconds and whether to print to serial. + // If the Inkplate isn't able to connect to a network stop further code execution and print an error message. + if (!display.connectWiFi(ssid, pass, WIFI_TIMEOUT, true)) + { + //Can't connect to netowrk + // Clear display for the error message + display.clearDisplay(); + // Set the font size; + display.setTextSize(3); + // Set the cursor positions and print the text. + display.setCursor((display.width() / 2) - 200, display.height() / 2); + display.print(F("Unable to connect to ")); + display.println(F(ssid)); + display.setCursor((display.width() / 2) - 200, (display.height() / 2) + 30); + display.println(F("Please check SSID and PASS!")); + // Display the error message on the Inkplate and go to deep sleep + display.display(); + esp_sleep_enable_timer_wakeup(1000L * DELAY_WIFI_RETRY_SECONDS); + (void)esp_deep_sleep_start(); + } + + setTime(); // Keep trying to get data if it fails the first time Serial.println("Getting data... "); @@ -141,6 +165,26 @@ void loop() // Never here } +void setTime() +{ + // Structure used to hold time information + struct tm timeInfo; + // Fetch current time from an NTP server and store it + time_t nowSec; + // Fetch current time in epoch format and store it + display.getNTPEpoch(&nowSec); + // This loop ensures that the NTP time fetched is valid and beyond a certain threshold + while(nowSec < 8 * 3600 * 2) + { + delay(500); + yield(); + display.getNTPEpoch(&nowSec); + } + gmtime_r(&nowSec, &timeInfo); + Serial.print(F("Current time: ")); + Serial.print(asctime(&timeInfo)); +} + // Function for drawing calendar info void drawInfo() { @@ -457,6 +501,10 @@ void drawData() &entries[entriesNum].timeStamp); } ++entriesNum; + if(entriesNum == 128) + { + break; + } } // Sort entries by time diff --git a/examples/Inkplate6/Projects/Inkplate6_Google_Calendar/Network.cpp b/examples/Inkplate6/Projects/Inkplate6_Google_Calendar/Network.cpp index 87a66fa1..66b16791 100644 --- a/examples/Inkplate6/Projects/Inkplate6_Google_Calendar/Network.cpp +++ b/examples/Inkplate6/Projects/Inkplate6_Google_Calendar/Network.cpp @@ -43,8 +43,6 @@ void Network::begin(char *ssid, char *pass) } Serial.println(F(" connected")); - // Find and print internet time, the timezone will be added later - setTime(); } // Gets time from ntp server @@ -141,29 +139,3 @@ bool Network::getData(char *calendarURL, char *data) return !f; } - -// Find internet time -void Network::setTime() -{ - // Used for setting correct time - configTime(0, 0, "pool.ntp.org", "time.nist.gov"); - - Serial.print(F("Waiting for NTP time sync: ")); - time_t nowSecs = time(nullptr); - while (nowSecs < 8 * 3600 * 2) - { - delay(500); - Serial.print(F(".")); - yield(); - nowSecs = time(nullptr); - } - Serial.println(); - - // Used to store time info - struct tm timeinfo; - gmtime_r(&nowSecs, &timeinfo); - - // Print the current time without adding a timezone - Serial.print(F("Current time: ")); - Serial.print(asctime(&timeinfo)); -} diff --git a/examples/Inkplate6/Projects/Inkplate6_Google_Calendar/Network.h b/examples/Inkplate6/Projects/Inkplate6_Google_Calendar/Network.h index 5449b75f..ffadefcb 100644 --- a/examples/Inkplate6/Projects/Inkplate6_Google_Calendar/Network.h +++ b/examples/Inkplate6/Projects/Inkplate6_Google_Calendar/Network.h @@ -35,8 +35,6 @@ class Network bool getData(char *calendarURL, char *data); private: - // Functions called from within our class - void setTime(); }; #endif diff --git a/examples/Inkplate6/Projects/Inkplate6_Hourly_Weather_Station/Inkplate6_Hourly_Weather_Station.ino b/examples/Inkplate6/Projects/Inkplate6_Hourly_Weather_Station/Inkplate6_Hourly_Weather_Station.ino index a7662869..569c9554 100644 --- a/examples/Inkplate6/Projects/Inkplate6_Hourly_Weather_Station/Inkplate6_Hourly_Weather_Station.ino +++ b/examples/Inkplate6/Projects/Inkplate6_Hourly_Weather_Station/Inkplate6_Hourly_Weather_Station.ino @@ -63,6 +63,7 @@ char apiKey[] = ""; // Delay between API calls, about 1000 per month, which is the free tier limit #define DELAY_MS 267800L +#define DELAY_WIFI_RETRY_SECONDS 5 // Inkplate object Inkplate display(INKPLATE_1BIT); @@ -125,6 +126,7 @@ void drawCurrent(); void drawTemps(); void drawCity(); void drawTime(); +void setTime(); void setup() { @@ -132,8 +134,30 @@ void setup() Serial.begin(115200); display.begin(); - // Calling our begin from network.h file - network.begin(city); + // Connect Inkplate to the WiFi network + // Try connecting to a WiFi network. + // Parameters are network SSID, password, timeout in seconds and whether to print to serial. + // If the Inkplate isn't able to connect to a network stop further code execution and print an error message. + if (!display.connectWiFi(ssid, pass, WIFI_TIMEOUT, true)) + { + //Can't connect to netowrk + // Clear display for the error message + display.clearDisplay(); + // Set the font size; + display.setTextSize(3); + // Set the cursor positions and print the text. + display.setCursor((display.width() / 2) - 200, display.height() / 2); + display.print(F("Unable to connect to ")); + display.println(F(ssid)); + display.setCursor((display.width() / 2) - 200, (display.height() / 2) + 30); + display.println(F("Please check SSID and PASS!")); + // Display the error message on the Inkplate and go to deep sleep + display.display(); + esp_sleep_enable_timer_wakeup(1000L * DELAY_WIFI_RETRY_SECONDS); + (void)esp_deep_sleep_start(); + } + + setTime(); // Get all relevant data, see Network.cpp for info network.getTime(currentTime); @@ -174,6 +198,26 @@ void loop() // nothing here } +// Function for getting time from NTP server +void setTime() +{ + // Structure used to hold time information + struct tm timeInfo; + time_t nowSec; + // Fetch current time in epoch format and store it + display.getNTPEpoch(&nowSec); + // This loop ensures that the NTP time fetched is valid and beyond a certain threshold + while (nowSec < 8 * 3600 * 2) + { + delay(500); + yield(); + display.getNTPEpoch(&nowSec); + } + gmtime_r(&nowSec, &timeInfo); + Serial.print(F("Current time: ")); + Serial.print(asctime(&timeInfo)); +} + // Function for drawing weather info void drawWeather() { diff --git a/examples/Inkplate6/Projects/Inkplate6_Hourly_Weather_Station/Network.cpp b/examples/Inkplate6/Projects/Inkplate6_Hourly_Weather_Station/Network.cpp index b77bb0f8..b3a7f43a 100644 --- a/examples/Inkplate6/Projects/Inkplate6_Hourly_Weather_Station/Network.cpp +++ b/examples/Inkplate6/Projects/Inkplate6_Hourly_Weather_Station/Network.cpp @@ -53,9 +53,6 @@ void Network::begin(char *city) } Serial.println(F(" connected")); - // Find internet time - setTime(); - // reduce power by making WiFi module sleep WiFi.setSleep(1); } @@ -227,32 +224,6 @@ bool Network::getData(char *city, char *temp1, char *temp2, char *temp3, char *t return !f; } -void Network::setTime() -{ - // Used for setting correct time - configTime(0, 0, "pool.ntp.org", "time.nist.gov"); - - Serial.print(F("Waiting for NTP time sync: ")); - time_t nowSecs = time(nullptr); - while (nowSecs < 8 * 3600 * 2) - { - // Print a dot every half a second while time is not set - delay(500); - Serial.print(F(".")); - yield(); - nowSecs = time(nullptr); - } - - Serial.println(); - - // Used to store time info - struct tm timeinfo; - gmtime_r(&nowSecs, &timeinfo); - - Serial.print(F("Current time: ")); - Serial.print(asctime(&timeinfo)); -} - void Network::getHours(char *hour1, char *hour2, char *hour3, char *hour4) { // Format hours info diff --git a/examples/Inkplate6/Projects/Inkplate6_Hourly_Weather_Station/Network.h b/examples/Inkplate6/Projects/Inkplate6_Hourly_Weather_Station/Network.h index 09ec7b79..ac0c179f 100644 --- a/examples/Inkplate6/Projects/Inkplate6_Hourly_Weather_Station/Network.h +++ b/examples/Inkplate6/Projects/Inkplate6_Hourly_Weather_Station/Network.h @@ -57,8 +57,6 @@ class Network time_t dataEpoch = 0; private: - // Functions called from within our class - void setTime(); }; #endif diff --git a/examples/Inkplate6/Projects/Inkplate6_News_API/Inkplate6_News_API.ino b/examples/Inkplate6/Projects/Inkplate6_News_API/Inkplate6_News_API.ino index 1fd93dce..78cea491 100644 --- a/examples/Inkplate6/Projects/Inkplate6_News_API/Inkplate6_News_API.ino +++ b/examples/Inkplate6/Projects/Inkplate6_News_API/Inkplate6_News_API.ino @@ -53,6 +53,7 @@ Inkplate display(INKPLATE_1BIT); // Delay between API calls in miliseconds (first 60 represents minutes so you can change to your need) #define DELAY_MS (uint32_t)60 * 60 * 1000 +#define DELAY_WIFI_RETRY_SECONDS 5 // Variable for counting partial refreshes RTC_DATA_ATTR unsigned refreshes = 0; @@ -60,6 +61,8 @@ RTC_DATA_ATTR unsigned refreshes = 0; // Constant to determine when to full update const int fullRefresh = 20; +void setTime(); + void setup() { // Begin serial communitcation, sed for debugging @@ -68,8 +71,30 @@ void setup() display.begin(); display.setTextWrap(false); - // Our begin function - network.begin(); + // Connect Inkplate to the WiFi network + // Try connecting to a WiFi network. + // Parameters are network SSID, password, timeout in seconds and whether to print to serial. + // If the Inkplate isn't able to connect to a network stop further code execution and print an error message. + if (!display.connectWiFi(ssid, pass, WIFI_TIMEOUT, true)) + { + //Can't connect to netowrk + // Clear display for the error message + display.clearDisplay(); + // Set the font size; + display.setTextSize(3); + // Set the cursor positions and print the text. + display.setCursor((display.width() / 2) - 200, display.height() / 2); + display.print(F("Unable to connect to ")); + display.println(F(ssid)); + display.setCursor((display.width() / 2) - 200, (display.height() / 2) + 30); + display.println(F("Please check SSID and PASS!")); + // Display the error message on the Inkplate and go to deep sleep + display.display(); + esp_sleep_enable_timer_wakeup(1000L * DELAY_WIFI_RETRY_SECONDS); + (void)esp_deep_sleep_start(); + } + + setTime(); struct news *entities; @@ -150,6 +175,26 @@ void drawNews(struct news *entities) } +// Function for getting time from NTP server +void setTime() +{ + // Structure used to hold time information + struct tm timeInfo; + time_t nowSec; + // Fetch current time in epoch format and store it + display.getNTPEpoch(&nowSec); + // This loop ensures that the NTP time fetched is valid and beyond a certain threshold + while (nowSec < 8 * 3600 * 2) + { + delay(500); + yield(); + nowSec = time(nullptr); + } + gmtime_r(&nowSec, &timeInfo); + Serial.print(F("Current time: ")); + Serial.print(asctime(&timeInfo)); +} + void loop() { // Never here diff --git a/examples/Inkplate6/Projects/Inkplate6_News_API/Network.cpp b/examples/Inkplate6/Projects/Inkplate6_News_API/Network.cpp index a1e810dc..9d15dc46 100644 --- a/examples/Inkplate6/Projects/Inkplate6_News_API/Network.cpp +++ b/examples/Inkplate6/Projects/Inkplate6_News_API/Network.cpp @@ -61,8 +61,6 @@ void Network::begin() } Serial.println(F(" connected")); - // Find internet time - setTime(); } // Gets time from ntp server @@ -212,28 +210,3 @@ struct news* Network::getData() return ent; } -// Function for initial time setting ovet the ntp server -void Network::setTime() -{ - // Used for setting correct time - configTime(0, 0, "pool.ntp.org", "time.nist.gov"); - - Serial.print(F("Waiting for NTP time sync: ")); - time_t nowSecs = time(nullptr); - while (nowSecs < 8 * 3600 * 2) - { - delay(500); - Serial.print(F(".")); - yield(); - nowSecs = time(nullptr); - } - - Serial.println(); - - // Used to store time info - struct tm timeinfo; - gmtime_r(&nowSecs, &timeinfo); - - Serial.print(F("Current time: ")); - Serial.print(asctime(&timeinfo)); -} diff --git a/examples/Inkplate6/Projects/Inkplate6_News_API/Network.h b/examples/Inkplate6/Projects/Inkplate6_News_API/Network.h index c6fcba3b..e269e15d 100644 --- a/examples/Inkplate6/Projects/Inkplate6_News_API/Network.h +++ b/examples/Inkplate6/Projects/Inkplate6_News_API/Network.h @@ -51,9 +51,6 @@ class Network void getTime(char *timeStr); struct news* getData(); - private: - // Functions called from within our class - void setTime(); }; #endif diff --git a/examples/Inkplate6/Projects/Inkplate6_OpenWeather_Station/Inkplate6_OpenWeather_Station.ino b/examples/Inkplate6/Projects/Inkplate6_OpenWeather_Station/Inkplate6_OpenWeather_Station.ino index 94dfff72..a423ee8d 100644 --- a/examples/Inkplate6/Projects/Inkplate6_OpenWeather_Station/Inkplate6_OpenWeather_Station.ino +++ b/examples/Inkplate6/Projects/Inkplate6_OpenWeather_Station/Inkplate6_OpenWeather_Station.ino @@ -40,6 +40,9 @@ #include "OpenWeatherOneCall.h" #define SSID "" #define PASS "" +#define DELAY_WIFI_RETRY_SECONDS 5 +// Variable to store WiFi connection timeout +int timeoutSeconds = 100; // Openweather API key /** @@ -52,9 +55,9 @@ * */ char *APIKEY = ""; +#define DELAY_API_RETRY_SECONDS 5 // Also, declare the function to check if the API key is valid bool checkIfAPIKeyIsValid(char *APIKEY); - float myLatitude = 45.560001; // I got this from Wikipedia float myLongitude = 18.675880; @@ -124,41 +127,12 @@ char Output[200] = {0}; OpenWeatherOneCall OWOC; // Invoke OpenWeather Library time_t t = now(); -void connectWifi() -{ - - int ConnectCount = 20; - - if (WiFi.status() != WL_CONNECTED) - { - while (WiFi.status() != WL_CONNECTED) - { - if (ConnectCount++ == 20) - { - Serial.println("Connect WiFi"); - WiFi.begin(SSID, PASS); - Serial.print("Connecting."); - ConnectCount = 0; - } - Serial.print("."); - delay(1000); - } - Serial.print("\nConnected to: "); - Serial.println(SSID); - Serial.println("IP address: "); - Serial.println(WiFi.localIP()); - Serial.println("Connected WiFi"); - } -} //======================== END WIFI CONNECT ======================= - void GetCurrentWeather() { //================================= // Get the Weather Forecast //================================= - connectWifi(); - Serial.println("Getting weather"); OWOC.parseWeather(APIKEY, NULL, myLatitude, myLongitude, metric, NULL); setTime(OWOC.current.dt); @@ -252,51 +226,72 @@ void setup() } Serial.println("Serial Monitor Initialized"); + // display.begin(); - connectWifi(); + // Initial cleaning of buffer and physical screen + display.clearDisplay(); + display.display(); + + // Welcome screen + display.setCursor(215, 400); + display.setTextSize(3); + display.print(F("Welcome to Wol Inkplate 6 weather example!")); + display.display(); + Serial.println("Welcome to Wol Inkplate 6 weather example!"); + display.display(); + + // Try connecting to a WiFi network. + // Parameters are network SSID, password, timeout in seconds and whether to print to serial. + // If the Inkplate isn't able to connect to a network stop further code execution and print an error messddage. + if (!display.connectWiFi(SSID, PASS, timeoutSeconds, true)) + { + //Can't connect to netowrk + // Clear display for the error message + display.clearDisplay(); + // Set the font size; + display.setTextSize(3); + // Set the cursor positions and print the text. + display.setCursor((display.width() / 2) - 200, display.height() / 2); + display.print(F("Unable to connect to ")); + display.println(F(SSID)); + display.setCursor((display.width() / 2) - 200, (display.height() / 2) + 30); + display.println(F("Please check SSID and PASS!")); + // Display the error message on the Inkplate and go to deep sleep + display.display(); + esp_sleep_enable_timer_wakeup(1000L * DELAY_WIFI_RETRY_SECONDS); + (void)esp_deep_sleep_start(); + } // Check if we have a valid API key: Serial.println("Checking if API key is valid..."); - if(!checkIfAPIKeyIsValid(APIKEY)) + if (!checkIfAPIKeyIsValid(APIKEY)) { // If we don't, notify the user Serial.println("API key is invalid!"); display.clearDisplay(); - display.setCursor(0,0); + display.setCursor(0, 0); display.setTextSize(2); display.println("Can't get data from OpenWeatherMaps! Check your API key!"); display.println("Only older API keys for OneCall 2.5 work in free tier."); display.println("See the code comments the example for more info."); display.display(); - while(1) - { - delay(100); - } + esp_sleep_enable_timer_wakeup(1000L * DELAY_API_RETRY_SECONDS); + (void)esp_deep_sleep_start(); } Serial.println("API key is valid!"); -} -void loop() -{ // Clear display t = now(); - if ((minute(t) % 30) == 0) // Also returns 0 when time isn't set { GetCurrentWeather(); - Serial.println("got weather data"); display.clearDisplay(); drawForecast(); - Serial.println("draw forecast"); drawCurrent(); - Serial.println("draw current"); drawHourly(); - Serial.println("draw hourly"); drawTime(); - Serial.println("draw time"); drawMoon(); - Serial.println("draw moon"); display.display(); } else @@ -311,9 +306,13 @@ void loop() } // Go to sleep before checking again - esp_sleep_enable_timer_wakeup(1000L * DELAY_MS); - (void)esp_light_sleep_start(); ++refreshes; + esp_sleep_enable_timer_wakeup(1000L * DELAY_MS); + (void)esp_deep_sleep_start(); +} + +void loop() +{ } // Function for drawing weather info diff --git a/examples/Inkplate6/Projects/Inkplate6_Quotables/Inkplate6_Quotables.ino b/examples/Inkplate6/Projects/Inkplate6_Quotables/Inkplate6_Quotables.ino index 2e05011d..1069315f 100644 --- a/examples/Inkplate6/Projects/Inkplate6_Quotables/Inkplate6_Quotables.ino +++ b/examples/Inkplate6/Projects/Inkplate6_Quotables/Inkplate6_Quotables.ino @@ -54,7 +54,7 @@ Inkplate display(INKPLATE_1BIT); // Delay between API calls in seconds, 300 seconds is 5 minutes #define DELAY_S 300 - +#define DELAY_WIFI_RETRY_SECONDS 5 // Our functions declared below setup and loop void drawAll(); @@ -75,8 +75,27 @@ void setup() display.clearDisplay(); display.display(); - // Our begin function - network.begin(); + // Try connecting to a WiFi network. + // Parameters are network SSID, password, timeout in seconds and whether to print to serial. + // If the Inkplate isn't able to connect to a network stop further code execution and print an error message. + if (!display.connectWiFi(ssid, pass, WIFI_TIMEOUT, true)) + { + //Can't connect to netowrk + // Clear display for the error message + display.clearDisplay(); + // Set the font size; + display.setTextSize(3); + // Set the cursor positions and print the text. + display.setCursor((display.width() / 2) - 200, display.height() / 2); + display.print(F("Unable to connect to ")); + display.println(F(ssid)); + display.setCursor((display.width() / 2) - 200, (display.height() / 2) + 30); + display.println(F("Please check SSID and PASS!")); + // Display the error message on the Inkplate and go to deep sleep + display.display(); + esp_sleep_enable_timer_wakeup(1000L * DELAY_WIFI_RETRY_SECONDS); + (void)esp_deep_sleep_start(); + } Serial.print("Retrying retriving data"); while (!network.getData(quote, author)) diff --git a/examples/Inkplate6/Projects/Inkplate6_Youtube_Subscriber_Counter/Inkplate6_Youtube_Subscriber_Counter.ino b/examples/Inkplate6/Projects/Inkplate6_Youtube_Subscriber_Counter/Inkplate6_Youtube_Subscriber_Counter.ino index 6ba510da..07f2b3f4 100644 --- a/examples/Inkplate6/Projects/Inkplate6_Youtube_Subscriber_Counter/Inkplate6_Youtube_Subscriber_Counter.ino +++ b/examples/Inkplate6/Projects/Inkplate6_Youtube_Subscriber_Counter/Inkplate6_Youtube_Subscriber_Counter.ino @@ -42,6 +42,7 @@ // Delay between API calls in miliseconds (10 minutes) #define DELAY_MS 10 * 60 * 1000 +#define DELAY_WIFI_RETRY_SECONDS 5 // Create object with all networking functions Network network; @@ -124,8 +125,27 @@ void setup() delay(1000); } - // Our begin function - network.begin(ssid, pass); + // Try connecting to a WiFi network. + // Parameters are network SSID, password, timeout in seconds and whether to print to serial. + // If the Inkplate isn't able to connect to a network stop further code execution and print an error message. + if (!display.connectWiFi(ssid, pass, WIFI_TIMEOUT, true)) + { + //Can't connect to netowrk + // Clear display for the error message + display.clearDisplay(); + // Set the font size; + display.setTextSize(3); + // Set the cursor positions and print the text. + display.setCursor((display.width() / 2) - 200, display.height() / 2); + display.print(F("Unable to connect to ")); + display.println(F(ssid)); + display.setCursor((display.width() / 2) - 200, (display.height() / 2) + 30); + display.println(F("Please check SSID and PASS!")); + // Display the error message on the Inkplate and go to deep sleep + display.display(); + esp_sleep_enable_timer_wakeup(1000L * DELAY_WIFI_RETRY_SECONDS); + (void)esp_deep_sleep_start(); + } while (!network.getData(&channel, channel_id, api_key, &display)) { diff --git a/examples/Inkplate6COLOR/Projects/Inkplate6COLOR_Crypto_Currency_Tracker/Inkplate6COLOR_Crypto_Currency_Tracker.ino b/examples/Inkplate6COLOR/Projects/Inkplate6COLOR_Crypto_Currency_Tracker/Inkplate6COLOR_Crypto_Currency_Tracker.ino index 6b3d98ca..d46cedb0 100644 --- a/examples/Inkplate6COLOR/Projects/Inkplate6COLOR_Crypto_Currency_Tracker/Inkplate6COLOR_Crypto_Currency_Tracker.ino +++ b/examples/Inkplate6COLOR/Projects/Inkplate6COLOR_Crypto_Currency_Tracker/Inkplate6COLOR_Crypto_Currency_Tracker.ino @@ -35,7 +35,7 @@ char pass[] = ""; // Delay between API calls in miliseconds #define DELAY_MS 3 * 60 * 1000 - +#define DELAY_WIFI_RETRY_SECONDS 5 // OPTIONAL: // change to a different currency char currency[] = "bitcoin"; @@ -113,6 +113,7 @@ textElement elements[] = { // Our functions declared below setup and loop void drawGraph(); void drawAll(); +void setTime(); void setup() { @@ -125,8 +126,30 @@ void setup() display.setTextWrap(false); display.setTextColor(0, 7); - // Our begin function - network.begin(); + // Try connecting to a WiFi network. + // Parameters are network SSID, password, timeout in seconds and whether to print to serial. + // If the Inkplate isn't able to connect to a network stop further code execution and print an error message. + if (!display.connectWiFi(ssid, pass, WIFI_TIMEOUT, true)) + { + // Can't connect to netowrk + // Clear display for the error message + display.clearDisplay(); + // Set the font size; + display.setTextSize(3); + // Set the cursor positions and print the text. + display.setCursor((display.width() / 2) - 200, display.height() / 2); + display.print(F("Unable to connect to ")); + display.println(F(ssid)); + display.setCursor((display.width() / 2) - 200, (display.height() / 2) + 30); + display.println(F("Please check ssid and pass!")); + // Display the error message on the Inkplate and go to deep sleep + display.display(); + esp_sleep_enable_timer_wakeup(1000L * DELAY_WIFI_RETRY_SECONDS); + (void)esp_deep_sleep_start(); + } + + // After connecting to WiFi we need to get internet time from NTP server + setTime(); } void loop() @@ -149,6 +172,26 @@ void loop() esp_deep_sleep_start(); } +// Function for getting time from NTP server +void setTime() +{ + // Structure used to hold time information + struct tm timeInfo; + time_t nowSec; + // Fetch current time in epoch format and store it + display.getNTPEpoch(&nowSec); + // This loop ensures that the NTP time fetched is valid and beyond a certain threshold + while (nowSec < 8 * 3600 * 2) + { + delay(500); + yield(); + display.getNTPEpoch(&nowSec); + } + gmtime_r(&nowSec, &timeInfo); + Serial.print(F("Current time: ")); + Serial.print(asctime(&timeInfo)); +} + // Function to draw our graph void drawGraph() { diff --git a/examples/Inkplate6COLOR/Projects/Inkplate6COLOR_Crypto_Currency_Tracker/Network.cpp b/examples/Inkplate6COLOR/Projects/Inkplate6COLOR_Crypto_Currency_Tracker/Network.cpp index dbad45e6..91ea19f5 100644 --- a/examples/Inkplate6COLOR/Projects/Inkplate6COLOR_Crypto_Currency_Tracker/Network.cpp +++ b/examples/Inkplate6COLOR/Projects/Inkplate6COLOR_Crypto_Currency_Tracker/Network.cpp @@ -58,9 +58,6 @@ void Network::begin() } } Serial.println(F(" connected")); - - // Find internet time - setTime(); } // Gets time from ntp server @@ -189,29 +186,3 @@ bool Network::getData(double *data) return !f; } - -// Function for initial time setting ovet the ntp server -void Network::setTime() -{ - // Used for setting correct time - configTime(0, 0, "pool.ntp.org", "time.nist.gov"); - - Serial.print(F("Waiting for NTP time sync: ")); - time_t nowSecs = time(nullptr); - while (nowSecs < 8 * 3600 * 2) - { - delay(500); - Serial.print(F(".")); - yield(); - nowSecs = time(nullptr); - } - - Serial.println(); - - // Used to store time info - struct tm timeinfo; - gmtime_r(&nowSecs, &timeinfo); - - Serial.print(F("Current time: ")); - Serial.print(asctime(&timeinfo)); -} diff --git a/examples/Inkplate6COLOR/Projects/Inkplate6COLOR_Crypto_Currency_Tracker/Network.h b/examples/Inkplate6COLOR/Projects/Inkplate6COLOR_Crypto_Currency_Tracker/Network.h index fd71a7ee..23acee48 100644 --- a/examples/Inkplate6COLOR/Projects/Inkplate6COLOR_Crypto_Currency_Tracker/Network.h +++ b/examples/Inkplate6COLOR/Projects/Inkplate6COLOR_Crypto_Currency_Tracker/Network.h @@ -41,8 +41,6 @@ class Network bool getData(double *data); private: - // Functions called from within our class - void setTime(); }; #endif diff --git a/examples/Inkplate6COLOR/Projects/Inkplate6COLOR_Daily_Weather_Station/Inkplate6COLOR_Daily_Weather_Station.ino b/examples/Inkplate6COLOR/Projects/Inkplate6COLOR_Daily_Weather_Station/Inkplate6COLOR_Daily_Weather_Station.ino index 51fdc6a9..b3ebb4ab 100644 --- a/examples/Inkplate6COLOR/Projects/Inkplate6COLOR_Daily_Weather_Station/Inkplate6COLOR_Daily_Weather_Station.ino +++ b/examples/Inkplate6COLOR/Projects/Inkplate6COLOR_Daily_Weather_Station/Inkplate6COLOR_Daily_Weather_Station.ino @@ -57,6 +57,7 @@ char apiKey[] = ""; // Delay between API calls #define DELAY_MS (uint32_t)10 * 60 * 1000 // 10 minute time +#define DELAY_WIFI_RETRY_SECONDS 5 // Inkplate object Inkplate display; @@ -107,6 +108,7 @@ void drawCurrent(); void drawTemps(); void drawCity(); void drawTime(); +void setTime(); void setup() { @@ -114,8 +116,29 @@ void setup() Serial.begin(115200); display.begin(); - // Calling our begin from network.h file - network.begin(ssid, pass); + // Try connecting to a WiFi network. + // Parameters are network SSID, password, timeout in seconds and whether to print to serial. + // If the Inkplate isn't able to connect to a network stop further code execution and print an error message. + if (!display.connectWiFi(ssid, pass, WIFI_TIMEOUT, true)) + { + //Can't connect to netowrk + // Clear display for the error message + display.clearDisplay(); + // Set the font size; + display.setTextSize(3); + // Set the cursor positions and print the text. + display.setCursor((display.width() / 2) - 200, display.height() / 2); + display.print(F("Unable to connect to ")); + display.println(F(ssid)); + display.setCursor((display.width() / 2) - 200, (display.height() / 2) + 30); + display.println(F("Please check ssid and pass!")); + // Display the error message on the Inkplate and go to deep sleep + display.display(); + esp_sleep_enable_timer_wakeup(100L * DELAY_WIFI_RETRY_SECONDS); + (void)esp_deep_sleep_start(); + } + + setTime(); // Get all relevant data, see Network.cpp for info Serial.print("Retrying fetching data"); @@ -150,6 +173,27 @@ void loop() // time. loop() must be empty! } +// Function for getting time from NTP server +void setTime() +{ + // Structure used to hold time information + struct tm timeInfo; + display.getNTPDateTime(&timeInfo); + time_t nowSec; + // Fetch current time in epoch format and store it + display.getNTPEpoch(&nowSec); + // This loop ensures that the NTP time fetched is valid and beyond a certain threshold + while (nowSec < 8 * 3600 * 2) + { + delay(500); + yield(); + display.getNTPEpoch(&nowSec); + } + gmtime_r(&nowSec, &timeInfo); + Serial.print(F("Current time: ")); + Serial.print(asctime(&timeInfo)); +} + // Function for drawing weather info void drawWeather() { diff --git a/examples/Inkplate6COLOR/Projects/Inkplate6COLOR_Daily_Weather_Station/Network.cpp b/examples/Inkplate6COLOR/Projects/Inkplate6COLOR_Daily_Weather_Station/Network.cpp index fc0e840c..76ee3ed5 100644 --- a/examples/Inkplate6COLOR/Projects/Inkplate6COLOR_Daily_Weather_Station/Network.cpp +++ b/examples/Inkplate6COLOR/Projects/Inkplate6COLOR_Daily_Weather_Station/Network.cpp @@ -43,9 +43,6 @@ void Network::begin(char *ssid, char *pass) } } Serial.println(F(" connected")); - - // Find internet time - setTime(); } // Gets time from ntp server @@ -227,32 +224,6 @@ bool Network::getData(char *lat, char *lon, char *apiKey, char *city, char *temp return 1; } -void Network::setTime() -{ - // Used for setting correct time - configTime(0, 0, "pool.ntp.org", "time.nist.gov"); - - Serial.print(F("Waiting for NTP time sync: ")); - time_t nowSecs = time(nullptr); - while (nowSecs < 8 * 3600 * 2) - { - // Print a dot every half a second while time is not set - delay(500); - Serial.print(F(".")); - yield(); - nowSecs = time(nullptr); - } - - Serial.println(); - - // Used to store time info - struct tm timeinfo; - gmtime_r(&nowSecs, &timeinfo); - - Serial.print(F("Current time: ")); - Serial.print(asctime(&timeinfo)); -} - void Network::getDays(char *day, char *day1, char *day2, char *day3) { // Seconds since 1.1.1970. diff --git a/examples/Inkplate6COLOR/Projects/Inkplate6COLOR_Daily_Weather_Station/Network.h b/examples/Inkplate6COLOR/Projects/Inkplate6COLOR_Daily_Weather_Station/Network.h index 7a8de194..4d2ab4e2 100644 --- a/examples/Inkplate6COLOR/Projects/Inkplate6COLOR_Daily_Weather_Station/Network.h +++ b/examples/Inkplate6COLOR/Projects/Inkplate6COLOR_Daily_Weather_Station/Network.h @@ -46,8 +46,6 @@ class Network int location = -1; private: - // Functions called from within our class - void setTime(); int timeZone = 0; }; diff --git a/examples/Inkplate6COLOR/Projects/Inkplate6COLOR_Google_Calendar/Inkplate6COLOR_Google_Calendar.ino b/examples/Inkplate6COLOR/Projects/Inkplate6COLOR_Google_Calendar/Inkplate6COLOR_Google_Calendar.ino index 926e8dc8..4d1f1f49 100644 --- a/examples/Inkplate6COLOR/Projects/Inkplate6COLOR_Google_Calendar/Inkplate6COLOR_Google_Calendar.ino +++ b/examples/Inkplate6COLOR/Projects/Inkplate6COLOR_Google_Calendar/Inkplate6COLOR_Google_Calendar.ino @@ -56,6 +56,7 @@ int timeZone = 2; // Delay between API calls #define DELAY_MS 15 * 60 * 1000 +#define DELAY_WIFI_RETRY_SECONDS 5 // Initiate out Inkplate object Inkplate display; @@ -91,6 +92,7 @@ void getToFrom(char *dst, char *from, char *to, int *day, int *timeStamp); bool drawEvent(entry *event, int day, int beginY, int maxHeigth, int *heigthNeeded); int cmp(const void *a, const void *b); void drawData(); +void setTime(); void setup() { @@ -107,7 +109,29 @@ void setup() display.setTextColor(0, 7); // Connect Inkplate to the WiFi network - network.begin(ssid, pass); + // Try connecting to a WiFi network. + // Parameters are network SSID, password, timeout in seconds and whether to print to serial. + // If the Inkplate isn't able to connect to a network stop further code execution and print an error message. + if (!display.connectWiFi(ssid, pass, WIFI_TIMEOUT, true)) + { + //Can't connect to netowrk + // Clear display for the error message + display.clearDisplay(); + // Set the font size; + display.setTextSize(3); + // Set the cursor positions and print the text. + display.setCursor((display.width() / 2) - 200, display.height() / 2); + display.print(F("Unable to connect to ")); + display.println(F(ssid)); + display.setCursor((display.width() / 2) - 200, (display.height() / 2) + 30); + display.println(F("Please check SSID and PASS!")); + // Display the error message on the Inkplate and go to deep sleep + display.display(); + esp_sleep_enable_timer_wakeup(1000L * DELAY_WIFI_RETRY_SECONDS); + (void)esp_deep_sleep_start(); + } + + setTime(); // Get the data from Google Calendar // Repeat attempts until data is fully downloaded @@ -141,6 +165,27 @@ void loop() // time. loop() must be empty! } +// Function to set time from NTP server +void setTime() +{ + // Structure used to hold time information + struct tm timeInfo; + // Fetch current time from an NTP server and store it + time_t nowSec; + // Fetch current time in epoch format and store it + display.getNTPEpoch(&nowSec); + // This loop ensures that the NTP time fetched is valid and beyond a certain threshold + while(nowSec < 8 * 3600 * 2) + { + delay(500); + yield(); + display.getNTPEpoch(&nowSec); + } + gmtime_r(&nowSec, &timeInfo); + Serial.print(F("Current time: ")); + Serial.print(asctime(&timeInfo)); +} + // Function for drawing calendar info void drawInfo() { @@ -547,6 +592,10 @@ void drawData() &entries[entriesNum].timeStamp); } ++entriesNum; + if(entriesNum == 128) + { + break; + } } // Sort entries by time diff --git a/examples/Inkplate6COLOR/Projects/Inkplate6COLOR_Google_Calendar/Network.cpp b/examples/Inkplate6COLOR/Projects/Inkplate6COLOR_Google_Calendar/Network.cpp index 36bb2c2d..43ad90eb 100644 --- a/examples/Inkplate6COLOR/Projects/Inkplate6COLOR_Google_Calendar/Network.cpp +++ b/examples/Inkplate6COLOR/Projects/Inkplate6COLOR_Google_Calendar/Network.cpp @@ -43,8 +43,6 @@ void Network::begin(char *ssid, char *pass) } Serial.println(F(" connected")); - // Find and print internet time, the timezone will be added later - setTime(); } // Gets time from ntp server @@ -142,29 +140,3 @@ bool Network::getData(char *calendarURL, char *data) return !f; } - -// Find internet time -void Network::setTime() -{ - // Used for setting correct time - configTime(0, 0, "pool.ntp.org", "time.nist.gov"); - - Serial.print(F("Waiting for NTP time sync: ")); - time_t nowSecs = time(nullptr); - while (nowSecs < 8 * 3600 * 2) - { - delay(500); - Serial.print(F(".")); - yield(); - nowSecs = time(nullptr); - } - Serial.println(); - - // Used to store time info - struct tm timeinfo; - gmtime_r(&nowSecs, &timeinfo); - - // Print the current time without adding a timezone - Serial.print(F("Current time: ")); - Serial.print(asctime(&timeinfo)); -} diff --git a/examples/Inkplate6COLOR/Projects/Inkplate6COLOR_Google_Calendar/Network.h b/examples/Inkplate6COLOR/Projects/Inkplate6COLOR_Google_Calendar/Network.h index 90ca03a2..ff149e16 100644 --- a/examples/Inkplate6COLOR/Projects/Inkplate6COLOR_Google_Calendar/Network.h +++ b/examples/Inkplate6COLOR/Projects/Inkplate6COLOR_Google_Calendar/Network.h @@ -35,8 +35,6 @@ class Network bool getData(char *calendarURL, char *data); private: - // Functions called from within our class - void setTime(); }; #endif diff --git a/examples/Inkplate6COLOR/Projects/Inkplate6COLOR_Hourly_Weather_Station/Inkplate6COLOR_Hourly_Weather_Station.ino b/examples/Inkplate6COLOR/Projects/Inkplate6COLOR_Hourly_Weather_Station/Inkplate6COLOR_Hourly_Weather_Station.ino index 9f0a51dc..5dee22ac 100644 --- a/examples/Inkplate6COLOR/Projects/Inkplate6COLOR_Hourly_Weather_Station/Inkplate6COLOR_Hourly_Weather_Station.ino +++ b/examples/Inkplate6COLOR/Projects/Inkplate6COLOR_Hourly_Weather_Station/Inkplate6COLOR_Hourly_Weather_Station.ino @@ -64,6 +64,7 @@ char apiKey[] = ""; // Delay between API calls #define DELAY_MS 15 * 60 * 1000 +#define DELAY_WIFI_RETRY_SECONDS 5 // Inkplate object Inkplate display; @@ -120,6 +121,7 @@ void drawCurrent(); void drawTemps(); void drawCity(); void drawTime(); +void setTime(); void setup() { @@ -127,8 +129,30 @@ void setup() Serial.begin(115200); display.begin(); - // Calling our begin from network.h file - network.begin(city); + // Connect Inkplate to the WiFi network + // Try connecting to a WiFi network. + // Parameters are network SSID, password, timeout in seconds and whether to print to serial. + // If the Inkplate isn't able to connect to a network stop further code execution and print an error message. + if (!display.connectWiFi(ssid, pass, WIFI_TIMEOUT, true)) + { + //Can't connect to netowrk + // Clear display for the error message + display.clearDisplay(); + // Set the font size; + display.setTextSize(3); + // Set the cursor positions and print the text. + display.setCursor((display.width() / 2) - 200, display.height() / 2); + display.print(F("Unable to connect to ")); + display.println(F(ssid)); + display.setCursor((display.width() / 2) - 200, (display.height() / 2) + 30); + display.println(F("Please check SSID and PASS!")); + // Display the error message on the Inkplate and go to deep sleep + display.display(); + esp_sleep_enable_timer_wakeup(1000L * DELAY_WIFI_RETRY_SECONDS); + (void)esp_deep_sleep_start(); + } + + setTime(); // Wait a bit before proceeding delay(5000); @@ -167,6 +191,26 @@ void loop() esp_deep_sleep_start(); } +// Function for getting time from NTP server +void setTime() +{ + // Structure used to hold time information + struct tm timeInfo; + time_t nowSec; + // Fetch current time in epoch format and store it + display.getNTPEpoch(&nowSec); + // This loop ensures that the NTP time fetched is valid and beyond a certain threshold + while (nowSec < 8 * 3600 * 2) + { + delay(500); + yield(); + display.getNTPEpoch(&nowSec); + } + gmtime_r(&nowSec, &timeInfo); + Serial.print(F("Current time: ")); + Serial.print(asctime(&timeInfo)); +} + // Function for drawing weather info void drawWeather() { diff --git a/examples/Inkplate6COLOR/Projects/Inkplate6COLOR_Hourly_Weather_Station/Network.cpp b/examples/Inkplate6COLOR/Projects/Inkplate6COLOR_Hourly_Weather_Station/Network.cpp index ac8836f7..aa52e3ba 100644 --- a/examples/Inkplate6COLOR/Projects/Inkplate6COLOR_Hourly_Weather_Station/Network.cpp +++ b/examples/Inkplate6COLOR/Projects/Inkplate6COLOR_Hourly_Weather_Station/Network.cpp @@ -53,9 +53,6 @@ void Network::begin(char *city) } Serial.println(F(" connected")); - // Find internet time - setTime(); - // reduce power by making WiFi module sleep WiFi.setSleep(1); } @@ -227,32 +224,6 @@ bool Network::getData(char *city, char *temp1, char *temp2, char *temp3, char *t return !f; } -void Network::setTime() -{ - // Used for setting correct time - configTime(0, 0, "pool.ntp.org", "time.nist.gov"); - - Serial.print(F("Waiting for NTP time sync: ")); - time_t nowSecs = time(nullptr); - while (nowSecs < 8 * 3600 * 2) - { - // Print a dot every half a second while time is not set - delay(500); - Serial.print(F(".")); - yield(); - nowSecs = time(nullptr); - } - - Serial.println(); - - // Used to store time info - struct tm timeinfo; - gmtime_r(&nowSecs, &timeinfo); - - Serial.print(F("Current time: ")); - Serial.print(asctime(&timeinfo)); -} - void Network::getHours(char *hour1, char *hour2, char *hour3, char *hour4) { // Format hours info diff --git a/examples/Inkplate6COLOR/Projects/Inkplate6COLOR_Hourly_Weather_Station/Network.h b/examples/Inkplate6COLOR/Projects/Inkplate6COLOR_Hourly_Weather_Station/Network.h index 7d4f916b..f5b5e52b 100644 --- a/examples/Inkplate6COLOR/Projects/Inkplate6COLOR_Hourly_Weather_Station/Network.h +++ b/examples/Inkplate6COLOR/Projects/Inkplate6COLOR_Hourly_Weather_Station/Network.h @@ -57,8 +57,6 @@ class Network time_t dataEpoch = 0; private: - // Functions called from within our class - void setTime(); }; #endif diff --git a/examples/Inkplate6COLOR/Projects/Inkplate6COLOR_News_API/Inkplate6COLOR_News_API.ino b/examples/Inkplate6COLOR/Projects/Inkplate6COLOR_News_API/Inkplate6COLOR_News_API.ino index 10d2032d..fe252b23 100644 --- a/examples/Inkplate6COLOR/Projects/Inkplate6COLOR_News_API/Inkplate6COLOR_News_API.ino +++ b/examples/Inkplate6COLOR/Projects/Inkplate6COLOR_News_API/Inkplate6COLOR_News_API.ino @@ -53,6 +53,7 @@ Inkplate display; // Delay between API calls in miliseconds (first 60 represents minutes so you can change to your need) #define DELAY_MS (uint32_t)60 * 60 * 1000 +#define DELAY_WIFI_RETRY_SECONDS 5 // Variable for counting partial refreshes RTC_DATA_ATTR unsigned refreshes = 0; @@ -60,6 +61,8 @@ RTC_DATA_ATTR unsigned refreshes = 0; // Constant to determine when to full update const int fullRefresh = 20; +void setTime(); + void setup() { // Begin serial communitcation, sed for debugging @@ -68,8 +71,30 @@ void setup() display.begin(); display.setTextWrap(false); - // Our begin function - network.begin(); + // Connect Inkplate to the WiFi network + // Try connecting to a WiFi network. + // Parameters are network SSID, password, timeout in seconds and whether to print to serial. + // If the Inkplate isn't able to connect to a network stop further code execution and print an error message. + if (!display.connectWiFi(ssid, pass, WIFI_TIMEOUT, true)) + { + //Can't connect to netowrk + // Clear display for the error message + display.clearDisplay(); + // Set the font size; + display.setTextSize(3); + // Set the cursor positions and print the text. + display.setCursor((display.width() / 2) - 200, display.height() / 2); + display.print(F("Unable to connect to ")); + display.println(F(ssid)); + display.setCursor((display.width() / 2) - 200, (display.height() / 2) + 30); + display.println(F("Please check SSID and PASS!")); + // Display the error message on the Inkplate and go to deep sleep + display.display(); + esp_sleep_enable_timer_wakeup(1000L * DELAY_WIFI_RETRY_SECONDS); + (void)esp_deep_sleep_start(); + } + + setTime(); struct news *entities; @@ -153,6 +178,26 @@ void drawNews(struct news *entities) } +// Function for getting time from NTP server +void setTime() +{ + // Structure used to hold time information + struct tm timeInfo; + time_t nowSec; + // Fetch current time in epoch format and store it + display.getNTPEpoch(&nowSec); + // This loop ensures that the NTP time fetched is valid and beyond a certain threshold + while (nowSec < 8 * 3600 * 2) + { + delay(500); + yield(); + nowSec = time(nullptr); + } + gmtime_r(&nowSec, &timeInfo); + Serial.print(F("Current time: ")); + Serial.print(asctime(&timeInfo)); +} + void loop() { // Never here! If you are using deep sleep, the whole program should be in setup() because the board restarts each diff --git a/examples/Inkplate6COLOR/Projects/Inkplate6COLOR_News_API/Network.cpp b/examples/Inkplate6COLOR/Projects/Inkplate6COLOR_News_API/Network.cpp index a1e810dc..db478231 100644 --- a/examples/Inkplate6COLOR/Projects/Inkplate6COLOR_News_API/Network.cpp +++ b/examples/Inkplate6COLOR/Projects/Inkplate6COLOR_News_API/Network.cpp @@ -61,8 +61,7 @@ void Network::begin() } Serial.println(F(" connected")); - // Find internet time - setTime(); + } // Gets time from ntp server @@ -211,29 +210,3 @@ struct news* Network::getData() return ent; } - -// Function for initial time setting ovet the ntp server -void Network::setTime() -{ - // Used for setting correct time - configTime(0, 0, "pool.ntp.org", "time.nist.gov"); - - Serial.print(F("Waiting for NTP time sync: ")); - time_t nowSecs = time(nullptr); - while (nowSecs < 8 * 3600 * 2) - { - delay(500); - Serial.print(F(".")); - yield(); - nowSecs = time(nullptr); - } - - Serial.println(); - - // Used to store time info - struct tm timeinfo; - gmtime_r(&nowSecs, &timeinfo); - - Serial.print(F("Current time: ")); - Serial.print(asctime(&timeinfo)); -} diff --git a/examples/Inkplate6COLOR/Projects/Inkplate6COLOR_News_API/Network.h b/examples/Inkplate6COLOR/Projects/Inkplate6COLOR_News_API/Network.h index c6fcba3b..990c855b 100644 --- a/examples/Inkplate6COLOR/Projects/Inkplate6COLOR_News_API/Network.h +++ b/examples/Inkplate6COLOR/Projects/Inkplate6COLOR_News_API/Network.h @@ -52,8 +52,6 @@ class Network struct news* getData(); private: - // Functions called from within our class - void setTime(); }; #endif diff --git a/examples/Inkplate6COLOR/Projects/Inkplate6COLOR_Quotables/Inkplate6COLOR_Quotables.ino b/examples/Inkplate6COLOR/Projects/Inkplate6COLOR_Quotables/Inkplate6COLOR_Quotables.ino index 6ea6f2a0..f9b43c7d 100644 --- a/examples/Inkplate6COLOR/Projects/Inkplate6COLOR_Quotables/Inkplate6COLOR_Quotables.ino +++ b/examples/Inkplate6COLOR/Projects/Inkplate6COLOR_Quotables/Inkplate6COLOR_Quotables.ino @@ -52,7 +52,7 @@ Inkplate display; // Delay between API calls in seconds, 300 seconds is 5 minutes #define DELAY_S 300 - +#define DELAY_WIFI_RETRY_SECONDS 5 // Our functions declared below setup and loop void drawAll(); @@ -69,8 +69,27 @@ void setup() display.setTextWrap(true); // Set text wrapping to true display.setTextColor(BLACK); - // Our begin function - network.begin(); + // Try connecting to a WiFi network. + // Parameters are network SSID, password, timeout in seconds and whether to print to serial. + // If the Inkplate isn't able to connect to a network stop further code execution and print an error message. + if (!display.connectWiFi(ssid, pass, WIFI_TIMEOUT, true)) + { + //Can't connect to netowrk + // Clear display for the error message + display.clearDisplay(); + // Set the font size; + display.setTextSize(3); + // Set the cursor positions and print the text. + display.setCursor((display.width() / 2) - 200, display.height() / 2); + display.print(F("Unable to connect to ")); + display.println(F(ssid)); + display.setCursor((display.width() / 2) - 200, (display.height() / 2) + 30); + display.println(F("Please check SSID and PASS!")); + // Display the error message on the Inkplate and go to deep sleep + display.display(); + esp_sleep_enable_timer_wakeup(1000L * DELAY_WIFI_RETRY_SECONDS); + (void)esp_deep_sleep_start(); + } while (!network.getData(quote, author)) { diff --git a/examples/Inkplate6FLICK/Projects/Inkplate6FLICK_Crypto_Currency_Checker/Inkplate6FLICK_Crypto_Currency_Checker.ino b/examples/Inkplate6FLICK/Projects/Inkplate6FLICK_Crypto_Currency_Checker/Inkplate6FLICK_Crypto_Currency_Checker.ino index 7a90a9da..75a082ba 100644 --- a/examples/Inkplate6FLICK/Projects/Inkplate6FLICK_Crypto_Currency_Checker/Inkplate6FLICK_Crypto_Currency_Checker.ino +++ b/examples/Inkplate6FLICK/Projects/Inkplate6FLICK_Crypto_Currency_Checker/Inkplate6FLICK_Crypto_Currency_Checker.ino @@ -52,7 +52,7 @@ int timeZone = 2; // Put in your ssid and password char ssid[] = ""; char pass[] = ""; - +#define DELAY_WIFI_RETRY_SECONDS 5 // OPTIONAL: // change to a different currency char currency[32] = "bitcoin"; @@ -64,6 +64,8 @@ double data[3]; // Variables for storing all displayed data as char arrays char date[64]; +void setTime(); + void setup() { Serial.begin(115200); @@ -81,7 +83,30 @@ void setup() Serial.println("Touchscreen init failed!"); } - network.begin(); + // Try connecting to a WiFi network. + // Parameters are network SSID, password, timeout in seconds and whether to print to serial. + // If the Inkplate isn't able to connect to a network stop further code execution and print an error message. + if (!display.connectWiFi(ssid, pass, WIFI_TIMEOUT, true)) + { + // Can't connect to netowrk + // Clear display for the error message + display.clearDisplay(); + // Set the font size; + display.setTextSize(3); + // Set the cursor positions and print the text. + display.setCursor((display.width() / 2) - 200, display.height() / 2); + display.print(F("Unable to connect to ")); + display.println(F(ssid)); + display.setCursor((display.width() / 2) - 200, (display.height() / 2) + 30); + display.println(F("Please check ssid and pass!")); + // Display the error message on the Inkplate and go to deep sleep + display.display(); + esp_sleep_enable_timer_wakeup(1000L * DELAY_WIFI_RETRY_SECONDS); + (void)esp_deep_sleep_start(); + } + + // After connecting to WiFi we need to get internet time from NTP server + setTime(); getCoinPrices(); @@ -93,6 +118,26 @@ void loop() keysEvents(); } +// Function for getting time from NTP server +void setTime() +{ + // Structure used to hold time information + struct tm timeInfo; + time_t nowSec; + // Fetch current time in epoch format and store it + display.getNTPEpoch(&nowSec); + // This loop ensures that the NTP time fetched is valid and beyond a certain threshold + while (nowSec < 8 * 3600 * 2) + { + delay(500); + yield(); + display.getNTPEpoch(&nowSec); + } + gmtime_r(&nowSec, &timeInfo); + Serial.print(F("Current time: ")); + Serial.print(asctime(&timeInfo)); +} + void keysEvents() { if (display.touchInArea(30, 610, 400, 80)) // Refresh Screen diff --git a/examples/Inkplate6FLICK/Projects/Inkplate6FLICK_Crypto_Currency_Checker/Network.cpp b/examples/Inkplate6FLICK/Projects/Inkplate6FLICK_Crypto_Currency_Checker/Network.cpp index 411d1064..676e8254 100644 --- a/examples/Inkplate6FLICK/Projects/Inkplate6FLICK_Crypto_Currency_Checker/Network.cpp +++ b/examples/Inkplate6FLICK/Projects/Inkplate6FLICK_Crypto_Currency_Checker/Network.cpp @@ -59,8 +59,6 @@ void Network::begin() } Serial.println(F(" connected")); - // Find internet time - setTime(); } // Gets time from ntp server @@ -186,28 +184,3 @@ bool Network::getData(double *data) return !f; } -// Function for initial time setting ovet the ntp server -void Network::setTime() -{ - // Used for setting correct time - configTime(0, 0, "pool.ntp.org", "time.nist.gov"); - - Serial.print(F("Waiting for NTP time sync: ")); - time_t nowSecs = time(nullptr); - while (nowSecs < 8 * 3600 * 2) - { - delay(500); - Serial.print(F(".")); - yield(); - nowSecs = time(nullptr); - } - - Serial.println(); - - // Used to store time info - struct tm timeinfo; - gmtime_r(&nowSecs, &timeinfo); - - Serial.print(F("Current time: ")); - Serial.print(asctime(&timeinfo)); -} diff --git a/examples/Inkplate6FLICK/Projects/Inkplate6FLICK_Crypto_Currency_Checker/Network.h b/examples/Inkplate6FLICK/Projects/Inkplate6FLICK_Crypto_Currency_Checker/Network.h index 976c27ce..543adc82 100644 --- a/examples/Inkplate6FLICK/Projects/Inkplate6FLICK_Crypto_Currency_Checker/Network.h +++ b/examples/Inkplate6FLICK/Projects/Inkplate6FLICK_Crypto_Currency_Checker/Network.h @@ -41,8 +41,6 @@ class Network bool getData(double *data); private: - // Functions called from within our class - void setTime(); }; #endif diff --git a/examples/Inkplate6FLICK/Projects/Inkplate6FLICK_Crypto_Currency_Tracker/Inkplate6FLICK_Crypto_Currency_Tracker.ino b/examples/Inkplate6FLICK/Projects/Inkplate6FLICK_Crypto_Currency_Tracker/Inkplate6FLICK_Crypto_Currency_Tracker.ino index 2ff83dc6..ded72188 100644 --- a/examples/Inkplate6FLICK/Projects/Inkplate6FLICK_Crypto_Currency_Tracker/Inkplate6FLICK_Crypto_Currency_Tracker.ino +++ b/examples/Inkplate6FLICK/Projects/Inkplate6FLICK_Crypto_Currency_Tracker/Inkplate6FLICK_Crypto_Currency_Tracker.ino @@ -35,7 +35,7 @@ char pass[] = ""; // Delay between API calls in miliseconds #define DELAY_MS 3 * 60 * 1000 - +#define DELAY_WIFI_RETRY_SECONDS 5 // OPTIONAL: // change to a different currency char currency[] = "bitcoin"; @@ -114,6 +114,7 @@ textElement elements[] = { // Our functions declared below setup and loop void drawGraph(); void drawAll(); +void setTime(); void setup() { @@ -132,25 +133,48 @@ void setup() display.setTextWrap(false); display.setTextColor(0, 7); - // Our begin function - network.begin(); - - // Do a new network request - Serial.print("Retrying retriving data "); - while (!network.getData(data)) - { - Serial.print('.'); - delay(1000); - } - - // Our main drawing function - drawAll(); - - // Time drawing function - drawTime(); - - // Full refresh + // Try connecting to a WiFi network. + // Parameters are network SSID, password, timeout in seconds and whether to print to serial. + // If the Inkplate isn't able to connect to a network stop further code execution and print an error message. + if (!display.connectWiFi(ssid, pass, WIFI_TIMEOUT, true)) + { + // Can't connect to netowrk + // Clear display for the error message + display.clearDisplay(); + // Set the font size; + display.setTextSize(3); + // Set the cursor positions and print the text. + display.setCursor((display.width() / 2) - 200, display.height() / 2); + display.print(F("Unable to connect to ")); + display.println(F(ssid)); + display.setCursor((display.width() / 2) - 200, (display.height() / 2) + 30); + display.println(F("Please check ssid and pass!")); + // Display the error message on the Inkplate and go to deep sleep display.display(); + esp_sleep_enable_timer_wakeup(1000L * DELAY_WIFI_RETRY_SECONDS); + (void)esp_deep_sleep_start(); + } + + // After connecting to WiFi we need to get internet time from NTP server + setTime(); + + + // Do a new network request + Serial.print("Retrying retriving data "); + while (!network.getData(data)) + { + Serial.print('.'); + delay(1000); + } + + // Our main drawing function + drawAll(); + + // Time drawing function + drawTime(); + + // Full refresh + display.display(); goToSleep(); @@ -162,6 +186,26 @@ void loop() // Nothing here as we're using deep sleep } +// Function for getting time from NTP server +void setTime() +{ + // Structure used to hold time information + struct tm timeInfo; + time_t nowSec; + // Fetch current time in epoch format and store it + display.getNTPEpoch(&nowSec); + // This loop ensures that the NTP time fetched is valid and beyond a certain threshold + while (nowSec < 8 * 3600 * 2) + { + delay(500); + yield(); + display.getNTPEpoch(&nowSec); + } + gmtime_r(&nowSec, &timeInfo); + Serial.print(F("Current time: ")); + Serial.print(asctime(&timeInfo)); +} + void goToSleep() { Serial.println("Going to sleep..."); diff --git a/examples/Inkplate6FLICK/Projects/Inkplate6FLICK_Crypto_Currency_Tracker/Network.cpp b/examples/Inkplate6FLICK/Projects/Inkplate6FLICK_Crypto_Currency_Tracker/Network.cpp index e409b929..31e8c7c8 100644 --- a/examples/Inkplate6FLICK/Projects/Inkplate6FLICK_Crypto_Currency_Tracker/Network.cpp +++ b/examples/Inkplate6FLICK/Projects/Inkplate6FLICK_Crypto_Currency_Tracker/Network.cpp @@ -58,9 +58,6 @@ void Network::begin() } } Serial.println(F(" connected")); - - // Find internet time - setTime(); } // Gets time from ntp server @@ -192,30 +189,4 @@ bool Network::getData(double *data) WiFi.setSleep(sleep); return !f; -} - -// Function for initial time setting ovet the ntp server -void Network::setTime() -{ - // Used for setting correct time - configTime(0, 0, "pool.ntp.org", "time.nist.gov"); - - Serial.print(F("Waiting for NTP time sync: ")); - time_t nowSecs = time(nullptr); - while (nowSecs < 8 * 3600 * 2) - { - delay(500); - Serial.print(F(".")); - yield(); - nowSecs = time(nullptr); - } - - Serial.println(); - - // Used to store time info - struct tm timeinfo; - gmtime_r(&nowSecs, &timeinfo); - - Serial.print(F("Current time: ")); - Serial.print(asctime(&timeinfo)); -} +} \ No newline at end of file diff --git a/examples/Inkplate6FLICK/Projects/Inkplate6FLICK_Crypto_Currency_Tracker/Network.h b/examples/Inkplate6FLICK/Projects/Inkplate6FLICK_Crypto_Currency_Tracker/Network.h index 976c27ce..543adc82 100644 --- a/examples/Inkplate6FLICK/Projects/Inkplate6FLICK_Crypto_Currency_Tracker/Network.h +++ b/examples/Inkplate6FLICK/Projects/Inkplate6FLICK_Crypto_Currency_Tracker/Network.h @@ -41,8 +41,6 @@ class Network bool getData(double *data); private: - // Functions called from within our class - void setTime(); }; #endif diff --git a/examples/Inkplate6FLICK/Projects/Inkplate6FLICK_Daily_Weather_Station/Inkplate6FLICK_Daily_Weather_Station.ino b/examples/Inkplate6FLICK/Projects/Inkplate6FLICK_Daily_Weather_Station/Inkplate6FLICK_Daily_Weather_Station.ino index 38f4cc21..35a7e185 100644 --- a/examples/Inkplate6FLICK/Projects/Inkplate6FLICK_Daily_Weather_Station/Inkplate6FLICK_Daily_Weather_Station.ino +++ b/examples/Inkplate6FLICK/Projects/Inkplate6FLICK_Daily_Weather_Station/Inkplate6FLICK_Daily_Weather_Station.ino @@ -57,6 +57,7 @@ char apiKey[] = ""; // Delay between API calls, about 1000 per month, which is the free tier limit #define DELAY_MS 267800L +#define DELAY_WIFI_RETRY_SECONDS 5 // Inkplate object Inkplate display(INKPLATE_1BIT); @@ -115,6 +116,7 @@ void drawTime(); void drawCurrent(); void drawCity(); void drawTemps(); +void setTime(); void setup() { @@ -123,7 +125,29 @@ void setup() display.begin(); - network.begin(ssid, pass); + // Try connecting to a WiFi network. + // Parameters are network SSID, password, timeout in seconds and whether to print to serial. + // If the Inkplate isn't able to connect to a network stop further code execution and print an error message. + if (!display.connectWiFi(ssid, pass, WIFI_TIMEOUT, true)) + { + //Can't connect to netowrk + // Clear display for the error message + display.clearDisplay(); + // Set the font size; + display.setTextSize(3); + // Set the cursor positions and print the text. + display.setCursor((display.width() / 2) - 200, display.height() / 2); + display.print(F("Unable to connect to ")); + display.println(F(ssid)); + display.setCursor((display.width() / 2) - 200, (display.height() / 2) + 30); + display.println(F("Please check ssid and pass!")); + // Display the error message on the Inkplate and go to deep sleep + display.display(); + esp_sleep_enable_timer_wakeup(100L * DELAY_WIFI_RETRY_SECONDS); + (void)esp_deep_sleep_start(); + } + + setTime(); // Get all relevant data, see Network.cpp for info network.getData(lat, lon, apiKey, city, temps[0], temps[1], temps[2], temps[3], currentTemp, currentWind, currentTime, @@ -151,6 +175,27 @@ void loop() // Never here } +// Function for getting time from NTP server +void setTime() +{ + // Structure used to hold time information + struct tm timeInfo; + display.getNTPDateTime(&timeInfo); + time_t nowSec; + // Fetch current time in epoch format and store it + display.getNTPEpoch(&nowSec); + // This loop ensures that the NTP time fetched is valid and beyond a certain threshold + while (nowSec < 8 * 3600 * 2) + { + delay(500); + yield(); + display.getNTPEpoch(&nowSec); + } + gmtime_r(&nowSec, &timeInfo); + Serial.print(F("Current time: ")); + Serial.print(asctime(&timeInfo)); +} + // Function for drawing weather info void drawWeather() { diff --git a/examples/Inkplate6FLICK/Projects/Inkplate6FLICK_Daily_Weather_Station/Network.cpp b/examples/Inkplate6FLICK/Projects/Inkplate6FLICK_Daily_Weather_Station/Network.cpp index fc0e840c..76ee3ed5 100644 --- a/examples/Inkplate6FLICK/Projects/Inkplate6FLICK_Daily_Weather_Station/Network.cpp +++ b/examples/Inkplate6FLICK/Projects/Inkplate6FLICK_Daily_Weather_Station/Network.cpp @@ -43,9 +43,6 @@ void Network::begin(char *ssid, char *pass) } } Serial.println(F(" connected")); - - // Find internet time - setTime(); } // Gets time from ntp server @@ -227,32 +224,6 @@ bool Network::getData(char *lat, char *lon, char *apiKey, char *city, char *temp return 1; } -void Network::setTime() -{ - // Used for setting correct time - configTime(0, 0, "pool.ntp.org", "time.nist.gov"); - - Serial.print(F("Waiting for NTP time sync: ")); - time_t nowSecs = time(nullptr); - while (nowSecs < 8 * 3600 * 2) - { - // Print a dot every half a second while time is not set - delay(500); - Serial.print(F(".")); - yield(); - nowSecs = time(nullptr); - } - - Serial.println(); - - // Used to store time info - struct tm timeinfo; - gmtime_r(&nowSecs, &timeinfo); - - Serial.print(F("Current time: ")); - Serial.print(asctime(&timeinfo)); -} - void Network::getDays(char *day, char *day1, char *day2, char *day3) { // Seconds since 1.1.1970. diff --git a/examples/Inkplate6FLICK/Projects/Inkplate6FLICK_Daily_Weather_Station/Network.h b/examples/Inkplate6FLICK/Projects/Inkplate6FLICK_Daily_Weather_Station/Network.h index 7a8de194..4d2ab4e2 100644 --- a/examples/Inkplate6FLICK/Projects/Inkplate6FLICK_Daily_Weather_Station/Network.h +++ b/examples/Inkplate6FLICK/Projects/Inkplate6FLICK_Daily_Weather_Station/Network.h @@ -46,8 +46,6 @@ class Network int location = -1; private: - // Functions called from within our class - void setTime(); int timeZone = 0; }; diff --git a/examples/Inkplate6FLICK/Projects/Inkplate6FLICK_Google_Calendar/Inkplate6FLICK_Google_Calendar.ino b/examples/Inkplate6FLICK/Projects/Inkplate6FLICK_Google_Calendar/Inkplate6FLICK_Google_Calendar.ino index 38a62b58..0efa9069 100644 --- a/examples/Inkplate6FLICK/Projects/Inkplate6FLICK_Google_Calendar/Inkplate6FLICK_Google_Calendar.ino +++ b/examples/Inkplate6FLICK/Projects/Inkplate6FLICK_Google_Calendar/Inkplate6FLICK_Google_Calendar.ino @@ -55,6 +55,7 @@ int timeZone = 2; // Delay between API calls #define DELAY_MS 4 * 60000 +#define DELAY_WIFI_RETRY_SECONDS 5 // Variables to keep count of when to get new data, and when to just update time RTC_DATA_ATTR unsigned int refreshes = 0; @@ -92,6 +93,7 @@ void getToFrom(char *dst, char *from, char *to, int *day, int *timeStamp); bool drawEvent(entry *event, int day, int beginY, int maxHeigth, int *heigthNeeded); int cmp(const void *a, const void *b); void drawData(); +void setTime(); void setup() { @@ -108,7 +110,29 @@ void setup() display.setTextColor(0, 7); // Connect Inkplate to the WiFi network - network.begin(ssid, pass); + // Try connecting to a WiFi network. + // Parameters are network SSID, password, timeout in seconds and whether to print to serial. + // If the Inkplate isn't able to connect to a network stop further code execution and print an error message. + if (!display.connectWiFi(ssid, pass, WIFI_TIMEOUT, true)) + { + //Can't connect to netowrk + // Clear display for the error message + display.clearDisplay(); + // Set the font size; + display.setTextSize(3); + // Set the cursor positions and print the text. + display.setCursor((display.width() / 2) - 200, display.height() / 2); + display.print(F("Unable to connect to ")); + display.println(F(ssid)); + display.setCursor((display.width() / 2) - 200, (display.height() / 2) + 30); + display.println(F("Please check SSID and PASS!")); + // Display the error message on the Inkplate and go to deep sleep + display.display(); + esp_sleep_enable_timer_wakeup(1000L * DELAY_WIFI_RETRY_SECONDS); + (void)esp_deep_sleep_start(); + } + + setTime(); // Get the data from Google Calendar // Repeat attempts until data is fully downloaded @@ -143,6 +167,27 @@ void loop() // Never here } +// Function to get time from NPT server +void setTime() +{ + // Structure used to hold time information + struct tm timeInfo; + // Fetch current time from an NTP server and store it + time_t nowSec; + // Fetch current time in epoch format and store it + display.getNTPEpoch(&nowSec); + // This loop ensures that the NTP time fetched is valid and beyond a certain threshold + while(nowSec < 8 * 3600 * 2) + { + delay(500); + yield(); + display.getNTPEpoch(&nowSec); + } + gmtime_r(&nowSec, &timeInfo); + Serial.print(F("Current time: ")); + Serial.print(asctime(&timeInfo)); +} + // Function for drawing calendar info void drawInfo() { @@ -459,6 +504,10 @@ void drawData() &entries[entriesNum].timeStamp); } ++entriesNum; + if(entriesNum == 128) + { + break; + } } // Sort entries by time diff --git a/examples/Inkplate6FLICK/Projects/Inkplate6FLICK_Google_Calendar/Network.cpp b/examples/Inkplate6FLICK/Projects/Inkplate6FLICK_Google_Calendar/Network.cpp index 87a66fa1..66b16791 100644 --- a/examples/Inkplate6FLICK/Projects/Inkplate6FLICK_Google_Calendar/Network.cpp +++ b/examples/Inkplate6FLICK/Projects/Inkplate6FLICK_Google_Calendar/Network.cpp @@ -43,8 +43,6 @@ void Network::begin(char *ssid, char *pass) } Serial.println(F(" connected")); - // Find and print internet time, the timezone will be added later - setTime(); } // Gets time from ntp server @@ -141,29 +139,3 @@ bool Network::getData(char *calendarURL, char *data) return !f; } - -// Find internet time -void Network::setTime() -{ - // Used for setting correct time - configTime(0, 0, "pool.ntp.org", "time.nist.gov"); - - Serial.print(F("Waiting for NTP time sync: ")); - time_t nowSecs = time(nullptr); - while (nowSecs < 8 * 3600 * 2) - { - delay(500); - Serial.print(F(".")); - yield(); - nowSecs = time(nullptr); - } - Serial.println(); - - // Used to store time info - struct tm timeinfo; - gmtime_r(&nowSecs, &timeinfo); - - // Print the current time without adding a timezone - Serial.print(F("Current time: ")); - Serial.print(asctime(&timeinfo)); -} diff --git a/examples/Inkplate6FLICK/Projects/Inkplate6FLICK_Google_Calendar/Network.h b/examples/Inkplate6FLICK/Projects/Inkplate6FLICK_Google_Calendar/Network.h index 5449b75f..ffadefcb 100644 --- a/examples/Inkplate6FLICK/Projects/Inkplate6FLICK_Google_Calendar/Network.h +++ b/examples/Inkplate6FLICK/Projects/Inkplate6FLICK_Google_Calendar/Network.h @@ -35,8 +35,6 @@ class Network bool getData(char *calendarURL, char *data); private: - // Functions called from within our class - void setTime(); }; #endif diff --git a/examples/Inkplate6FLICK/Projects/Inkplate6FLICK_Hourly_Weather_Station/Inkplate6FLICK_Hourly_Weather_Station.ino b/examples/Inkplate6FLICK/Projects/Inkplate6FLICK_Hourly_Weather_Station/Inkplate6FLICK_Hourly_Weather_Station.ino index 28b50c10..1e3688cf 100644 --- a/examples/Inkplate6FLICK/Projects/Inkplate6FLICK_Hourly_Weather_Station/Inkplate6FLICK_Hourly_Weather_Station.ino +++ b/examples/Inkplate6FLICK/Projects/Inkplate6FLICK_Hourly_Weather_Station/Inkplate6FLICK_Hourly_Weather_Station.ino @@ -63,7 +63,7 @@ char apiKey[] = ""; // Delay between API calls, about 1000 per month, which is the free tier limit #define DELAY_MS 267800L - +#define DELAY_WIFI_RETRY_SECONDS 5 // Inkplate object Inkplate display(INKPLATE_1BIT); @@ -125,6 +125,7 @@ void drawCurrent(); void drawTemps(); void drawCity(); void drawTime(); +void setTime(); void setup() { @@ -133,8 +134,30 @@ void setup() display.begin(); - // Calling our begin from network.h file - network.begin(city); + // Connect Inkplate to the WiFi network + // Try connecting to a WiFi network. + // Parameters are network SSID, password, timeout in seconds and whether to print to serial. + // If the Inkplate isn't able to connect to a network stop further code execution and print an error message. + if (!display.connectWiFi(ssid, pass, WIFI_TIMEOUT, true)) + { + //Can't connect to netowrk + // Clear display for the error message + display.clearDisplay(); + // Set the font size; + display.setTextSize(3); + // Set the cursor positions and print the text. + display.setCursor((display.width() / 2) - 200, display.height() / 2); + display.print(F("Unable to connect to ")); + display.println(F(ssid)); + display.setCursor((display.width() / 2) - 200, (display.height() / 2) + 30); + display.println(F("Please check SSID and PASS!")); + // Display the error message on the Inkplate and go to deep sleep + display.display(); + esp_sleep_enable_timer_wakeup(1000L * DELAY_WIFI_RETRY_SECONDS); + (void)esp_deep_sleep_start(); + } + + setTime(); // Get all relevant data, see Network.cpp for info network.getTime(currentTime); @@ -175,6 +198,26 @@ void loop() // nothing here } +// Function for getting time from NTP server +void setTime() +{ + // Structure used to hold time information + struct tm timeInfo; + time_t nowSec; + // Fetch current time in epoch format and store it + display.getNTPEpoch(&nowSec); + // This loop ensures that the NTP time fetched is valid and beyond a certain threshold + while (nowSec < 8 * 3600 * 2) + { + delay(500); + yield(); + display.getNTPEpoch(&nowSec); + } + gmtime_r(&nowSec, &timeInfo); + Serial.print(F("Current time: ")); + Serial.print(asctime(&timeInfo)); +} + // Function for drawing weather info void drawWeather() { diff --git a/examples/Inkplate6FLICK/Projects/Inkplate6FLICK_Hourly_Weather_Station/Network.cpp b/examples/Inkplate6FLICK/Projects/Inkplate6FLICK_Hourly_Weather_Station/Network.cpp index b77bb0f8..b3a7f43a 100644 --- a/examples/Inkplate6FLICK/Projects/Inkplate6FLICK_Hourly_Weather_Station/Network.cpp +++ b/examples/Inkplate6FLICK/Projects/Inkplate6FLICK_Hourly_Weather_Station/Network.cpp @@ -53,9 +53,6 @@ void Network::begin(char *city) } Serial.println(F(" connected")); - // Find internet time - setTime(); - // reduce power by making WiFi module sleep WiFi.setSleep(1); } @@ -227,32 +224,6 @@ bool Network::getData(char *city, char *temp1, char *temp2, char *temp3, char *t return !f; } -void Network::setTime() -{ - // Used for setting correct time - configTime(0, 0, "pool.ntp.org", "time.nist.gov"); - - Serial.print(F("Waiting for NTP time sync: ")); - time_t nowSecs = time(nullptr); - while (nowSecs < 8 * 3600 * 2) - { - // Print a dot every half a second while time is not set - delay(500); - Serial.print(F(".")); - yield(); - nowSecs = time(nullptr); - } - - Serial.println(); - - // Used to store time info - struct tm timeinfo; - gmtime_r(&nowSecs, &timeinfo); - - Serial.print(F("Current time: ")); - Serial.print(asctime(&timeinfo)); -} - void Network::getHours(char *hour1, char *hour2, char *hour3, char *hour4) { // Format hours info diff --git a/examples/Inkplate6FLICK/Projects/Inkplate6FLICK_Hourly_Weather_Station/Network.h b/examples/Inkplate6FLICK/Projects/Inkplate6FLICK_Hourly_Weather_Station/Network.h index 09ec7b79..ac0c179f 100644 --- a/examples/Inkplate6FLICK/Projects/Inkplate6FLICK_Hourly_Weather_Station/Network.h +++ b/examples/Inkplate6FLICK/Projects/Inkplate6FLICK_Hourly_Weather_Station/Network.h @@ -57,8 +57,6 @@ class Network time_t dataEpoch = 0; private: - // Functions called from within our class - void setTime(); }; #endif diff --git a/examples/Inkplate6FLICK/Projects/Inkplate6FLICK_News_API/Inkplate6FLICK_News_API.ino b/examples/Inkplate6FLICK/Projects/Inkplate6FLICK_News_API/Inkplate6FLICK_News_API.ino index 8228bfb0..d5b24317 100644 --- a/examples/Inkplate6FLICK/Projects/Inkplate6FLICK_News_API/Inkplate6FLICK_News_API.ino +++ b/examples/Inkplate6FLICK/Projects/Inkplate6FLICK_News_API/Inkplate6FLICK_News_API.ino @@ -53,6 +53,7 @@ Inkplate display(INKPLATE_1BIT); // Delay between API calls in miliseconds (first 60 represents minutes so you can change to your need) #define DELAY_MS (uint32_t)60 * 60 * 1000 +#define DELAY_WIFI_RETRY_SECONDS 5 // Variable for counting partial refreshes RTC_DATA_ATTR unsigned refreshes = 0; @@ -60,6 +61,8 @@ RTC_DATA_ATTR unsigned refreshes = 0; // Constant to determine when to full update const int fullRefresh = 20; +void setTime(); + void setup() { // Begin serial communitcation, sed for debugging @@ -69,8 +72,30 @@ void setup() display.begin(); display.setTextWrap(false); - // Our begin function - network.begin(); + // Connect Inkplate to the WiFi network + // Try connecting to a WiFi network. + // Parameters are network SSID, password, timeout in seconds and whether to print to serial. + // If the Inkplate isn't able to connect to a network stop further code execution and print an error message. + if (!display.connectWiFi(ssid, pass, WIFI_TIMEOUT, true)) + { + //Can't connect to netowrk + // Clear display for the error message + display.clearDisplay(); + // Set the font size; + display.setTextSize(3); + // Set the cursor positions and print the text. + display.setCursor((display.width() / 2) - 200, display.height() / 2); + display.print(F("Unable to connect to ")); + display.println(F(ssid)); + display.setCursor((display.width() / 2) - 200, (display.height() / 2) + 30); + display.println(F("Please check SSID and PASS!")); + // Display the error message on the Inkplate and go to deep sleep + display.display(); + esp_sleep_enable_timer_wakeup(1000L * DELAY_WIFI_RETRY_SECONDS); + (void)esp_deep_sleep_start(); + } + + setTime(); struct news *entities; @@ -151,6 +176,46 @@ void drawNews(struct news *entities) } +// Function for getting time from NTP server +void setTime() +{ + // Structure used to hold time information + struct tm timeInfo; + time_t nowSec; + // Fetch current time in epoch format and store it + display.getNTPEpoch(&nowSec); + // This loop ensures that the NTP time fetched is valid and beyond a certain threshold + while (nowSec < 8 * 3600 * 2) + { + delay(500); + yield(); + nowSec = time(nullptr); + } + gmtime_r(&nowSec, &timeInfo); + Serial.print(F("Current time: ")); + Serial.print(asctime(&timeInfo)); +} + +// Function for getting time from NTP server +void setTime() +{ + // Structure used to hold time information + struct tm timeInfo; + time_t nowSec; + // Fetch current time in epoch format and store it + display.getNTPEpoch(&nowSec); + // This loop ensures that the NTP time fetched is valid and beyond a certain threshold + while (nowSec < 8 * 3600 * 2) + { + delay(500); + yield(); + nowSec = time(nullptr); + } + gmtime_r(&nowSec, &timeInfo); + Serial.print(F("Current time: ")); + Serial.print(asctime(&timeInfo)); +} + void loop() { // Never here diff --git a/examples/Inkplate6FLICK/Projects/Inkplate6FLICK_News_API/Network.cpp b/examples/Inkplate6FLICK/Projects/Inkplate6FLICK_News_API/Network.cpp index a1e810dc..71e8a03b 100644 --- a/examples/Inkplate6FLICK/Projects/Inkplate6FLICK_News_API/Network.cpp +++ b/examples/Inkplate6FLICK/Projects/Inkplate6FLICK_News_API/Network.cpp @@ -60,9 +60,6 @@ void Network::begin() } } Serial.println(F(" connected")); - - // Find internet time - setTime(); } // Gets time from ntp server @@ -211,29 +208,3 @@ struct news* Network::getData() return ent; } - -// Function for initial time setting ovet the ntp server -void Network::setTime() -{ - // Used for setting correct time - configTime(0, 0, "pool.ntp.org", "time.nist.gov"); - - Serial.print(F("Waiting for NTP time sync: ")); - time_t nowSecs = time(nullptr); - while (nowSecs < 8 * 3600 * 2) - { - delay(500); - Serial.print(F(".")); - yield(); - nowSecs = time(nullptr); - } - - Serial.println(); - - // Used to store time info - struct tm timeinfo; - gmtime_r(&nowSecs, &timeinfo); - - Serial.print(F("Current time: ")); - Serial.print(asctime(&timeinfo)); -} diff --git a/examples/Inkplate6FLICK/Projects/Inkplate6FLICK_News_API/Network.h b/examples/Inkplate6FLICK/Projects/Inkplate6FLICK_News_API/Network.h index c6fcba3b..990c855b 100644 --- a/examples/Inkplate6FLICK/Projects/Inkplate6FLICK_News_API/Network.h +++ b/examples/Inkplate6FLICK/Projects/Inkplate6FLICK_News_API/Network.h @@ -52,8 +52,6 @@ class Network struct news* getData(); private: - // Functions called from within our class - void setTime(); }; #endif diff --git a/examples/Inkplate6FLICK/Projects/Inkplate6FLICK_OpenWeather_Station/Inkplate6FLICK_OpenWeather_Station.ino b/examples/Inkplate6FLICK/Projects/Inkplate6FLICK_OpenWeather_Station/Inkplate6FLICK_OpenWeather_Station.ino index a00d4e0c..2545d454 100644 --- a/examples/Inkplate6FLICK/Projects/Inkplate6FLICK_OpenWeather_Station/Inkplate6FLICK_OpenWeather_Station.ino +++ b/examples/Inkplate6FLICK/Projects/Inkplate6FLICK_OpenWeather_Station/Inkplate6FLICK_OpenWeather_Station.ino @@ -77,6 +77,8 @@ bool metric = true; //<------------------------------TRUE is METRIC, FALSE is IM // Delay between API calls #define DELAY_MS 59000 +#define DELAY_WIFI_RETRY_SECONDS 5 +#define DELAY_API_RETRY_SECONDS 5 // Inkplate object Inkplate display(INKPLATE_1BIT); @@ -126,28 +128,26 @@ time_t t = now(); void connectWifi() { - - int ConnectCount = 20; - - if (WiFi.status() != WL_CONNECTED) + // Try connecting to a WiFi network. + // Parameters are network SSID, password, timeout in seconds and whether to print to serial. + // If the Inkplate isn't able to connect to a network stop further code execution and print an error message. + if (!display.connectWiFi(SSID, PASS, WIFI_TIMEOUT, true)) { - while (WiFi.status() != WL_CONNECTED) - { - if (ConnectCount++ == 20) - { - Serial.println("Connect WiFi"); - WiFi.begin(SSID, PASS); - Serial.print("Connecting."); - ConnectCount = 0; - } - Serial.print("."); - delay(1260); - } - Serial.print("\nConnected to: "); - Serial.println(SSID); - Serial.println("IP address: "); - Serial.println(WiFi.localIP()); - Serial.println("Connected WiFi"); + //Can't connect to netowrk + // Clear display for the error message + display.clearDisplay(); + // Set the font size; + display.setTextSize(3); + // Set the cursor positions and print the text. + display.setCursor((display.width() / 2) - 200, display.height() / 2); + display.print(F("Unable to connect to ")); + display.println(F(SSID)); + display.setCursor((display.width() / 2) - 200, (display.height() / 2) + 30); + display.println(F("Please check SSID and PASS!")); + // Display the error message on the Inkplate and go to deep sleep + display.display(); + esp_sleep_enable_timer_wakeup(1000L * DELAY_WIFI_RETRY_SECONDS); + (void)esp_deep_sleep_start(); } } //======================== END WIFI CONNECT ======================= @@ -268,10 +268,8 @@ void setup() display.println("Only older API keys for OneCall 2.5 work in free tier."); display.println("See the code comments the example for more info."); display.display(); - while (1) - { - delay(100); - } + esp_sleep_enable_timer_wakeup(1000L * DELAY_API_RETRY_SECONDS); + (void)esp_deep_sleep_start(); } Serial.println("API key is valid!"); diff --git a/examples/Inkplate6FLICK/Projects/Inkplate6FLICK_Quotables/Inkplate6FLICK_Quotables.ino b/examples/Inkplate6FLICK/Projects/Inkplate6FLICK_Quotables/Inkplate6FLICK_Quotables.ino index ce46030c..df219663 100644 --- a/examples/Inkplate6FLICK/Projects/Inkplate6FLICK_Quotables/Inkplate6FLICK_Quotables.ino +++ b/examples/Inkplate6FLICK/Projects/Inkplate6FLICK_Quotables/Inkplate6FLICK_Quotables.ino @@ -54,6 +54,7 @@ Inkplate display(INKPLATE_1BIT); // Delay between API calls in seconds, 300 seconds is 5 minutes #define DELAY_S 300 +#define DELAY_WIFI_RETRY_SECONDS 5 // Our functions declared below setup and loop void drawAll(); @@ -73,8 +74,27 @@ void setup() display.setTextColor(BLACK); display.setTextSize(3); - // Our begin function - network.begin(); + // Try connecting to a WiFi network. + // Parameters are network SSID, password, timeout in seconds and whether to print to serial. + // If the Inkplate isn't able to connect to a network stop further code execution and print an error message. + if (!display.connectWiFi(ssid, pass, WIFI_TIMEOUT, true)) + { + //Can't connect to netowrk + // Clear display for the error message + display.clearDisplay(); + // Set the font size; + display.setTextSize(3); + // Set the cursor positions and print the text. + display.setCursor((display.width() / 2) - 200, display.height() / 2); + display.print(F("Unable to connect to ")); + display.println(F(ssid)); + display.setCursor((display.width() / 2) - 200, (display.height() / 2) + 30); + display.println(F("Please check SSID and PASS!")); + // Display the error message on the Inkplate and go to deep sleep + display.display(); + esp_sleep_enable_timer_wakeup(1000L * DELAY_WIFI_RETRY_SECONDS); + (void)esp_deep_sleep_start(); + } while (!network.getData(quote, author)) { diff --git a/examples/Inkplate6FLICK/Projects/Inkplate6FLICK_Youtube_Subscriber_Counter/Inkplate6FLICK_Youtube_Subscriber_Counter.ino b/examples/Inkplate6FLICK/Projects/Inkplate6FLICK_Youtube_Subscriber_Counter/Inkplate6FLICK_Youtube_Subscriber_Counter.ino index 671051fc..16447357 100644 --- a/examples/Inkplate6FLICK/Projects/Inkplate6FLICK_Youtube_Subscriber_Counter/Inkplate6FLICK_Youtube_Subscriber_Counter.ino +++ b/examples/Inkplate6FLICK/Projects/Inkplate6FLICK_Youtube_Subscriber_Counter/Inkplate6FLICK_Youtube_Subscriber_Counter.ino @@ -42,6 +42,7 @@ // Delay between API calls in miliseconds (10 minutes) #define DELAY_MS 10 * 60 * 1000 +#define DELAY_WIFI_RETRY_SECONDS 5 // Create object with all networking functions Network network; @@ -125,8 +126,27 @@ void setup() delay(1000); } - // Our begin function - network.begin(ssid, pass); + // Try connecting to a WiFi network. + // Parameters are network SSID, password, timeout in seconds and whether to print to serial. + // If the Inkplate isn't able to connect to a network stop further code execution and print an error message. + if (!display.connectWiFi(ssid, pass, WIFI_TIMEOUT, true)) + { + //Can't connect to netowrk + // Clear display for the error message + display.clearDisplay(); + // Set the font size; + display.setTextSize(3); + // Set the cursor positions and print the text. + display.setCursor((display.width() / 2) - 200, display.height() / 2); + display.print(F("Unable to connect to ")); + display.println(F(ssid)); + display.setCursor((display.width() / 2) - 200, (display.height() / 2) + 30); + display.println(F("Please check SSID and PASS!")); + // Display the error message on the Inkplate and go to deep sleep + display.display(); + esp_sleep_enable_timer_wakeup(1000L * DELAY_WIFI_RETRY_SECONDS); + (void)esp_deep_sleep_start(); + } while (!network.getData(&channel, channel_id, api_key, &display)) { diff --git a/examples/Inkplate6PLUS/Projects/Inkplate6PLUS_Crypto_Currency_Checker/Inkplate6PLUS_Crypto_Currency_Checker.ino b/examples/Inkplate6PLUS/Projects/Inkplate6PLUS_Crypto_Currency_Checker/Inkplate6PLUS_Crypto_Currency_Checker.ino index fb865bf2..8dfcd6bf 100644 --- a/examples/Inkplate6PLUS/Projects/Inkplate6PLUS_Crypto_Currency_Checker/Inkplate6PLUS_Crypto_Currency_Checker.ino +++ b/examples/Inkplate6PLUS/Projects/Inkplate6PLUS_Crypto_Currency_Checker/Inkplate6PLUS_Crypto_Currency_Checker.ino @@ -33,7 +33,7 @@ int timeZone = 2; // Put in your ssid and password char ssid[] = ""; char pass[] = ""; - +#define DELAY_WIFI_RETRY_SECONDS 5 // OPTIONAL: // change to a different currency char currency[32] = "bitcoin"; @@ -45,6 +45,8 @@ double data[3]; // Variables for storing all displayed data as char arrays char date[64]; +void setTime(); + void setup() { Serial.begin(115200); @@ -64,7 +66,30 @@ void setup() Serial.println("Touchscreen init failed!"); } - network.begin(); + // Try connecting to a WiFi network. + // Parameters are network SSID, password, timeout in seconds and whether to print to serial. + // If the Inkplate isn't able to connect to a network stop further code execution and print an error message. + if (!display.connectWiFi(ssid, pass, WIFI_TIMEOUT, true)) + { + // Can't connect to netowrk + // Clear display for the error message + display.clearDisplay(); + // Set the font size; + display.setTextSize(3); + // Set the cursor positions and print the text. + display.setCursor((display.width() / 2) - 200, display.height() / 2); + display.print(F("Unable to connect to ")); + display.println(F(ssid)); + display.setCursor((display.width() / 2) - 200, (display.height() / 2) + 30); + display.println(F("Please check ssid and pass!")); + // Display the error message on the Inkplate and go to deep sleep + display.display(); + esp_sleep_enable_timer_wakeup(1000L * DELAY_WIFI_RETRY_SECONDS); + (void)esp_deep_sleep_start(); + } + + // After connecting to WiFi we need to get internet time from NTP server + setTime(); getCoinPrices(); @@ -76,6 +101,26 @@ void loop() keysEvents(); } +// Function for getting time from NTP server +void setTime() +{ + // Structure used to hold time information + struct tm timeInfo; + time_t nowSec; + // Fetch current time in epoch format and store it + display.getNTPEpoch(&nowSec); + // This loop ensures that the NTP time fetched is valid and beyond a certain threshold + while (nowSec < 8 * 3600 * 2) + { + delay(500); + yield(); + display.getNTPEpoch(&nowSec); + } + gmtime_r(&nowSec, &timeInfo); + Serial.print(F("Current time: ")); + Serial.print(asctime(&timeInfo)); +} + void keysEvents() { if (display.touchInArea(30, 610, 400, 80)) // Refresh Screen diff --git a/examples/Inkplate6PLUS/Projects/Inkplate6PLUS_Crypto_Currency_Checker/Network.cpp b/examples/Inkplate6PLUS/Projects/Inkplate6PLUS_Crypto_Currency_Checker/Network.cpp index 411d1064..77c44425 100644 --- a/examples/Inkplate6PLUS/Projects/Inkplate6PLUS_Crypto_Currency_Checker/Network.cpp +++ b/examples/Inkplate6PLUS/Projects/Inkplate6PLUS_Crypto_Currency_Checker/Network.cpp @@ -58,9 +58,6 @@ void Network::begin() } } Serial.println(F(" connected")); - - // Find internet time - setTime(); } // Gets time from ntp server @@ -185,29 +182,3 @@ bool Network::getData(double *data) return !f; } - -// Function for initial time setting ovet the ntp server -void Network::setTime() -{ - // Used for setting correct time - configTime(0, 0, "pool.ntp.org", "time.nist.gov"); - - Serial.print(F("Waiting for NTP time sync: ")); - time_t nowSecs = time(nullptr); - while (nowSecs < 8 * 3600 * 2) - { - delay(500); - Serial.print(F(".")); - yield(); - nowSecs = time(nullptr); - } - - Serial.println(); - - // Used to store time info - struct tm timeinfo; - gmtime_r(&nowSecs, &timeinfo); - - Serial.print(F("Current time: ")); - Serial.print(asctime(&timeinfo)); -} diff --git a/examples/Inkplate6PLUS/Projects/Inkplate6PLUS_Crypto_Currency_Checker/Network.h b/examples/Inkplate6PLUS/Projects/Inkplate6PLUS_Crypto_Currency_Checker/Network.h index 976c27ce..543adc82 100644 --- a/examples/Inkplate6PLUS/Projects/Inkplate6PLUS_Crypto_Currency_Checker/Network.h +++ b/examples/Inkplate6PLUS/Projects/Inkplate6PLUS_Crypto_Currency_Checker/Network.h @@ -41,8 +41,6 @@ class Network bool getData(double *data); private: - // Functions called from within our class - void setTime(); }; #endif diff --git a/examples/Inkplate6PLUS/Projects/Inkplate6PLUS_Crypto_Currency_Tracker/Inkplate6PLUS_Crypto_Currency_Tracker.ino b/examples/Inkplate6PLUS/Projects/Inkplate6PLUS_Crypto_Currency_Tracker/Inkplate6PLUS_Crypto_Currency_Tracker.ino index 188ab466..f592d415 100644 --- a/examples/Inkplate6PLUS/Projects/Inkplate6PLUS_Crypto_Currency_Tracker/Inkplate6PLUS_Crypto_Currency_Tracker.ino +++ b/examples/Inkplate6PLUS/Projects/Inkplate6PLUS_Crypto_Currency_Tracker/Inkplate6PLUS_Crypto_Currency_Tracker.ino @@ -35,6 +35,7 @@ char pass[] = ""; // Delay between API calls in miliseconds #define DELAY_MS 3 * 60 * 1000 +#define DELAY_WIFI_RETRY_SECONDS 5 // OPTIONAL: // change to a different currency @@ -114,6 +115,7 @@ textElement elements[] = { // Our functions declared below setup and loop void drawGraph(); void drawAll(); +void setTime(); void setup() { @@ -134,25 +136,45 @@ void setup() display.setTextWrap(false); display.setTextColor(0, 7); - // Our begin function - network.begin(); - - // Do a new network request - Serial.print("Retrying retriving data "); - while (!network.getData(data)) - { - Serial.print('.'); - delay(1000); - } - - // Our main drawing function - drawAll(); - - // Time drawing function - drawTime(); - - // Full refresh + // Try connecting to a WiFi network. + // Parameters are network SSID, password, timeout in seconds and whether to print to serial. + // If the Inkplate isn't able to connect to a network stop further code execution and print an error message. + if (!display.connectWiFi(ssid, pass, WIFI_TIMEOUT, true)) + { + // Can't connect to netowrk + // Clear display for the error message + display.clearDisplay(); + // Set the font size; + display.setTextSize(3); + // Set the cursor positions and print the text. + display.setCursor((display.width() / 2) - 200, display.height() / 2); + display.print(F("Unable to connect to ")); + display.println(F(ssid)); + display.setCursor((display.width() / 2) - 200, (display.height() / 2) + 30); + display.println(F("Please check ssid and pass!")); + // Display the error message on the Inkplate and go to deep sleep display.display(); + esp_sleep_enable_timer_wakeup(1000L * DELAY_WIFI_RETRY_SECONDS); + (void)esp_deep_sleep_start(); + } + + setTime(); + // Do a new network request + Serial.print("Retrying retriving data "); + while (!network.getData(data)) + { + Serial.print('.'); + delay(1000); + } + + // Our main drawing function + drawAll(); + + // Time drawing function + drawTime(); + + // Full refresh + display.display(); goToSleep(); @@ -164,6 +186,26 @@ void loop() // Nothing here as we're using deep sleep } +// Function for getting time from NTP server +void setTime() +{ + // Structure used to hold time information + struct tm timeInfo; + time_t nowSec; + // Fetch current time in epoch format and store it + display.getNTPEpoch(&nowSec); + // This loop ensures that the NTP time fetched is valid and beyond a certain threshold + while (nowSec < 8 * 3600 * 2) + { + delay(500); + yield(); + display.getNTPEpoch(&nowSec); + } + gmtime_r(&nowSec, &timeInfo); + Serial.print(F("Current time: ")); + Serial.print(asctime(&timeInfo)); +} + void goToSleep() { Serial.println("Going to sleep..."); diff --git a/examples/Inkplate6PLUS/Projects/Inkplate6PLUS_Daily_Weather_Station/Inkplate6PLUS_Daily_Weather_Station.ino b/examples/Inkplate6PLUS/Projects/Inkplate6PLUS_Daily_Weather_Station/Inkplate6PLUS_Daily_Weather_Station.ino index 3c3ca2fd..79528a4e 100644 --- a/examples/Inkplate6PLUS/Projects/Inkplate6PLUS_Daily_Weather_Station/Inkplate6PLUS_Daily_Weather_Station.ino +++ b/examples/Inkplate6PLUS/Projects/Inkplate6PLUS_Daily_Weather_Station/Inkplate6PLUS_Daily_Weather_Station.ino @@ -57,6 +57,7 @@ char apiKey[] = ""; // Delay between API calls, about 1000 per month, which is the free tier limit #define DELAY_MS 267800L +#define DELAY_WIFI_RETRY_SECONDS 5 // Inkplate object Inkplate display(INKPLATE_1BIT); @@ -115,6 +116,7 @@ void drawTime(); void drawCurrent(); void drawCity(); void drawTemps(); +void setTime(); void setup() { @@ -125,7 +127,29 @@ void setup() //display.setInkplatePowerMode(INKPLATE_USB_PWR_ONLY); display.begin(); - network.begin(ssid, pass); + // Try connecting to a WiFi network. + // Parameters are network SSID, password, timeout in seconds and whether to print to serial. + // If the Inkplate isn't able to connect to a network stop further code execution and print an error message. + if (!display.connectWiFi(ssid, pass, WIFI_TIMEOUT, true)) + { + //Can't connect to netowrk + // Clear display for the error message + display.clearDisplay(); + // Set the font size; + display.setTextSize(3); + // Set the cursor positions and print the text. + display.setCursor((display.width() / 2) - 200, display.height() / 2); + display.print(F("Unable to connect to ")); + display.println(F(ssid)); + display.setCursor((display.width() / 2) - 200, (display.height() / 2) + 30); + display.println(F("Please check ssid and pass!")); + // Display the error message on the Inkplate and go to deep sleep + display.display(); + esp_sleep_enable_timer_wakeup(100L * DELAY_WIFI_RETRY_SECONDS); + (void)esp_deep_sleep_start(); + } + + setTime(); // Get all relevant data, see Network.cpp for info network.getData(lat, lon, apiKey, city, temps[0], temps[1], temps[2], temps[3], currentTemp, currentWind, currentTime, @@ -153,6 +177,28 @@ void loop() // Never here } +// Function for getting time from NTP server +void setTime() +{ + // Structure used to hold time information + struct tm timeInfo; + display.getNTPDateTime(&timeInfo); + time_t nowSec; + // Fetch current time in epoch format and store it + display.getNTPEpoch(&nowSec); + // This loop ensures that the NTP time fetched is valid and beyond a certain threshold + while (nowSec < 8 * 3600 * 2) + { + delay(500); + yield(); + display.getNTPEpoch(&nowSec); + } + gmtime_r(&nowSec, &timeInfo); + Serial.print(F("Current time: ")); + Serial.print(asctime(&timeInfo)); +} + + // Function for drawing weather info void drawWeather() { diff --git a/examples/Inkplate6PLUS/Projects/Inkplate6PLUS_Daily_Weather_Station/Network.cpp b/examples/Inkplate6PLUS/Projects/Inkplate6PLUS_Daily_Weather_Station/Network.cpp index fc0e840c..76ee3ed5 100644 --- a/examples/Inkplate6PLUS/Projects/Inkplate6PLUS_Daily_Weather_Station/Network.cpp +++ b/examples/Inkplate6PLUS/Projects/Inkplate6PLUS_Daily_Weather_Station/Network.cpp @@ -43,9 +43,6 @@ void Network::begin(char *ssid, char *pass) } } Serial.println(F(" connected")); - - // Find internet time - setTime(); } // Gets time from ntp server @@ -227,32 +224,6 @@ bool Network::getData(char *lat, char *lon, char *apiKey, char *city, char *temp return 1; } -void Network::setTime() -{ - // Used for setting correct time - configTime(0, 0, "pool.ntp.org", "time.nist.gov"); - - Serial.print(F("Waiting for NTP time sync: ")); - time_t nowSecs = time(nullptr); - while (nowSecs < 8 * 3600 * 2) - { - // Print a dot every half a second while time is not set - delay(500); - Serial.print(F(".")); - yield(); - nowSecs = time(nullptr); - } - - Serial.println(); - - // Used to store time info - struct tm timeinfo; - gmtime_r(&nowSecs, &timeinfo); - - Serial.print(F("Current time: ")); - Serial.print(asctime(&timeinfo)); -} - void Network::getDays(char *day, char *day1, char *day2, char *day3) { // Seconds since 1.1.1970. diff --git a/examples/Inkplate6PLUS/Projects/Inkplate6PLUS_Daily_Weather_Station/Network.h b/examples/Inkplate6PLUS/Projects/Inkplate6PLUS_Daily_Weather_Station/Network.h index 7a8de194..4d2ab4e2 100644 --- a/examples/Inkplate6PLUS/Projects/Inkplate6PLUS_Daily_Weather_Station/Network.h +++ b/examples/Inkplate6PLUS/Projects/Inkplate6PLUS_Daily_Weather_Station/Network.h @@ -46,8 +46,6 @@ class Network int location = -1; private: - // Functions called from within our class - void setTime(); int timeZone = 0; }; diff --git a/examples/Inkplate6PLUS/Projects/Inkplate6PLUS_Google_Calendar/Inkplate6PLUS_Google_Calendar.ino b/examples/Inkplate6PLUS/Projects/Inkplate6PLUS_Google_Calendar/Inkplate6PLUS_Google_Calendar.ino index a66ad1bf..315ed0a3 100644 --- a/examples/Inkplate6PLUS/Projects/Inkplate6PLUS_Google_Calendar/Inkplate6PLUS_Google_Calendar.ino +++ b/examples/Inkplate6PLUS/Projects/Inkplate6PLUS_Google_Calendar/Inkplate6PLUS_Google_Calendar.ino @@ -55,6 +55,7 @@ int timeZone = 2; // Delay between API calls #define DELAY_MS 4 * 60000 +#define DELAY_WIFI_RETRY_SECONDS 5 // Variables to keep count of when to get new data, and when to just update time RTC_DATA_ATTR unsigned int refreshes = 0; @@ -110,7 +111,29 @@ void setup() display.setTextColor(0, 7); // Connect Inkplate to the WiFi network - network.begin(ssid, pass); + // Try connecting to a WiFi network. + // Parameters are network SSID, password, timeout in seconds and whether to print to serial. + // If the Inkplate isn't able to connect to a network stop further code execution and print an error message. + if (!display.connectWiFi(ssid, pass, WIFI_TIMEOUT, true)) + { + //Can't connect to netowrk + // Clear display for the error message + display.clearDisplay(); + // Set the font size; + display.setTextSize(3); + // Set the cursor positions and print the text. + display.setCursor((display.width() / 2) - 200, display.height() / 2); + display.print(F("Unable to connect to ")); + display.println(F(ssid)); + display.setCursor((display.width() / 2) - 200, (display.height() / 2) + 30); + display.println(F("Please check SSID and PASS!")); + // Display the error message on the Inkplate and go to deep sleep + display.display(); + esp_sleep_enable_timer_wakeup(1000L * DELAY_WIFI_RETRY_SECONDS); + (void)esp_deep_sleep_start(); + } + + setTime(); // Get the data from Google Calendar // Repeat attempts until data is fully downloaded @@ -145,6 +168,27 @@ void loop() // Never here } +// Function to get the time from NTP server +void setTime() +{ + // Structure used to hold time information + struct tm timeInfo; + // Fetch current time from an NTP server and store it + time_t nowSec; + // Fetch current time in epoch format and store it + display.getNTPEpoch(&nowSec); + // This loop ensures that the NTP time fetched is valid and beyond a certain threshold + while(nowSec < 8 * 3600 * 2) + { + delay(500); + yield(); + display.getNTPEpoch(&nowSec); + } + gmtime_r(&nowSec, &timeInfo); + Serial.print(F("Current time: ")); + Serial.print(asctime(&timeInfo)); +} + // Function for drawing calendar info void drawInfo() { @@ -461,6 +505,10 @@ void drawData() &entries[entriesNum].timeStamp); } ++entriesNum; + if(entriesNum == 128) + { + break; + } } // Sort entries by time diff --git a/examples/Inkplate6PLUS/Projects/Inkplate6PLUS_Google_Calendar/Network.cpp b/examples/Inkplate6PLUS/Projects/Inkplate6PLUS_Google_Calendar/Network.cpp index 87a66fa1..66b16791 100644 --- a/examples/Inkplate6PLUS/Projects/Inkplate6PLUS_Google_Calendar/Network.cpp +++ b/examples/Inkplate6PLUS/Projects/Inkplate6PLUS_Google_Calendar/Network.cpp @@ -43,8 +43,6 @@ void Network::begin(char *ssid, char *pass) } Serial.println(F(" connected")); - // Find and print internet time, the timezone will be added later - setTime(); } // Gets time from ntp server @@ -141,29 +139,3 @@ bool Network::getData(char *calendarURL, char *data) return !f; } - -// Find internet time -void Network::setTime() -{ - // Used for setting correct time - configTime(0, 0, "pool.ntp.org", "time.nist.gov"); - - Serial.print(F("Waiting for NTP time sync: ")); - time_t nowSecs = time(nullptr); - while (nowSecs < 8 * 3600 * 2) - { - delay(500); - Serial.print(F(".")); - yield(); - nowSecs = time(nullptr); - } - Serial.println(); - - // Used to store time info - struct tm timeinfo; - gmtime_r(&nowSecs, &timeinfo); - - // Print the current time without adding a timezone - Serial.print(F("Current time: ")); - Serial.print(asctime(&timeinfo)); -} diff --git a/examples/Inkplate6PLUS/Projects/Inkplate6PLUS_Google_Calendar/Network.h b/examples/Inkplate6PLUS/Projects/Inkplate6PLUS_Google_Calendar/Network.h index 5449b75f..ffadefcb 100644 --- a/examples/Inkplate6PLUS/Projects/Inkplate6PLUS_Google_Calendar/Network.h +++ b/examples/Inkplate6PLUS/Projects/Inkplate6PLUS_Google_Calendar/Network.h @@ -35,8 +35,6 @@ class Network bool getData(char *calendarURL, char *data); private: - // Functions called from within our class - void setTime(); }; #endif diff --git a/examples/Inkplate6PLUS/Projects/Inkplate6PLUS_Hourly_Weather_Station/Inkplate6PLUS_Hourly_Weather_Station.ino b/examples/Inkplate6PLUS/Projects/Inkplate6PLUS_Hourly_Weather_Station/Inkplate6PLUS_Hourly_Weather_Station.ino index 97204df1..f8ff594f 100644 --- a/examples/Inkplate6PLUS/Projects/Inkplate6PLUS_Hourly_Weather_Station/Inkplate6PLUS_Hourly_Weather_Station.ino +++ b/examples/Inkplate6PLUS/Projects/Inkplate6PLUS_Hourly_Weather_Station/Inkplate6PLUS_Hourly_Weather_Station.ino @@ -63,6 +63,7 @@ char apiKey[] = ""; // Delay between API calls, about 1000 per month, which is the free tier limit #define DELAY_MS 267800L +#define DELAY_WIFI_RETRY_SECONDS 5 // Inkplate object Inkplate display(INKPLATE_1BIT); @@ -125,6 +126,7 @@ void drawCurrent(); void drawTemps(); void drawCity(); void drawTime(); +void setTime(); void setup() { @@ -135,8 +137,30 @@ void setup() //display.setInkplatePowerMode(INKPLATE_USB_PWR_ONLY); display.begin(); - // Calling our begin from network.h file - network.begin(city); + // Connect Inkplate to the WiFi network + // Try connecting to a WiFi network. + // Parameters are network SSID, password, timeout in seconds and whether to print to serial. + // If the Inkplate isn't able to connect to a network stop further code execution and print an error message. + if (!display.connectWiFi(ssid, pass, WIFI_TIMEOUT, true)) + { + //Can't connect to netowrk + // Clear display for the error message + display.clearDisplay(); + // Set the font size; + display.setTextSize(3); + // Set the cursor positions and print the text. + display.setCursor((display.width() / 2) - 200, display.height() / 2); + display.print(F("Unable to connect to ")); + display.println(F(ssid)); + display.setCursor((display.width() / 2) - 200, (display.height() / 2) + 30); + display.println(F("Please check SSID and PASS!")); + // Display the error message on the Inkplate and go to deep sleep + display.display(); + esp_sleep_enable_timer_wakeup(1000L * DELAY_WIFI_RETRY_SECONDS); + (void)esp_deep_sleep_start(); + } + + setTime(); // Get all relevant data, see Network.cpp for info network.getTime(currentTime); @@ -177,6 +201,27 @@ void loop() // nothing here } + +// Function for getting time from NTP server +void setTime() +{ + // Structure used to hold time information + struct tm timeInfo; + time_t nowSec; + // Fetch current time in epoch format and store it + display.getNTPEpoch(&nowSec); + // This loop ensures that the NTP time fetched is valid and beyond a certain threshold + while (nowSec < 8 * 3600 * 2) + { + delay(500); + yield(); + display.getNTPEpoch(&nowSec); + } + gmtime_r(&nowSec, &timeInfo); + Serial.print(F("Current time: ")); + Serial.print(asctime(&timeInfo)); +} + // Function for drawing weather info void drawWeather() { diff --git a/examples/Inkplate6PLUS/Projects/Inkplate6PLUS_Hourly_Weather_Station/Network.cpp b/examples/Inkplate6PLUS/Projects/Inkplate6PLUS_Hourly_Weather_Station/Network.cpp index b77bb0f8..b3a7f43a 100644 --- a/examples/Inkplate6PLUS/Projects/Inkplate6PLUS_Hourly_Weather_Station/Network.cpp +++ b/examples/Inkplate6PLUS/Projects/Inkplate6PLUS_Hourly_Weather_Station/Network.cpp @@ -53,9 +53,6 @@ void Network::begin(char *city) } Serial.println(F(" connected")); - // Find internet time - setTime(); - // reduce power by making WiFi module sleep WiFi.setSleep(1); } @@ -227,32 +224,6 @@ bool Network::getData(char *city, char *temp1, char *temp2, char *temp3, char *t return !f; } -void Network::setTime() -{ - // Used for setting correct time - configTime(0, 0, "pool.ntp.org", "time.nist.gov"); - - Serial.print(F("Waiting for NTP time sync: ")); - time_t nowSecs = time(nullptr); - while (nowSecs < 8 * 3600 * 2) - { - // Print a dot every half a second while time is not set - delay(500); - Serial.print(F(".")); - yield(); - nowSecs = time(nullptr); - } - - Serial.println(); - - // Used to store time info - struct tm timeinfo; - gmtime_r(&nowSecs, &timeinfo); - - Serial.print(F("Current time: ")); - Serial.print(asctime(&timeinfo)); -} - void Network::getHours(char *hour1, char *hour2, char *hour3, char *hour4) { // Format hours info diff --git a/examples/Inkplate6PLUS/Projects/Inkplate6PLUS_Hourly_Weather_Station/Network.h b/examples/Inkplate6PLUS/Projects/Inkplate6PLUS_Hourly_Weather_Station/Network.h index 09ec7b79..ac0c179f 100644 --- a/examples/Inkplate6PLUS/Projects/Inkplate6PLUS_Hourly_Weather_Station/Network.h +++ b/examples/Inkplate6PLUS/Projects/Inkplate6PLUS_Hourly_Weather_Station/Network.h @@ -57,8 +57,6 @@ class Network time_t dataEpoch = 0; private: - // Functions called from within our class - void setTime(); }; #endif diff --git a/examples/Inkplate6PLUS/Projects/Inkplate6PLUS_News_API/Inkplate6PLUS_News_API.ino b/examples/Inkplate6PLUS/Projects/Inkplate6PLUS_News_API/Inkplate6PLUS_News_API.ino index 3c3053e5..9cfc4d43 100644 --- a/examples/Inkplate6PLUS/Projects/Inkplate6PLUS_News_API/Inkplate6PLUS_News_API.ino +++ b/examples/Inkplate6PLUS/Projects/Inkplate6PLUS_News_API/Inkplate6PLUS_News_API.ino @@ -53,6 +53,7 @@ Inkplate display(INKPLATE_1BIT); // Delay between API calls in miliseconds (first 60 represents minutes so you can change to your need) #define DELAY_MS (uint32_t)60 * 60 * 1000 +#define DELAY_WIFI_RETRY_SECONDS 5 // Variable for counting partial refreshes RTC_DATA_ATTR unsigned refreshes = 0; @@ -60,6 +61,8 @@ RTC_DATA_ATTR unsigned refreshes = 0; // Constant to determine when to full update const int fullRefresh = 20; +void setTime(); + void setup() { // Begin serial communitcation, sed for debugging @@ -71,8 +74,30 @@ void setup() display.begin(); display.setTextWrap(false); - // Our begin function - network.begin(); + // Connect Inkplate to the WiFi network + // Try connecting to a WiFi network. + // Parameters are network SSID, password, timeout in seconds and whether to print to serial. + // If the Inkplate isn't able to connect to a network stop further code execution and print an error message. + if (!display.connectWiFi(ssid, pass, WIFI_TIMEOUT, true)) + { + //Can't connect to netowrk + // Clear display for the error message + display.clearDisplay(); + // Set the font size; + display.setTextSize(3); + // Set the cursor positions and print the text. + display.setCursor((display.width() / 2) - 200, display.height() / 2); + display.print(F("Unable to connect to ")); + display.println(F(ssid)); + display.setCursor((display.width() / 2) - 200, (display.height() / 2) + 30); + display.println(F("Please check SSID and PASS!")); + // Display the error message on the Inkplate and go to deep sleep + display.display(); + esp_sleep_enable_timer_wakeup(1000L * DELAY_WIFI_RETRY_SECONDS); + (void)esp_deep_sleep_start(); + } + + setTime(); struct news *entities; @@ -153,6 +178,26 @@ void drawNews(struct news *entities) } +// Function for getting time from NTP server +void setTime() +{ + // Structure used to hold time information + struct tm timeInfo; + time_t nowSec; + // Fetch current time in epoch format and store it + display.getNTPEpoch(&nowSec); + // This loop ensures that the NTP time fetched is valid and beyond a certain threshold + while (nowSec < 8 * 3600 * 2) + { + delay(500); + yield(); + nowSec = time(nullptr); + } + gmtime_r(&nowSec, &timeInfo); + Serial.print(F("Current time: ")); + Serial.print(asctime(&timeInfo)); +} + void loop() { // Never here diff --git a/examples/Inkplate6PLUS/Projects/Inkplate6PLUS_News_API/Network.cpp b/examples/Inkplate6PLUS/Projects/Inkplate6PLUS_News_API/Network.cpp index a1e810dc..71e8a03b 100644 --- a/examples/Inkplate6PLUS/Projects/Inkplate6PLUS_News_API/Network.cpp +++ b/examples/Inkplate6PLUS/Projects/Inkplate6PLUS_News_API/Network.cpp @@ -60,9 +60,6 @@ void Network::begin() } } Serial.println(F(" connected")); - - // Find internet time - setTime(); } // Gets time from ntp server @@ -211,29 +208,3 @@ struct news* Network::getData() return ent; } - -// Function for initial time setting ovet the ntp server -void Network::setTime() -{ - // Used for setting correct time - configTime(0, 0, "pool.ntp.org", "time.nist.gov"); - - Serial.print(F("Waiting for NTP time sync: ")); - time_t nowSecs = time(nullptr); - while (nowSecs < 8 * 3600 * 2) - { - delay(500); - Serial.print(F(".")); - yield(); - nowSecs = time(nullptr); - } - - Serial.println(); - - // Used to store time info - struct tm timeinfo; - gmtime_r(&nowSecs, &timeinfo); - - Serial.print(F("Current time: ")); - Serial.print(asctime(&timeinfo)); -} diff --git a/examples/Inkplate6PLUS/Projects/Inkplate6PLUS_News_API/Network.h b/examples/Inkplate6PLUS/Projects/Inkplate6PLUS_News_API/Network.h index c6fcba3b..990c855b 100644 --- a/examples/Inkplate6PLUS/Projects/Inkplate6PLUS_News_API/Network.h +++ b/examples/Inkplate6PLUS/Projects/Inkplate6PLUS_News_API/Network.h @@ -52,8 +52,6 @@ class Network struct news* getData(); private: - // Functions called from within our class - void setTime(); }; #endif diff --git a/examples/Inkplate6PLUS/Projects/Inkplate6PLUS_OpenWeather_Station/Inkplate6PLUS_OpenWeather_Station.ino b/examples/Inkplate6PLUS/Projects/Inkplate6PLUS_OpenWeather_Station/Inkplate6PLUS_OpenWeather_Station.ino index 3f96860c..3391c596 100644 --- a/examples/Inkplate6PLUS/Projects/Inkplate6PLUS_OpenWeather_Station/Inkplate6PLUS_OpenWeather_Station.ino +++ b/examples/Inkplate6PLUS/Projects/Inkplate6PLUS_OpenWeather_Station/Inkplate6PLUS_OpenWeather_Station.ino @@ -40,6 +40,8 @@ #include "OpenWeatherOneCall.h" #define SSID "" #define PASS "" +// Variable to store WiFi connection timeout +int timeoutSeconds = 100; // Openweather API key /** @@ -77,6 +79,8 @@ bool metric = true; //<------------------------------TRUE is METRIC, FALSE is IM // Delay between API calls #define DELAY_MS 59000 +#define DELAY_WIFI_RETRY_SECONDS 5 +#define DELAY_API_RETRY_SECONDS 5 // Inkplate object Inkplate display(INKPLATE_1BIT); @@ -124,33 +128,6 @@ char Output[200] = {0}; OpenWeatherOneCall OWOC; // Invoke OpenWeather Library time_t t = now(); -void connectWifi() -{ - - int ConnectCount = 20; - - if (WiFi.status() != WL_CONNECTED) - { - while (WiFi.status() != WL_CONNECTED) - { - if (ConnectCount++ == 20) - { - Serial.println("Connect WiFi"); - WiFi.begin(SSID, PASS); - Serial.print("Connecting."); - ConnectCount = 0; - } - Serial.print("."); - delay(1260); - } - Serial.print("\nConnected to: "); - Serial.println(SSID); - Serial.println("IP address: "); - Serial.println(WiFi.localIP()); - Serial.println("Connected WiFi"); - } -} //======================== END WIFI CONNECT ======================= - void GetCurrentWeather() { //================================= @@ -250,36 +227,64 @@ void setup() } Serial.println("Serial Monitor Initialized"); - // Uncomment this line if you have a USB Power Only Inkplate6PLUS - // Must be called before display.begin()! - //display.setInkplatePowerMode(INKPLATE_USB_PWR_ONLY); + // display.begin(); - connectWifi(); + // Initial cleaning of buffer and physical screen + display.clearDisplay(); + display.display(); + + // Welcome screen + display.setCursor(215, 400); + display.setTextSize(3); + display.print(F("Welcome to Wol Inkplate 6PLUS weather example!")); + display.display(); + Serial.println("Welcome to Wol Inkplate 6PLUS weather example!"); + display.display(); + + // Try connecting to a WiFi network. + // Parameters are network SSID, password, timeout in seconds and whether to print to serial. + // If the Inkplate isn't able to connect to a network stop further code execution and print an error message. + if (!display.connectWiFi(SSID, PASS, timeoutSeconds, true)) + { + // Can't connect to netowrk + // Clear display for the error message + display.clearDisplay(); + // Set the font size; + display.setTextSize(3); + // Set the cursor positions and print the text. + display.setCursor((display.width() / 2) - 200, display.height() / 2); + display.print(F("Unable to connect to ")); + display.println(F(SSID)); + display.setCursor((display.width() / 2) - 200, (display.height() / 2) + 30); + display.println(F("Please check SSID and PASS!")); + // Display the error message on the Inkplate and go to deep sleep + display.display(); + esp_sleep_enable_timer_wakeup(1260L * DELAY_WIFI_RETRY_SECONDS); + (void)esp_deep_sleep_start(); + } // Check if we have a valid API key: Serial.println("Checking if API key is valid..."); - if(!checkIfAPIKeyIsValid(APIKEY)) + if (!checkIfAPIKeyIsValid(APIKEY)) { // If we don't, notify the user Serial.println("API key is invalid!"); display.clearDisplay(); - display.setCursor(0,0); + display.setCursor(0, 0); display.setTextSize(2); display.println("Can't get data from OpenWeatherMaps! Check your API key!"); display.println("Only older API keys for OneCall 2.5 work in free tier."); display.println("See the code comments the example for more info."); + // Display the error message on the Inkplate and go to deep sleep display.display(); - while(1) - { - delay(100); - } + esp_sleep_enable_timer_wakeup(1260L * DELAY_API_RETRY_SECONDS); + (void)esp_deep_sleep_start(); } Serial.println("API key is valid!"); // Clear display t = now(); - if ((minute(t) % 30) == 0) // Also returns 0 when time isn't set { GetCurrentWeather(); @@ -302,11 +307,10 @@ void setup() { } - ++refreshes; - // Go to sleep before checking again + ++refreshes; esp_sleep_enable_timer_wakeup(1260L * DELAY_MS); - (void)esp_light_sleep_start(); + (void)esp_deep_sleep_start(); } void loop() @@ -583,7 +587,7 @@ bool checkIfAPIKeyIsValid(char *APIKEY) bool failed = false; // Create the buffer for holding the API response, make it large enough just in case - char * data; + char *data; data = (char *)ps_malloc(50000LL); // Http object used to make GET request @@ -593,7 +597,7 @@ bool checkIfAPIKeyIsValid(char *APIKEY) http.getStream().setNoDelay(true); // Combine the base URL and the API key to do a test call - char * baseURL = "https://api.openweathermap.org/data/2.5/onecall?lat=45.560001&lon=18.675880&units=metric&appid="; + char *baseURL = "https://api.openweathermap.org/data/2.5/onecall?lat=45.560001&lon=18.675880&units=metric&appid="; char apiTestURL[200]; sprintf(apiTestURL, "%s%s", baseURL, APIKEY); @@ -601,7 +605,7 @@ bool checkIfAPIKeyIsValid(char *APIKEY) http.begin(apiTestURL); delay(300); - + // Download data until it's a verified complete download // Actually do request int httpCode = http.GET(); @@ -624,7 +628,8 @@ bool checkIfAPIKeyIsValid(char *APIKEY) data[n++] = 0; // If the reply constains this string - it's invalid - if(strstr(data, "Invalid API key.") != NULL) failed = true; + if (strstr(data, "Invalid API key.") != NULL) + failed = true; } else { diff --git a/examples/Inkplate6PLUS/Projects/Inkplate6PLUS_Quotables/Inkplate6PLUS_Quotables.ino b/examples/Inkplate6PLUS/Projects/Inkplate6PLUS_Quotables/Inkplate6PLUS_Quotables.ino index bdbb4a3e..a3b1bff7 100644 --- a/examples/Inkplate6PLUS/Projects/Inkplate6PLUS_Quotables/Inkplate6PLUS_Quotables.ino +++ b/examples/Inkplate6PLUS/Projects/Inkplate6PLUS_Quotables/Inkplate6PLUS_Quotables.ino @@ -55,6 +55,7 @@ Inkplate display(INKPLATE_1BIT); // Delay between API calls in seconds, 300 seconds is 5 minutes #define DELAY_S 300 +#define DELAY_WIFI_RETRY_SECONDS 5 // Our functions declared below setup and loop void drawAll(); @@ -77,7 +78,27 @@ void setup() display.setTextSize(3); // Our begin function - network.begin(); + // Try connecting to a WiFi network. + // Parameters are network SSID, password, timeout in seconds and whether to print to serial. + // If the Inkplate isn't able to connect to a network stop further code execution and print an error message. + if (!display.connectWiFi(ssid, pass, WIFI_TIMEOUT, true)) + { + //Can't connect to netowrk + // Clear display for the error message + display.clearDisplay(); + // Set the font size; + display.setTextSize(3); + // Set the cursor positions and print the text. + display.setCursor((display.width() / 2) - 200, display.height() / 2); + display.print(F("Unable to connect to ")); + display.println(F(ssid)); + display.setCursor((display.width() / 2) - 200, (display.height() / 2) + 30); + display.println(F("Please check SSID and PASS!")); + // Display the error message on the Inkplate and go to deep sleep + display.display(); + esp_sleep_enable_timer_wakeup(1000L * DELAY_WIFI_RETRY_SECONDS); + (void)esp_deep_sleep_start(); + } while (!network.getData(quote, author)) { diff --git a/examples/Inkplate6PLUS/Projects/Inkplate6PLUS_Youtube_Subscriber_Counter/Inkplate6PLUS_Youtube_Subscriber_Counter.ino b/examples/Inkplate6PLUS/Projects/Inkplate6PLUS_Youtube_Subscriber_Counter/Inkplate6PLUS_Youtube_Subscriber_Counter.ino index 813d923e..f2e58f0b 100644 --- a/examples/Inkplate6PLUS/Projects/Inkplate6PLUS_Youtube_Subscriber_Counter/Inkplate6PLUS_Youtube_Subscriber_Counter.ino +++ b/examples/Inkplate6PLUS/Projects/Inkplate6PLUS_Youtube_Subscriber_Counter/Inkplate6PLUS_Youtube_Subscriber_Counter.ino @@ -42,6 +42,7 @@ // Delay between API calls in miliseconds (10 minutes) #define DELAY_MS 10 * 60 * 1000 +#define DELAY_WIFI_RETRY_SECONDS 5 // Create object with all networking functions Network network; @@ -127,8 +128,27 @@ void setup() delay(1000); } - // Our begin function - network.begin(ssid, pass); + // Try connecting to a WiFi network. + // Parameters are network SSID, password, timeout in seconds and whether to print to serial. + // If the Inkplate isn't able to connect to a network stop further code execution and print an error message. + if (!display.connectWiFi(ssid, pass, WIFI_TIMEOUT, true)) + { + //Can't connect to netowrk + // Clear display for the error message + display.clearDisplay(); + // Set the font size; + display.setTextSize(3); + // Set the cursor positions and print the text. + display.setCursor((display.width() / 2) - 200, display.height() / 2); + display.print(F("Unable to connect to ")); + display.println(F(ssid)); + display.setCursor((display.width() / 2) - 200, (display.height() / 2) + 30); + display.println(F("Please check SSID and PASS!")); + // Display the error message on the Inkplate and go to deep sleep + display.display(); + esp_sleep_enable_timer_wakeup(1000L * DELAY_WIFI_RETRY_SECONDS); + (void)esp_deep_sleep_start(); + } while (!network.getData(&channel, channel_id, api_key, &display)) {