-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathDIY_Door_Opener_Code.ino
More file actions
159 lines (132 loc) · 4.1 KB
/
DIY_Door_Opener_Code.ino
File metadata and controls
159 lines (132 loc) · 4.1 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
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
#include <ESP8266WiFi.h>
#include <ESP8266WebServer.h>
#include <DNSServer.h>
#include <Servo.h>
// --------- AP (your hotspot) ----------
const char* AP_SSID = "Dr. Maker";
const char* AP_PASS = "XXXXXXXXXXXXXX";
// --------- STA (your home/office WiFi) ----------
const char* STA_SSID = "SSID";
const char* STA_PASS = "XXXXXXXXXXXXXXXXXX";
ESP8266WebServer server(80);
DNSServer dnsServer;
const byte DNS_PORT = 53;
Servo servo;
const int SERVO_PIN = 5; // GPIO5 = D1
// ---- OPEN action ----
void doServoOpen() {
servo.attach(5);
servo.write(0);
delay(1500);
servo.detach();
delay(100);
servo.attach(5);
servo.write(180);
delay(250);
servo.detach();
}
// ---- CLOSE action ----
void doServoClose() {
servo.attach(5);
servo.write(180);
delay(1500);
servo.detach();
delay(100);
servo.attach(5);
servo.write(0);
delay(250);
servo.detach();
}
// ---- web page ----
String pageHTML() {
return
"<!DOCTYPE html><html><head>"
"<meta name='viewport' content='width=device-width, initial-scale=1'>"
"<title>Dr. Maker Door</title>"
"<style>"
"body{font-family:Arial;text-align:center;margin-top:40px;}"
"button{font-size:22px;padding:16px 26px; margin:10px;}"
"</style></head><body>"
"<h2>Door Control</h2>"
"<form action='/run' method='POST'>"
"<button type='submit'>Open</button>"
"</form>"
"<form action='/close' method='POST'>"
"<button type='submit'>Close</button>"
"</form>"
"</body></html>";
}
void redirectToPortal() {
// When connected to AP, portal IP is normally 192.168.4.1
server.sendHeader("Location", "http://192.168.4.1/", true);
server.send(302, "text/plain", "");
}
void handleRoot() {
server.send(200, "text/html", pageHTML());
}
void handleOpenLink() {
doServoOpen();
server.send(200, "text/plain", "OPEN OK");
}
void handleRun() {
doServoOpen();
server.send(200, "text/html",
"<html><body style='font-family:Arial;text-align:center;margin-top:40px;'>"
"<h3>The Door is Opened Welcome Home!!</h3><a href='/'>Back</a>"
"</body></html>");
}
void handleClose() {
doServoClose();
server.send(200, "text/html",
"<html><body style='font-family:Arial;text-align:center;margin-top:40px;'>"
"<h3>The Door is Closed.</h3><a href='/'>Back</a>"
"</body></html>");
}
void setup() {
Serial.begin(115200);
delay(200);
// Dual mode: AP + STA
WiFi.mode(WIFI_AP_STA);
// Start AP
WiFi.softAP(AP_SSID, AP_PASS);
IPAddress apIP = WiFi.softAPIP(); // usually 192.168.4.1
Serial.print("AP IP: ");
Serial.println(apIP);
// Start STA (connect to router)
WiFi.begin(STA_SSID, STA_PASS);
Serial.print("Connecting to STA WiFi");
unsigned long start = millis();
while (WiFi.status() != WL_CONNECTED && millis() - start < 15000) {
delay(300);
Serial.print(".");
}
Serial.println();
if (WiFi.status() == WL_CONNECTED) {
Serial.print("STA connected. STA IP: ");
Serial.println(WiFi.localIP());
} else {
Serial.println("STA not connected (AP will still work).");
}
// Captive portal DNS for AP clients (redirect all domains to AP IP)
dnsServer.start(DNS_PORT, "*", apIP);
// Routes (work from BOTH: AP and STA IP)
server.on("/", HTTP_GET, handleRoot);
server.on("/run", HTTP_POST, handleRun);
server.on("/close", HTTP_POST, handleClose);
server.on("/open", HTTP_GET, handleOpenLink);
// Captive portal probe URLs -> redirect (mainly for AP clients)
server.on("/generate_204", HTTP_GET, redirectToPortal); // Android
server.on("/gen_204", HTTP_GET, redirectToPortal);
server.on("/hotspot-detect.html", HTTP_GET, redirectToPortal); // Apple
server.on("/library/test/success.html", HTTP_GET, redirectToPortal); // Apple
server.on("/connecttest.txt", HTTP_GET, redirectToPortal); // Windows
server.on("/ncsi.txt", HTTP_GET, redirectToPortal);
// Anything else -> redirect to portal (AP captive behavior)
server.onNotFound(redirectToPortal);
server.begin();
Serial.println("Web server started");
}
void loop() {
dnsServer.processNextRequest();
server.handleClient();
}