#include <WiFi.h>
#include <time.h>
const char *ssid = "Wokwi-GUEST";
const char *password = "";
const char *ntpServer = "pool.ntp.org";
const long gmtOffset_sec = 3600;
const int daylightOffset_sec = 3600;
const int relayPin = 12; // Change this to the pin connected to your relay or switch
const int onHour = 07; // Change this to the desired hour to turn on the light
const int onMinute = 34; // Change this to the desired minute to turn on the light
const int offHour = 14; // Change this to the desired hour to turn off the light
const int offMinute = 02; // Change this to the desired minute to turn off the light
const int Night_1[1][1] = {05, 00, 06, 00} // {Hour ON, Minute ON, Hour OFF, Minute OFF}
const int Daybreak_1[1][1] = {06, 00, 07, 00} // {Hour ON, Minute ON, Hour OFF, Minute OFF}
const int Day_1[1][1] = {07, 00, 09, 00} // {Hour ON, Minute ON, Hour OFF, Minute OFF}
const int Day_2[1][1] = {17, 00, 20, 00} // {Hour ON, Minute ON, Hour OFF, Minute OFF}
const int Daybreak_2[1][1] = {20, 00, 21, 00} // {Hour ON, Minute ON, Hour OFF, Minute OFF}
const int Night_2[1][1] = {21, 00, 23, 00} // {Hour ON, Minute ON, Hour OFF, Minute OFF}
const int luc_status = 0; // Initial state of the light
const int matrix[4][4] = {0, 1, 2, 3, 4, 0, 1, 2, 4, 2, 0, 1, 4, 1, 2, 0};
void connectToWiFi() {
Serial.println("Connecting to WiFi");
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED) {
delay(1000);
Serial.println("Connecting...");
}
Serial.println("Connected to WiFi");
}
void syncTime() {
configTime(gmtOffset_sec, daylightOffset_sec, ntpServer);
Serial.println("Waiting for time synchronization");
while (time(nullptr) < 1510644967) {
delay(500);
Serial.println("Synchronizing time...");
}
Serial.println("Time synchronized");
}
void printmatrix() {
for (byte i = 0; i < 4; i = i + 1) {
Serial.println();
for (byte j = 0; j < 4; j = j + 1) {
Serial.print(matrix[i][j]);
}
}
}
void setup() {
Serial.begin(115200);
pinMode(relayPin, OUTPUT);
connectToWiFi();
syncTime();
//printmatrix();
}
void loop() {
struct tm timeinfo;
if (!getLocalTime(&timeinfo)) {
Serial.println("Failed to obtain time");
return;
}
//Serial.println(&timeinfo, "%A, %B %d %Y %H:%M:%S"); // Print current time
int currentHour = timeinfo.tm_hour;
int currentMinute = timeinfo.tm_min;
// Check if it's time to turn on the light
if (currentHour == onHour && currentMinute == onMinute) {
digitalWrite(relayPin, HIGH); // Turn on the light
//Serial.println("Light's ON!");
else if
}
// Check if it's time to turn off the light
if (currentHour == offHour && currentMinute == offMinute) {
digitalWrite(relayPin, LOW); // Turn off the light
//Serial.println("Light's OFF!");
}
// You can add additional logic or conditions as needed
delay(1000); // Wait for 1 second before checking again
}