// Include libraries
#include <Wire.h> // Provides functions for I2C communication with the OLED display. (built-in)
#include <Adafruit_GFX.h> // Basic graphics library used by the OLED library.
#include <Adafruit_SSD1306.h> // Library for controlling the SSD1306 OLED display.
#include <WiFi.h> // Library for connecting the ESP32 to a Wi-Fi network.
//#include <time.h> // Provides functions for retriving time info. (built-in)
// Define OLED parameters
#define SCREEN_WIDTH 128
#define SCREEN_HEIGHT 64
#define OLED_RESET -1 // Pin connected to the OLED reset pin (if not connected, set to -1).
#define SCREEN_ADDRESS 0x3C // I2C address of the OLED display
// Define WIFI-Time parameters
#define NTP_SERVER "pool.ntp.org" // Address of the NTP server used for time synchronization ("pool.ntp.org" is a common choice).
#define UTC_OFFSET 3600*5 + 30*60 // Offset from UTC in seconds
#define UTC_OFFSET_DST 0 // Offset during Daylight Saving Time
// variables for time
int days = 0;
int hours = 0;
int minutes = 0;
int seconds = 0;
// An instance (display) of the Adafruit_SSD1306 class to interact with the OLED display.
Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, OLED_RESET);
void setup() {
Serial.begin(115200); // Initializes serial communication.
// Initializes the OLED display using display.begin(). If unsuccessful, an error message is printed.
if (!display.begin(SSD1306_SWITCHCAPVCC, SCREEN_ADDRESS)) {
Serial.println(F("SSD1306 allocation failed"));
for (;;);
}
// Adafruit boot screen
display.display();
delay(500);
// Initiate a connection to a Wi-Fi network.
WiFi.begin("Wokwi-GUEST", "", 6); // Arguments = (SSID, password, channel)
display.clearDisplay();
print_line("Connecting", 0, 0, 2);
print_line("to WIFI", 0, 20, 2);
int counter = 0;
while (WiFi.status() != WL_CONNECTED) { // While wifi not connected
print_line("-", 5 * counter, 40, 4); // To simulate a loading bar
counter += 1;
}
display.clearDisplay();
print_line("WIFI", 0, 0, 2);
print_line("Connected", 0, 20, 2);
delay(500);
configTime(UTC_OFFSET, UTC_OFFSET_DST, NTP_SERVER); // Configure the time with time zone and server.
}
void loop() {
display.clearDisplay();
update_time();
print_time_now();
}
void update_time() {
// Create a time structure (struct tm) called timeinfo that contains all the details about the time (min, sec, hour, etc…).
// This line declares a variable named timeinfo of type struct tm.
struct tm timeinfo;
// If a function needs to modify the original data,
// you can pass the address (memory location) of the variable using the & (address-of) operator.
// This line retrieves the current local time and stores the details in the timeinfo variable.
getLocalTime(&timeinfo);
// Declares a character array named time_hour with a size of 3 to store the hour value as a string (including a null terminator).
char time_hour[3];
char time_minute[3];
char time_second[3];
char time_day[3];
// This line uses the strftime function to format the hour value from the timeinfo structure
// according to the format specifier "%H".
// string from time
strftime(time_hour, 3, "%H", &timeinfo);
strftime(time_minute, 3, "%M", &timeinfo);
strftime(time_second, 3, "%S", &timeinfo);
strftime(time_day, 3, "%d", &timeinfo);
// Converts the string in time_hour (which should now represent the hour as a string) to an integer using atoi (ASCII to integer)
// and stores the converted value in the variable hours.
// ASCII to integer
hours = atoi(time_hour);
minutes = atoi(time_minute);
seconds = atoi(time_second);
days = atoi(time_day);
}
void print_time_now(void) { // update_time() and print_time_now() finctions can be combined.
struct tm timeinfo;
getLocalTime(&timeinfo);
char full_time[9];
strftime(full_time, 9, "%H:%M:%S", &timeinfo);
print_line(full_time, 15,0,2);
/*************
print_line(String(days), 0, 0, 2);
print_line(":", 20, 0, 2);
print_line(String(hours), 30, 0, 2);
print_line(":", 50, 0, 2);
print_line(String(minutes), 60, 0, 2);
print_line(":", 80, 0, 2);
print_line(String(seconds), 90, 0, 2);
**************/
}
void print_line(String text, int column, int row, int text_size) {
display.setTextSize(text_size);
display.setTextColor(SSD1306_WHITE);
display.setCursor(column, row);
display.println(text);
display.display();
}
/************************************************************
// Alternate shorter implementation (Can eliminate update_time() and print_time_now() functions)
void loop() {
display.clearDisplay();
print_time();
}
void print_time() {
struct tm timeinfo;
getLocalTime(&timeinfo);
char full_time[9];
strftime(full_time, 9, "%H:%M:%S", &timeinfo);
print_line(full_time, 15,0,2);
}
*/