#include <WiFi.h>
#include <NTPClient.h>
#include <WiFiUdp.h>
// WiFi credentials
const char* ssid = "Wokwi-GUEST";
const char* password = "";
// NTP Client setup
WiFiUDP ntpUDP;
NTPClient timeClient(ntpUDP, "pool.ntp.org", 25200, 3600000); // UTC, update every hour
unsigned long lastNTPSync = 0;
unsigned long messageCount = 0;
void setup() {
// Initialize serial communication
Serial.begin(115200);
// Connect to WiFi
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
}
Serial.println("\nWiFi connected");
// Initialize NTP Client
timeClient.begin();
timeClient.update();
}
void loop() {
// Sync time every hour
if (millis() - lastNTPSync >= 3600000) {
timeClient.update();
lastNTPSync = millis();
Serial.println("NTP time synced");
}
// Send serial message every second
static unsigned long lastMessage = 0;
if (millis() - lastMessage >= 1000) {
// Get current time
time_t epochTime = timeClient.getEpochTime();
struct tm *ptm = gmtime(&epochTime);
// Format message with current time and message count
char message[100];
snprintf(message, sizeof(message),
"Message %lu: %02d:%02d:%02d %02d/%02d/%04d",
messageCount++,
ptm->tm_hour, ptm->tm_min, ptm->tm_sec,
ptm->tm_mday, ptm->tm_mon + 1, ptm->tm_year + 1900
);
Serial.println(message);
lastMessage = millis();
}
delay(100);
}
//slow af