Skip to content

Commit

Permalink
Implemented ConnectWiFi in Inkplate projects
Browse files Browse the repository at this point in the history
  • Loading branch information
rsoric committed Sep 17, 2024
1 parent f6ef4ce commit d356bee
Show file tree
Hide file tree
Showing 176 changed files with 3,177 additions and 1,985 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -113,6 +114,7 @@ textElement elements[] = {
};

// Our functions declared below setup and loop
void getTime();
void drawGraph();
void drawAll();

Expand All @@ -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))
{
Expand All @@ -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()
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -58,9 +58,6 @@ void Network::begin()
}
}
Serial.println(F(" connected"));

// Find internet time
setTime();
}

// Gets time from ntp server
Expand Down Expand Up @@ -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));
}
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -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
Expand All @@ -115,15 +116,37 @@ void drawCurrent();
void drawTemps();
void drawCity();
void drawTime();
void setTime();

void setup()
{
// Begin serial and display
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)
Expand Down Expand Up @@ -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()
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,9 +43,6 @@ void Network::begin(char *ssid, char *pass)
}
}
Serial.println(F(" connected"));

// Find internet time
setTime();
}

// Gets time from ntp server
Expand Down Expand Up @@ -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.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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();
}

Expand Down Expand Up @@ -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;
Expand All @@ -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)
Expand All @@ -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;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Loading

0 comments on commit d356bee

Please sign in to comment.