#include <WiFi.h>
#include "time.h"
const char* ssid = "Wokwi-GUEST";
const char* password = "";
const char* ntpServer = "pool.ntp.org";
const long gmtOffset_sec = 7 * 3600; // Offset GMT+7 untuk Malang
const int daylightOffset_sec = 0;
void printLocalTime() {
struct tm timeinfo;
if (!getLocalTime(&timeinfo)) {
Serial.println("Failed to obtain time");
return;
}
// Print waktu lokal di Malang
Serial.print("Malang, ");
Serial.println(&timeinfo, "%B %d, %Y %I:%M:%S %p");
// Print waktu EPOCH
time_t epochTime = mktime(&timeinfo);
Serial.println(epochTime);
Serial.println( );
}
void setup() {
Serial.begin(115200);
Serial.print("Connecting to WiFi");
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED) {
delay(100);
Serial.print(".");
}
Serial.println(" Connected!");
// Menghubungkan ke NTP server dengan offset GMT+7 untuk Malang
configTime(gmtOffset_sec, daylightOffset_sec, ntpServer);
printLocalTime();
}
void loop() {
delay(10000);
printLocalTime();
}