-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathservodoorwifi.ino
201 lines (180 loc) · 7.19 KB
/
servodoorwifi.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
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
#include <WiFi.h>
#include <ESPAsyncWebServer.h>
#include <ESP32Servo.h>
// Wi-Fi credentials
const char* ssid = "Brav";
const char* password = "abcd0987";
// Pin definition
const int servoPin = 33;
// Constants
const int SERVO_OPEN_ANGLE = 90; // Angle when door is open
const int SERVO_CLOSED_ANGLE = 0; // Angle when door is closed
// Initialize objects
AsyncWebServer server(80);
Servo myServo;
// State variable
volatile bool isDoorOpen = false;
void setup() {
Serial.begin(115200);
// Initialize servo in closed position
myServo.attach(servoPin);
myServo.write(SERVO_CLOSED_ANGLE);
isDoorOpen = false;
Serial.println("Servo initialized to closed position");
// Connect to Wi-Fi
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
}
Serial.println("\nConnected to WiFi!");
Serial.print("IP Address: ");
Serial.println(WiFi.localIP());
// Serve the web page
server.on("/", HTTP_GET, [](AsyncWebServerRequest *request) {
String html = R"rawliteral(
<!DOCTYPE html>
<html>
<head>
<title>Door Control</title>
<meta name="viewport" content="width=device-width, initial-scale=1">
<style>
body {
font-family: Arial, sans-serif;
margin: 20px;
text-align: center;
}
.container {
max-width: 600px;
margin: 0 auto;
padding: 20px;
box-shadow: 0 0 10px rgba(0,0,0,0.1);
border-radius: 10px;
}
.status {
padding: 20px;
margin: 20px 0;
border-radius: 5px;
font-size: 1.2em;
font-weight: bold;
}
.open {
background-color: #d4edda;
color: #155724;
}
.closed {
background-color: #f8f9fa;
color: #383d41;
}
.button {
padding: 15px 30px;
font-size: 1.2em;
margin: 10px;
border: none;
border-radius: 5px;
cursor: pointer;
transition: background-color 0.3s;
}
.open-btn {
background-color: #28a745;
color: white;
}
.close-btn {
background-color: #dc3545;
color: white;
}
.button:hover {
opacity: 0.9;
}
.button:disabled {
opacity: 0.5;
cursor: not-allowed;
}
</style>
</head>
<body>
<div class="container">
<h1>Automatic Door Control</h1>
<div id="doorStatus" class="status closed">Door Status: Closed</div>
<div>
<button id="openBtn" class="button open-btn" onclick="controlDoor('open')">Open Door</button>
<button id="closeBtn" class="button close-btn" onclick="controlDoor('close')">Close Door</button>
</div>
</div>
<script>
let isProcessing = false;
function updateButtonStates(doorState) {
const openBtn = document.getElementById('openBtn');
const closeBtn = document.getElementById('closeBtn');
openBtn.disabled = doorState === 'open' || isProcessing;
closeBtn.disabled = doorState === 'closed' || isProcessing;
}
function updateStatus() {
fetch('/status')
.then(response => response.json())
.then(data => {
const status = data.isDoorOpen ? 'Open' : 'Closed';
const statusDiv = document.getElementById('doorStatus');
statusDiv.innerText = 'Door Status: ' + status;
statusDiv.className = 'status ' + (data.isDoorOpen ? 'open' : 'closed');
updateButtonStates(status.toLowerCase());
});
}
function controlDoor(action) {
if (isProcessing) return;
isProcessing = true;
updateButtonStates();
fetch('/' + action)
.then(response => response.text())
.then(data => {
console.log(data);
setTimeout(() => {
isProcessing = false;
updateStatus();
}, 1000);
})
.catch(error => {
console.error('Error:', error);
isProcessing = false;
updateStatus();
});
}
// Update status every second
setInterval(updateStatus, 1000);
// Initial status update
updateStatus();
</script>
</body>
</html>
)rawliteral";
request->send(200, "text/html", html);
});
// Handle open door request
server.on("/open", HTTP_GET, [](AsyncWebServerRequest *request) {
if (!isDoorOpen) {
myServo.write(SERVO_OPEN_ANGLE);
isDoorOpen = true;
Serial.println("Door opened");
}
request->send(200, "text/plain", "Door opened");
});
// Handle close door request
server.on("/close", HTTP_GET, [](AsyncWebServerRequest *request) {
if (isDoorOpen) {
myServo.write(SERVO_CLOSED_ANGLE);
isDoorOpen = false;
Serial.println("Door closed");
}
request->send(200, "text/plain", "Door closed");
});
// Status endpoint
server.on("/status", HTTP_GET, [](AsyncWebServerRequest *request) {
String json = "{\"isDoorOpen\":" + String(isDoorOpen ? "true" : "false") + "}";
request->send(200, "application/json", json);
});
server.begin();
}
void loop() {
// Main loop is empty as everything is handled by the web server
delay(100);
}