-
Notifications
You must be signed in to change notification settings - Fork 0
/
esp8266-web-server.ino
105 lines (84 loc) · 2.91 KB
/
esp8266-web-server.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
#include <ESP8266WebServer.h>
#include <ESP8266WiFi.h>
#include <Ticker.h>
#define WIFI_SSID "My_Wi-Fi"
#define WIFI_PASSWORD "my-awesome-password"
#define HTTP_SERVER_PORT 8266
#define SERIAL_MONITOR_PORT 115200
WiFiEventHandler wifiConnectHandler;
WiFiEventHandler wifiDisconnectHandler;
Ticker wifiReconnectTimer;
// initialise server
ESP8266WebServer server(HTTP_SERVER_PORT);
void connectToWifi() {
Serial.println("Connecting to Wi-Fi...");
WiFi.begin(WIFI_SSID, WIFI_PASSWORD);
}
void onWifiConnect(const WiFiEventStationModeGotIP &event) {
Serial.printf("Connected to Wi-Fi: `%s`\n", WiFi.SSID().c_str());
Serial.print("Local IP address: ");
Serial.println(WiFi.localIP());
Serial.println();
// start server
startWebServer();
}
void onWifiDisconnect(const WiFiEventStationModeDisconnected &event) {
// kill webserver
server.close();
server.stop();
Serial.println("Disconnected from Wi-Fi.");
wifiReconnectTimer.once(4, connectToWifi);
}
void startWebServer() {
server.begin();
Serial.print("Web server started, open http://");
Serial.print(WiFi.localIP().toString().c_str());
Serial.print(":");
Serial.print(HTTP_SERVER_PORT);
Serial.println("/ in a web browser\n");
}
String generateRootHTML() {
String pageContent = "<!DOCTYPE html>\n";
pageContent += "<html lang=\"en\">\n";
pageContent += "<head>\n<meta charset=\"UTF-8\">\n";
pageContent += "<meta name=\"viewport\" content=\"width=device-width, "
"initial-scale=1.0\">\n";
pageContent += "<title>ESP8266 Web Server</title>\n</head>\n";
pageContent += "<body>\n<p>Hello World 🤨</p>\n</body>\n</html>";
return pageContent;
}
String generateNotFoundHTML() {
String pageContent = "<!DOCTYPE html>\n";
pageContent += "<html lang=\"en\">\n";
pageContent += "<head>\n<meta charset=\"UTF-8\">\n";
pageContent += "<meta name=\"viewport\" content=\"width=device-width, "
"initial-scale=1.0\">\n";
pageContent += "<title>404 | Not Found</title>\n</head>\n";
pageContent += "<body>\n<p>404 | Not Found 😴</p>\n</body>\n</html>";
return pageContent;
}
void verboseRequest(int status) {
Serial.print((server.method() == HTTP_GET) ? "[GET] " : "[POST] ");
Serial.print(server.uri());
Serial.print(" ");
Serial.println(status);
}
void serveRootPage() {
verboseRequest(200);
server.send(200, "text/html", generateRootHTML());
}
void handleNotFound() {
verboseRequest(404);
server.send(404, "text/html", generateNotFoundHTML());
}
void setup() {
Serial.begin(SERIAL_MONITOR_PORT);
Serial.println();
wifiConnectHandler = WiFi.onStationModeGotIP(onWifiConnect);
wifiDisconnectHandler = WiFi.onStationModeDisconnected(onWifiDisconnect);
// server routes
server.on("/", serveRootPage);
server.onNotFound(handleNotFound);
connectToWifi();
}
void loop() { server.handleClient(); }