-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathwbc code.txt
More file actions
208 lines (173 loc) · 6.74 KB
/
Copy pathwbc code.txt
File metadata and controls
208 lines (173 loc) · 6.74 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
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
202
203
204
205
206
207
208
#include <ESP8266WiFi.h>
#include <ESP8266WebServer.h>
const char* ssid = "Amity-Guest";
const char* password = "Amity@654321";
ESP8266WebServer server(80);
String controlPassword = "admin123";
bool isAuthenticated = false;
bool LEDstatusRed = LOW;
bool LEDstatusGreen = LOW;
bool LEDstatusOrange = LOW;
void setup() {
Serial.begin(9600);
delay(100);
pinMode(D4, OUTPUT);
pinMode(D3, OUTPUT);
pinMode(D2, OUTPUT);
Serial.println("Connecting to ");
Serial.println(ssid);
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED) {
delay(1000);
Serial.print(".");
}
Serial.println("");
Serial.println("WiFi connected..!");
Serial.print("Got IP: ");
Serial.println(WiFi.localIP());
server.on("/", handle_OnConnect);
server.on("/submitPassword", handle_PasswordSubmit);
server.on("/ledonred", handle_ledonRed);
server.on("/ledoffred", handle_ledoffRed);
server.on("/ledongreen", handle_ledonGreen);
server.on("/ledoffgreen", handle_ledoffGreen);
server.onNotFound(handle_NotFound);
server.begin();
Serial.println("HTTP Server Started");
}
void loop() {
server.handleClient();
digitalWrite(D4, LEDstatusRed ? HIGH : LOW);
digitalWrite(D3, LEDstatusGreen ? HIGH : LOW);
}
void blinkOrangeLED() {
for (int i = 0; i < 5; i++) {
digitalWrite(D2, HIGH);
delay(500);
digitalWrite(D2, LOW);
delay(500);
}
}
String passwordPage(String message = "") {
String ptr = "<!DOCTYPE html> <html>\n";
ptr +="<head><meta name=\"viewport\" content=\"width=device-width, initial-scale=1.0, user-scalable=no\">\n";
ptr +="<title>Enter Password</title>\n";
ptr +="<style>html {font-family: Helvetica; display: inline-block; margin: 0px auto; text-align: center;}\n";
ptr +="body{margin-top: 50px;} h1 {color: #444444;margin: 50px auto 30px;} h3 {color: #444444;margin-bottom: 50px;}\n";
ptr +="p {font-size: 14px;color: #888;margin-bottom: 10px;}\n";
ptr +="input {padding: 13px;font-size: 18px;border-radius: 4px;border: 1px solid #ccc;width: 80%;}\n";
ptr +=".button {display: inline-block;background-color: #3498db;border: none;color: white;padding: 13px 30px;text-decoration: none;font-size: 20px;margin-top: 20px;cursor: pointer;border-radius: 4px;}\n";
ptr +="</style>\n";
ptr +="</head>\n";
ptr +="<body>\n";
ptr +="<h1>Smart Traffic System</h1>\n";
ptr +="<h3>BY Team - WBC</h3>\n";
if (message != "") {
ptr +="<p style=\"color:red;\">" + message + "</p>\n"; // Show error message if any
}
ptr +="<form action=\"/submitPassword\" method=\"POST\">\n";
ptr +="<input type=\"password\" name=\"password\" placeholder=\"Enter Password\"><br>\n";
ptr +="<input type=\"submit\" class=\"button\" value=\"Submit\">\n";
ptr +="</form>\n";
ptr +="</body>\n";
ptr +="</html>\n";
return ptr;
}
void handle_OnConnect() {
if (isAuthenticated) {
server.send(200, "text/html", updateWebpage(LEDstatusRed, LEDstatusGreen));
} else {
server.send(200, "text/html", passwordPage()); // Show password input page
}
}
void handle_PasswordSubmit() {
if (server.hasArg("password")) {
String inputPassword = server.arg("password");
if (inputPassword == controlPassword) {
isAuthenticated = true;
server.send(200, "text/html", updateWebpage(LEDstatusRed, LEDstatusGreen));
} else {
server.send(200, "text/html", passwordPage("Invalid Password! Try again."));
}
} else {
server.send(200, "text/html", passwordPage("Please enter a password."));
}
}
void handle_ledonRed() {
if (isAuthenticated) {
if (LEDstatusGreen) {
blinkOrangeLED();
}
LEDstatusRed = HIGH;
LEDstatusGreen = LOW;
Serial.println("Red LED: ON, Green LED: OFF");
server.send(200, "text/html", updateWebpage(LEDstatusRed, LEDstatusGreen));
} else {
server.send(200, "text/html", passwordPage("You need to login first."));
}
}
void handle_ledoffRed() {
if (isAuthenticated) {
LEDstatusRed = LOW;
Serial.println("Red LED: OFF");
server.send(200, "text/html", updateWebpage(LEDstatusRed, LEDstatusGreen));
} else {
server.send(200, "text/html", passwordPage("You need to login first."));
}
}
void handle_ledonGreen() {
if (isAuthenticated) {
if (LEDstatusRed) {
blinkOrangeLED();
}
LEDstatusGreen = HIGH;
LEDstatusRed = LOW;
Serial.println("Green LED: ON, Red LED: OFF");
server.send(200, "text/html", updateWebpage(LEDstatusRed, LEDstatusGreen));
} else {
server.send(200, "text/html", passwordPage("You need to login first."));
}
}
void handle_ledoffGreen() {
if (isAuthenticated) {
LEDstatusGreen = LOW;
Serial.println("Green LED: OFF");
server.send(200, "text/html", updateWebpage(LEDstatusRed, LEDstatusGreen));
} else {
server.send(200, "text/html", passwordPage("You need to login first."));
}
}
void handle_NotFound() {
server.send(404, "text/plain", "Not found");
}
String updateWebpage(uint8_t LEDstatusRed, uint8_t LEDstatusGreen) {
String ptr = "<!DOCTYPE html> <html>\n";
ptr +="<head><meta name=\"viewport\" content=\"width=device-width, initial-scale=1.0, user-scalable=no\">\n";
ptr +="<title>LED Control</title>\n";
ptr +="<style>html {font-family: Helvetica; display: inline-block; margin: 0px auto; text-align: center;}\n";
ptr +="body{margin-top: 50px;} h1 {color: #444444;margin: 50px auto 30px;} h3 {color: #444444;margin-bottom: 50px;}\n";
ptr +=".button {display: block;width: 80px;background-color: #1abc9c;border: none;color: white;padding: 13px 30px;text-decoration: none;font-size: 25px;margin: 0px auto 35px;cursor: pointer;border-radius: 4px;}\n";
ptr +=".button-on {background-color: #3498db;}\n";
ptr +=".button-on:active {background-color: #3498db;}\n";
ptr +=".button-off {background-color: #34495e;}\n";
ptr +=".button-off:active {background-color: #2c3e50;}\n";
ptr +="p {font-size: 14px;color: #888;margin-bottom: 10px;}\n";
ptr +="</style>\n";
ptr +="</head>\n";
ptr +="<body>\n";
ptr +="<h1>Smart Traffic System</h1>\n";
ptr +="<h3>BY Team - WBC</h3>\n";
if (LEDstatusRed) {
ptr +="<p>RED LED: ON</p><a class=\"button button-off\" href=\"/ledoffred\">OFF</a>\n";
} else {
ptr +="<p>RED LED: OFF</p><a class=\"button button-on\" href=\"/ledonred\">ON</a>\n";
}
if (LEDstatusGreen) {
ptr +="<p>GREEN LED: ON</p><a class=\"button button-off\" href=\"/ledoffgreen\">OFF</a>\n";
} else {
ptr +="<p>GREEN LED: OFF</p><a class=\"button button-on\" href=\"/ledongreen\">ON</a>\n";
}
ptr +="</body>\n";
ptr +="</html>\n";
return ptr;
}