Code: #include <Arduino.h>
#include <NTPClient.h>
#include <RTC.h> // Replace with your specific RTC library header
#include <WiFiS3.h>
RTCTime currentTime;
String formattedTime;
const char* ssid = "you know what goes here";
const char* password = "and here";
// NTP server details
const char* ntpServer = "pool.ntp.org";
const long gmtOffset_sec = 3600; // Adjust based on your time zone offset in seconds
// Create instances for communication
WiFiUDP ntpUDP;
NTPClient timeClient(ntpUDP);
void setup() {
Serial.begin(115200);
RTC.begin();
RTCTime startTime(30, Month::JUNE, 2023, 13, 37, 00, DayOfWeek::WEDNESDAY, SaveLight::SAVING_TIME_ACTIVE);
RTC.setTime(startTime);
// Connect to WiFi network
Serial.print("Connecting to ");
Serial.println(ssid);
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
}
Serial.println("");
Serial.println("WiFi connected.");
Serial.print("IP address: ");
Serial.println(WiFi.localIP());
// Initialize NTP client
timeClient.begin();
timeClient.setTimeOffset(gmtOffset_sec);
}
void loop() {
// Check for NTP update
while (!timeClient.update()) {
timeClient.forceUpdate();
}
// Get formatted time from NTP
formattedTime = timeClient.getFormattedTime();
Serial.println(formattedTime);
// Assuming formattedTime is in the format "YYYY-MM-DDTHH:MM:SSZ"
int year = formattedTime.substring(0, 4).toInt();Serial.println(year);
int month = formattedTime.substring(5, 7).toInt();Serial.println(month);
int day = formattedTime.substring(8, 10).toInt();Serial.println(day);
int hour = formattedTime.substring(11, 13).toInt();
int minute = formattedTime.substring(14, 16).toInt();
int second = formattedTime.substring(17, 19).toInt();
RTCTime startTime(day, Month::JANUARY, year, hour, minute, second, DayOfWeek::TUESDAY, SaveLight::SAVING_TIME_INACTIVE);
RTC.setTime(startTime);
RTCTime currentTime;
RTC.getTime(currentTime);
Serial.print(currentTime.getHour()); // Replace with actual method name
Serial.print("/");
Serial.print(currentTime.getMinutes()); // Replace with actual method name
Serial.print("/");
Serial.print(currentTime.getYear()); // Replace with actual method name
Serial.print(" ");
Serial.print(currentTime.getHour()); // Replace with actual method name
Serial.print(":");
Serial.print(currentTime.getMinutes()); // Replace with actual method name
Serial.print(":");
Serial.println(currentTime.getSeconds()); // Replace with actual method name
delay(10000); // Update time every 10 seconds
}