forked from binaryupdates/esp01-firmware
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathESP01-Webserver.ino
More file actions
100 lines (83 loc) · 2.9 KB
/
Copy pathESP01-Webserver.ino
File metadata and controls
100 lines (83 loc) · 2.9 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
#include <ESP8266WiFi.h>
#include <ESP8266WebServer.h>
/*Put WiFi SSID & Password*/
const char* ssid = "binaryupdates"; // Enter SSID here
const char* password = "bestcourses"; // Enter Password here
ESP8266WebServer server(80);
bool LEDstatus = LOW;
void setup() {
Serial.begin(115200);
delay(100);
pinMode(2, OUTPUT);
Serial.println("Connecting to ");
Serial.println(ssid);
//connect to your local wi-fi network
WiFi.begin(ssid, password);
//check NodeMCU is connected to Wi-fi network
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("/ledon", handle_ledon);
server.on("/ledoff", handle_ledoff);
server.onNotFound(handle_NotFound);
server.begin();
Serial.println("HTTP Server Started");
}
void loop() {
server.handleClient();
if (LEDstatus) {
digitalWrite(2, HIGH);
} else {
digitalWrite(2, LOW);
}
}
void handle_OnConnect() {
LEDstatus = LOW;
Serial.println("LED: OFF");
server.send(200, "text/html", updateWebpage(LEDstatus));
}
void handle_ledon() {
LEDstatus = HIGH;
Serial.println("LED: ON");
server.send(200, "text/html", updateWebpage(LEDstatus));
}
void handle_ledoff() {
LEDstatus = LOW;
Serial.println("LED: OFF");
server.send(200, "text/html", updateWebpage(LEDstatus));
}
void handle_NotFound() {
server.send(404, "text/plain", "Not found");
}
String updateWebpage(uint8_t LEDstatus) {
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>ESP8266 Web Server</h1>\n";
ptr += "<h3>Using Station(STA) Mode</h3>\n";
if (LEDstatus) {
ptr += "<p>BLUE LED: ON</p><a class=\"button button-off\" href=\"/ledoff\">OFF</a>\n";
} else {
ptr += "<p>BLUE LED: OFF</p><a class=\"button button-on\" href=\"/ledon\">ON</a>\n";
}
ptr += "</body>\n";
ptr += "</html>\n";
return ptr;
}