#include <WiFi.h>
#include <time.h>
// Replace with your network credentials
const char* ssid = "Wokwi-GUEST",
* password = "";
// Timezone offset (in seconds) and daylight saving time offset
const int gmtOffset_sec = -10800, // Replace with your GMT offset (e.g., -18000 for GMT-5)
daylightOffset_sec = 0; // Replace with your daylight saving time offset
void setup()
{
Serial.begin(115200);
// Connect to Wi-Fi
Serial.print("Connecting to Wi-Fi");
WiFi.begin(ssid, password, 6);
while (WiFi.status() != WL_CONNECTED)
{
delay(500);
Serial.print(".");
}
Serial.println(" Connected!");
// Initialize and get the time
configTime(gmtOffset_sec, daylightOffset_sec, "pool.ntp.org", "time.nist.gov");
printLocalTime();
}
void loop()
{
// Print time periodically
delay(10000);
printLocalTime();
}
void printLocalTime()
{
struct tm timeinfo;
if (!getLocalTime(&timeinfo))
{
Serial.println("Failed to obtain time");
return;
}
Serial.println(&timeinfo, "%A(%w), %B %d %Y %H:%M:%S"); // Example: Tuesday, September 12 2023 14:30:00
}