-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathProcess_FaceDetection_Esp32Pre_Final_SmartLightningAutomation.ino
168 lines (141 loc) · 5.24 KB
/
Process_FaceDetection_Esp32Pre_Final_SmartLightningAutomation.ino
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
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
#include <WiFi.h>
#include <WebServer.h>
// Replace with your network credentials
const char* ssid = "IDEA_WIFI_2";
const char* password = "ideawifi2";
WebServer server(80);
bool faceDetectionEnabled = false;
bool irSensorEnabled = true;
bool ledState = false;
bool manualControl = false;
bool bothEnabled = false;
const int ledPin = 4;
const int irSensorPin = 14;
const int buzzerPin = 5; // Pin for the buzzer
bool lastLedState = false;
void handleRoot();
void handleFaceDetectionControl();
void handleIrSensorControl();
void handleLedControl();
void handleBothControl();
void handleStatusCheck();
void setup() {
Serial.begin(115200);
pinMode(ledPin, OUTPUT);
pinMode(irSensorPin, INPUT);
pinMode(buzzerPin, OUTPUT); // Set the buzzer pin as output
WiFi.begin(ssid, password);
Serial.print("Connecting to Wi-Fi");
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
}
Serial.println();
Serial.println("Connected to Wi-Fi");
Serial.print("IP Address: ");
Serial.println(WiFi.localIP());
server.on("/", HTTP_GET, handleRoot);
server.on("/face", HTTP_GET, handleFaceDetectionControl);
server.on("/ir", HTTP_GET, handleIrSensorControl);
server.on("/led", HTTP_GET, handleLedControl);
server.on("/both", HTTP_GET, handleBothControl);
server.on("/status", HTTP_GET, handleStatusCheck);
server.begin();
}
void loop() {
server.handleClient();
bool irSensorState = digitalRead(irSensorPin);
bool faceDetected = faceDetectionEnabled ? getFaceDetectionStatus() : false;
if (manualControl) {
updateLedState(ledState);
} else if (bothEnabled) {
if (irSensorState == LOW || faceDetected) {
updateLedState(true);
} else {
updateLedState(false);
}
} else if (faceDetectionEnabled) {
updateLedState(faceDetected);
} else if (irSensorEnabled) {
updateLedState(irSensorState == LOW);
}
}
bool getFaceDetectionStatus() {
// Simulate face detection status for demo purposes
// You should replace this with actual status retrieval logic
return false; // Replace with actual status retrieval from the Python script
}
void updateLedState(bool state) {
if (state && !lastLedState) {
// LED just turned on, make the buzzer beep for one second
digitalWrite(buzzerPin, HIGH);
delay(200); // Beep for one second
digitalWrite(buzzerPin, LOW);
}
digitalWrite(ledPin, state ? HIGH : LOW);
lastLedState = state; // Update the last LED state
}
void handleRoot() {
String html = "<!DOCTYPE html><html><head><title>Smart Lighting Automation</title>";
html += "<style>";
html += "body { font-family: Arial, sans-serif; background: linear-gradient(45deg, #e3f2fd, #90caf9); height: 100vh; display: flex; justify-content: center; align-items: center; margin: 0; }";
html += ".container { text-align: center; }";
html += "h1 { color: #ffffff; font-size: 48px; }";
html += ".button { display: inline-block; padding: 15px 25px; font-size: 20px; color: white; background-color: #4CAF50; text-decoration: none; border-radius: 10px; margin: 10px; }";
html += "</style>";
html += "</head><body>";
html += "<div class='container'>";
html += "<h1>Smart Lighting Automation</h1>";
html += "<a href=\"/face?enable=1\" class='button'>Enable Face Detection</a>";
html += "<a href=\"/face?enable=0\" class='button'>Disable Face Detection</a>";
html += "<a href=\"/ir?enable=1\" class='button'>Enable Motion Sensor</a>";
html += "<a href=\"/ir?enable=0\" class='button'>Disable Motion Sensor</a>";
html += "<a href=\"/led?state=1\" class='button'>Turn LED On</a>";
html += "<a href=\"/led?state=0\" class='button'>Turn LED Off</a>";
html += "<a href=\"/both?enable=1\" class='button'>Enable Both Sensors</a>";
html += "<a href=\"/both?enable=0\" class='button'>Disable Both Sensors</a>";
html += "<a href=\"/status\" class='button'>Check Status</a>";
html += "</div>";
html += "</body></html>";
server.send(200, "text/html", html);
}
void handleFaceDetectionControl() {
if (server.hasArg("enable")) {
faceDetectionEnabled = server.arg("enable") == "1";
}
bothEnabled = false;
manualControl = false;
server.send(200, "text/plain", faceDetectionEnabled ? "Face Detection Enabled" : "Face Detection Disabled");
}
void handleIrSensorControl() {
if (server.hasArg("enable")) {
irSensorEnabled = server.arg("enable") == "1";
}
bothEnabled = false;
manualControl = false;
server.send(200, "text/plain", irSensorEnabled ? "Motion Sensor Enabled" : "Motion Sensor Disabled");
}
void handleLedControl() {
if (server.hasArg("state")) {
ledState = server.arg("state") == "1";
manualControl = true;
updateLedState(ledState);
}
server.send(200, "text/plain", ledState ? "LED On" : "LED Off");
}
void handleBothControl() {
if (server.hasArg("enable")) {
bothEnabled = server.arg("enable") == "1";
faceDetectionEnabled = bothEnabled;
irSensorEnabled = bothEnabled;
}
manualControl = false;
server.send(200, "text/plain", bothEnabled ? "Both Sensors Enabled" : "Both Sensors Disabled");
}
void handleStatusCheck() {
String status = "Face Detection is ";
status += faceDetectionEnabled ? "Enabled" : "Disabled";
status += "\nMotion Sensor is ";
status += irSensorEnabled ? "Enabled" : "Disabled";
server.send(200, "text/plain", status);
}