#include <WiFi.h>
#include <time.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>
#define SCREEN_WIDTH 128 // OLED display width, in pixels
#define SCREEN_HEIGHT 64 // OLED display height, in pixels
// Declaration for an SSD1306 display connected to I2C (SDA, SCL pins)
#define OLED_RESET -1
Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, OLED_RESET);
// Replace with your network credentials
const char* ssid = "Wokwi-GUEST";
const char* password = "";
// Set the time (year, month, day, hour, minute, second) in UTC
const tm timeInfo = {
2024 - 1900, // year
1 - 1, // month (0 - 11)
22, // day
12, // hour
0, // minute
0 // second
};
void printLocalTime() {
struct tm timeinfo;
if (!getLocalTime(&timeinfo)) {
Serial.println("Failed to obtain time");
return;
}
display.clearDisplay();
display.setTextSize(1);
display.setTextColor(SSD1306_WHITE);
display.setCursor(0, 10); // Set the cursor position
display.print("Time: ");
display.print(timeinfo.tm_hour);
display.print(":");
display.print(timeinfo.tm_min);
display.print(":");
display.println(timeinfo.tm_sec);
display.setCursor(0, 30); // Set the cursor position
display.print("Date: ");
display.print(timeinfo.tm_mday);
display.print("/");
display.print(timeinfo.tm_mon + 1);
display.print("/");
display.println(timeinfo.tm_year + 1900);
display.setCursor(0, 50);
display.println("Credit:-Praween soni");
display.display(); // Display the content
}
void setup() {
// Start serial communication
Serial.begin(115200);
// Initialize the OLED display
if(!display.begin(SSD1306_SWITCHCAPVCC, 0x3C)) {
Serial.println(F("SSD1306 allocation failed"));
for(;;);
}
// Connect to Wi-Fi
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED) {
delay(250);
Serial.print('.');
}
Serial.println();
// Set the system time using configTzTime
configTzTime("IST-5:30", "pool.ntp.org");
Serial.println("Waiting for time to be set...");
while (time(nullptr) < 1510644967) {
delay(500);
Serial.print('.');
}
Serial.println();
Serial.println("Time set successfully");
}
void loop() {
// Print the current time on the OLED display
printLocalTime();
// Add your code here to perform actions based on the current time
// Delay for 1 second
delay(1000);
}
Loading
ssd1306
ssd1306