-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathsketch_esp32.ino
More file actions
134 lines (114 loc) · 3.42 KB
/
Copy pathsketch_esp32.ino
File metadata and controls
134 lines (114 loc) · 3.42 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
#include <WiFi.h>
#include <WebServer.h>
#include "LittleFS.h"
// Wi-Fi credentials
const char* ssid = "{Your Wi-fi SSID}";
const char* password = "{Your Wi-Fi Password}";
// GPIO setup
const int relayPin = 18; // Relay IN (#Active LOW)
const int statusLedPin = 5; // System status LED = ON always
const int relayLedPin = 4; // NEW: LED indicator for relay ON
const int buttonPin = 12; // Button to GND
// Button debounce
bool lastButtonState = HIGH;
bool buttonPressed = false;
unsigned long lastDebounceTime = 0;
const unsigned long debounceDelay = 50;
// Relay timing
bool relayActive = false;
unsigned long relayStartTime = 0;
WebServer server(80);
// ===== Relay Trigger (1s pulse) =====
void triggerRelay() {
Serial.println("Triggering relay...");
digitalWrite(relayPin, LOW); // Relay ON
digitalWrite(relayLedPin, HIGH); // LED ON (active HIGH)
relayStartTime = millis();
relayActive = true;
}
// ===== Serve Static Files =====
void serveFile(const char* path, const char* type) {
if (LittleFS.exists(path)) {
File file = LittleFS.open(path, "r");
server.streamFile(file, type);
file.close();
} else {
server.send(404, "text/plain", "File Not Found");
}
}
void setup() {
Serial.begin(115200);
// Pin modes
pinMode(relayPin, OUTPUT);
pinMode(statusLedPin, OUTPUT);
pinMode(relayLedPin, OUTPUT); // NEW
pinMode(buttonPin, INPUT_PULLUP);
// Initial states
digitalWrite(relayPin, HIGH); // Relay OFF
digitalWrite(statusLedPin, HIGH); // System LED ON
digitalWrite(relayLedPin, LOW); // Relay LED OFF
// Wi-Fi connect
WiFi.begin(ssid, password);
Serial.print("Connecting to WiFi");
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
}
Serial.println("\nConnected!");
Serial.print("IP Address: ");
Serial.println(WiFi.localIP());
// LittleFS init
if (!LittleFS.begin(true)) {
Serial.println("LittleFS mount failed!");
return;
}
// === Web Routes ===
server.on("/", []() {
serveFile("/index.html", "text/html");
});
server.on("/pc.html", []() {
serveFile("/pc.html", "text/html");
});
server.on("/style.css", []() {
serveFile("/style.css", "text/css");
});
server.on("/trigger", HTTP_POST, []() {
triggerRelay();
server.sendHeader("Access-Control-Allow-Origin", "*");
server.send(200, "text/plain", "Relay triggered!");
});
server.on("/trigger", HTTP_OPTIONS, []() {
server.sendHeader("Access-Control-Allow-Origin", "*");
server.sendHeader("Access-Control-Allow-Methods", "POST, OPTIONS");
server.sendHeader("Access-Control-Allow-Headers", "Content-Type");
server.send(204);
});
server.begin();
Serial.println("Web server started.");
}
void loop() {
server.handleClient();
// Button debounce
bool reading = digitalRead(buttonPin);
if (reading != lastButtonState) {
lastDebounceTime = millis();
}
if ((millis() - lastDebounceTime) > debounceDelay) {
if (reading == LOW && !buttonPressed) {
Serial.println("Physical button press detected.");
triggerRelay();
buttonPressed = true;
}
if (reading == HIGH) {
buttonPressed = false;
}
}
lastButtonState = reading;
// Relay auto-release after 1 second
if (relayActive && millis() - relayStartTime >= 1000) {
digitalWrite(relayPin, HIGH); // Relay OFF
digitalWrite(relayLedPin, LOW); // LED OFF
relayActive = false;
Serial.println("Relay released.");
}
}