// Time variables
const char* ntpServer = "pool.ntp.org";
const long gmtOffset_sec = 28800;
const int daylightOffset_sec = 0;
struct tm timeinfo;
// Pin definitions
const int LED_PIN = 4;
const int BUTTON_PIN = 3;
// State variables
int ledState = LOW;
int buttonState;
int lastButtonState = LOW;
unsigned long lastDebounceTime = 0;
unsigned long debounceDelay = 50;
// Schedule variables
bool isEnabled = true;
int onHour = 8;
int onMinute = 0;
int offHour = 18;
int offMinute = 0;
void setup() {
// Initialize serial communication
Serial.begin(9600);
// Configure LED and button pins
pinMode(LED_PIN, OUTPUT);
pinMode(BUTTON_PIN, INPUT);
// Set up WiFiManager
wifiManager.autoConnect("AutoConnectAP");
// Initialize time
configTime(gmtOffset_sec, daylightOffset_sec, ntpServer);
if (!getLocalTime(&timeinfo)) {
Serial.println("Failed to obtain time");
}
// Set up web server
server.on("/", handleRoot);
server.on("/schedule", handleSchedule);
server.begin();
// Turn on LED if current time is within schedule
if (isEnabled && isWithinSchedule(timeinfo)) {
ledState = HIGH;
}
}
void loop() {
// Update time
if (!getLocalTime(&timeinfo)) {
Serial.println("Failed to obtain time");
}
// Handle button
int reading = digitalRead(BUTTON_PIN);
if (reading != lastButtonState) {
lastDebounceTime = millis();
}
if ((millis() -
lastDebounceTime) > debounceDelay) {
if (reading != buttonState) {
buttonState = reading;
if (buttonState == HIGH) {
// Toggle schedule on/off
isEnabled = !isEnabled;
if (isEnabled) {
// Turn on LED if current time is within schedule
if (isWithinSchedule(timeinfo)) {
ledState = HIGH;
}
} else {
// Turn off LED
ledState = LOW;
}
}
}
}
lastButtonState = reading;
// Update LED state if necessary
if (isEnabled && isWithinSchedule(timeinfo)) {
ledState = HIGH;
} else {
ledState = LOW;
}
// Update LED
digitalWrite(LED_PIN, ledState);
// Handle web server requests
server.handleClient();
}
bool isWithinSchedule(struct tm t) {
int h = t.tm_hour;
int m = t.tm_min;
if ((h > onHour || (h == onHour && m >= onMinute)) && (h < offHour || (h == offHour && m < offMinute))) {
return true;
} else {
return false;
}
}
void handleRoot() {
String html = "<html><body>";
html += "<h1>ESP32-D2WD Timer</h1>";
html += "<p>Current time: " + getTimeString(timeinfo) + "</p>";
html += "<p>Schedule is " + (isEnabled ? "enabled" : "disabled") + "</p>";
html += "<p>LED is " + (ledState == HIGH ? "on" : "off") + "</p>";
html += "<p><a href='/schedule'>Edit schedule</a></p>";
html += "</body></html>";
server.send(200, "text/html", html);
}
void handleSchedule() {
String html = "<html><body>";
html += "<h1>Edit Schedule</h1>";
html += "<form method='post' action='/schedule'>";
html += "<p>On time: <input type='text' name='onHour' value='" + String(onHour) + "'>:<input type='text' name='onMinute' value='" + String(onMinute) + "'></p>";
html += "<p>Off time: <input type='text' name='offHour' value='" + String(offHour) + "'>:<input type='text' name='offMinute' value='" + String(offMinute) + "'></p>";
html += "<p><input type='submit' value='Save'></p>";
html += "</form>";
html += "</body></html>";
if (server.method() == HTTP_POST) {
onHour = server.arg("onHour").toInt();
onMinute = server.arg("onMinute").toInt();
offHour = server.arg("offHour").toInt();
offMinute = server.arg("offMinute").toInt();
}
server.send(200, "text/html", html);
}
String getTimeString(struct tm t) {
String s = "";
s += (t.tm_hour < 10 ? "0" : "") + String(t.tm_hour);
s += ":";
s += (t.tm_min < 10 ? "0" : "") + String(t.tm_min);
s += ":";
s += (t.tm_sec < 10 ? "0" : "") + String(t.tm_sec);
return s;
}