#include <RTClib.h>
#include <Wire.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>
#define SCREEN_WIDTH 128 // OLED width, in pixels
#define SCREEN_HEIGHT 64 // OLED height, in pixels
// create an OLED display object connected to I2C
Adafruit_SSD1306 oled(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, -1);
RTC_DS1307 RTC;
int hourupg;
int minupg;
int secupg;
int yearupg;
int monthupg;
int dayupg;
int hrupg;
int mnupg;
void setup () {
Serial.begin(9600);
RTC.begin();
if (! RTC.isrunning()) {
//Serial.println("RTC is NOT running!");
RTC.adjust(DateTime(__DATE__, __TIME__));// Set the date and time at compile time
}
// automatically sets the RTC to the date & time on PC this sketch was compiled
RTC.adjust(DateTime(F(__DATE__), F(__TIME__)));
// initialize OLED display with I2C address 0x3C
if (!oled.begin(SSD1306_SWITCHCAPVCC, 0x3C)) {
Serial.println(F("failed to start SSD1306 OLED"));
while (1);
}
delay(2000); // wait two seconds for initializing
oled.clearDisplay(); // clear display
}
void loop () {
DateTime now = RTC.now();
Serial.println("ESP32 RTC Date Time: ");
displayDateTime ();
oled.setTextSize(1); // set text size
oled.setTextColor(WHITE); // set text color
oled.setCursor(0, 2); // set position to display (x,y)
oled.println("Hello World"); // set text
oled.display(); // display on OLED
delay(1000); // delay 1 seconds
}
/*===================================================================================================*/
void displayDateTime () { // //display the current date and time //
DateTime now = RTC.now();
Serial.print(now.hour(), DEC);
hourupg = now.hour();
Serial.print(":");
Serial.print(now.minute(), DEC);
minupg = now.minute();
Serial.print(":");
Serial.print(now.second(), DEC);
secupg = now.second();
Serial.println(now.month(), DEC);
monthupg=now.month();
Serial.print("/");
Serial.print(now.day(), DEC); ////display Day
dayupg = now.day();
Serial.print("/");
Serial.println(now.year(), DEC); ////display Year
yearupg = now.year();
Serial.print("");
} // EOF void //displayDateTime
/*----------------------------------------------------------------------------------------------------------------------------*/