#include <WiFi.h>
#include "time.h"
// WiFi credentials
const char* ssid = "Wokwi-GUEST";
const char* password = "";
// NTP server details
const char* ntpServer = "pool.ntp.org";
const long gmtOffset_sec = 8 * 3600; // GMT+8 for MY Standard Time
const int daylightOffset_sec = 0; // No daylight saving time in MY
bool beforeSchool = false;
bool afterSchool = false;
void setup() {
Serial.begin(115200);
// Connect to Wi-Fi
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED) {
delay(1000);
Serial.println("Connecting to WiFi...");
}
Serial.println("Connected to WiFi");
// Initialize NTP
configTime(gmtOffset_sec, daylightOffset_sec, ntpServer);
// Wait for time to be set
struct tm timeinfo;
if (!getLocalTime(&timeinfo)) {
Serial.println("Failed to obtain time");
return;
}
// Print current time
Serial.println(&timeinfo, "Current time: %Y-%m-%d %H:%M:%S");
}
void loop() {
// Get current time
struct tm timeinfo;
if (!getLocalTime(&timeinfo)) {
Serial.println("Failed to obtain time");
return;
}
// Print current time
Serial.println(&timeinfo, "Current time: %Y-%m-%d %H:%M:%S");
// Check if time is after 3:40 PM
if (timeinfo.tm_hour > 15 || (timeinfo.tm_hour == 15 && timeinfo.tm_min >= 40)) {
Serial.println("Current time is after 3:40 PM");
afterSchool = true;
} else {
Serial.println("Current time is before 3:40 PM");
afterSchool = false;
}
// Check if time is before 7:30 AM
if (timeinfo.tm_hour < 7 || (timeinfo.tm_hour == 7 && timeinfo.tm_min < 30)) {
Serial.println("Current time is before 7:30 AM");
beforeSchool = true;
} else {
Serial.println("Current time is after 7:30 AM");
beforeSchool = false;
}
// Wait for a minute before checking again
delay(60000);
}