#include <WiFi.h>
#include <Wire.h>
#include <TM1637Display.h> //https://github.com/avishorp/TM1637
#include "RTClib.h"
#include "time.h"
#define CLK 27
#define DIO 14
TM1637Display display(CLK, DIO);
RTC_DS3231 rtc;
#define NTP_SERVER "pool.ntp.org"
#define gmtOffset_sec 7 * 3600 //offset in second
#define daylightOffset_sec 0 //offset in second
DateTime now;
#define SSID "Wokwi-GUEST" //Change with your SSID
#define Password "" //change with your password
char datetime[20];
byte dayPrevious, dayNow;
// Create an array that sets individual segments per digit to display the word "dOnE"
const uint8_t Error[] = {
SEG_A | SEG_D | SEG_E | SEG_F | SEG_G, // E
SEG_E | SEG_G, // r
SEG_E | SEG_G, // r
SEG_E | SEG_G | SEG_C | SEG_D // o
};
void setup() {
// put your setup code here, to run once:
Serial.begin(115200);
Serial.println("Time With NTP, RTC, and TM1637 Display");
// Set the display brightness (0-7)
display.setBrightness(5);
// Clear the display
display.clear();
// Check if RTC is connected correctly
if (! rtc.begin()) {
Serial.println("Couldn't find RTC");
display.setSegments(Error);
while (1) {
delay(1000);
}
}
// Check if the RTC lost power and if so, set the time > JIKA RTC HILANG POWER ATAU WAKTUNYA ERROR MAKA
//KITA AKAN KONEKSIKAN KE NTP.
if (!rtc.lostPower()) { //SEBAGAI PERCOBAAN KITA PAKAI TANDA ! AGAR BISA TEREKSEKUSI PERNYATAAN IF INI
Serial.println("RTC lost power, lets set the time from NTP!");
// Set WIFI
WiFi.begin(SSID, Password);
Serial.println("CONNECTING TO WIFI!");
byte i = 0;
while (WiFi.status() != WL_CONNECTED) {
delay(250);
Serial.print('.');
display.showNumberDecEx(0, (0x80 >> i), true);
if (i == 3) {
display.clear();
i = 0;
}
}
Serial.println('#');
Serial.println("Updating time...");
setTimeNTP();
}
now = rtc.now();
dayPrevious = now.day();
Serial.println("Done");
}
void loop() {
// Get current date and time
now = rtc.now();
dayNow = now.day();
sprintf(datetime, "%02d/%02d/%d %02d:%02d:%02d", now.day(), now.month(), now.year(), now.hour(), now.minute(), now.second());
Serial.println(datetime);
// Create time format to display
int displaytime = (now.hour() * 100) + now.minute();
// Display the current time in 24 hour format with leading zeros and a center colon enabled
display.showNumberDecEx(displaytime, 0b11100000, true);
//Update time from NTP every midnight.
//For Example we use minute for day
if (dayNow != dayPrevious) {
setTimeNTP();
dayPrevious=dayNow;
}
delay(1000);
}
void setTimeNTP() {
// Set WIFI
WiFi.begin(SSID, Password);
Serial.println("CONNECTING TO WIFI!");
byte i = 0;
while (WiFi.status() != WL_CONNECTED) {
delay(250);
Serial.print('.');
}
Serial.println('#');
Serial.println("Updating time...");
configTime(gmtOffset_sec, daylightOffset_sec, NTP_SERVER);
struct tm timeinfo;
getLocalTime(&timeinfo);
byte jam = timeinfo.tm_hour;
byte menit = timeinfo.tm_min;
byte detik = timeinfo.tm_sec;
byte hari = timeinfo.tm_mday;
byte bulan = timeinfo.tm_mon + 1;
int tahun = timeinfo.tm_year + 1900;
sprintf(datetime, "%02d/%02d/%d %02d:%02d:%02d", hari, bulan, tahun, jam, menit, detik);
Serial.println(datetime);
rtc.adjust(DateTime(tahun, bulan, hari, jam, menit, detik));
//disconnect WiFi as it's no longer needed
WiFi.disconnect(true);
WiFi.mode(WIFI_OFF);
}