-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwebserver.cpp
158 lines (138 loc) · 5.32 KB
/
webserver.cpp
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
#include "settings.h"
#include <ESPAsyncWebServer.h>
AsyncWebServer server(80);
const char pageTemplate[] PROGMEM = R"=====(<!DOCTYPE html>
<html><head><meta content='width=device-width' name='viewport'><title>Update</title></head><style>
* {
font-family: arial;
}
input[type=text] {
border-radius: .2rem;
padding: .4rem;
margin: .4rem .2rem;
border: 1px solid #ccc;
}
input[type="submit"] {
margin: .4rem auto;
display: block;
width: 80%%;
background-color: lightskyblue;
border: 1px solid aliceblue;
border-radius: .4rem;
padding: .5rem;
cursor: pointer;
}
label {
width: 5rem;
display: inline-block;
text-align: right;
margin: .4rem 1rem;
}
</style>
<body>
%CONTENT%
</body></html>)=====";
String settingsFormProcessor(const String& var) {
String settingsForm;
settingsForm = "<form method='POST' action='/api/net'>";
preferences.begin("net", false);
settingsForm += "<label>ssid</label><input type='text' name='ssid' value='" + preferences.getString("ssid") + "'/><br/>";
settingsForm += "<label>password</label><input type='text' name='password' value='" + preferences.getString("password") + "' /><br/>";
settingsForm += "<input type='submit' value='Update Network Settings'>";
preferences.end();
settingsForm += "</form><br/>";
settingsForm += "<form method='POST' action='/api/artnet'>";
preferences.begin("artnet", false);
settingsForm += "<label>LED count</label><input type='text' name='numLeds' value='" + String(preferences.getUInt("numLeds")) + "'/><br/>";
settingsForm += "<label>Universe</label><input type='text' name='startUniverse' value='" + String(preferences.getUInt("startUniverse")) + "' /><br/>";
preferences.end();
settingsForm += "<input type='submit' value='Update Artnet Settings'>";
settingsForm += "</form><br />";
settingsForm += "<form method='GET' action='/api/reset'>";
settingsForm += "<input type='submit' value='Reset to Defaults'>";
settingsForm += "</form>";
settingsForm += "<form method='GET' action='/api/reboot'>";
settingsForm += "<input type='submit' value='Reboot'>";
settingsForm += "</form>";
return settingsForm;
}
//TODO don't require preferences be set in.. just create
//a new object locally
void printSettings() {
Serial.println("===== settings =====");
preferences.begin("net", false);
Serial.print("SSID: ");
Serial.println(preferences.getString("ssid"));
Serial.print("PASS: ");
Serial.println(preferences.getString("password"));
preferences.end();
preferences.begin("artnet", false);
Serial.print("LED Count: ");
Serial.println(preferences.getUInt("numLeds"));
Serial.print("Universe: ");
Serial.println(preferences.getUInt("startUniverse"));
preferences.end();
}
void notFound(AsyncWebServerRequest *request) {
request->send(404, "text/plain", "Not found");
}
void initWebServer() {
// Route for root / web page
server.on("/", HTTP_GET, [&pageTemplate](AsyncWebServerRequest *request){
request->send_P(200, "text/html", pageTemplate, settingsFormProcessor);
});
server.on("/api/net", HTTP_GET, [&preferences](AsyncWebServerRequest *request){
preferences.begin("net", false);
String ssid = preferences.getString("ssid", DEFAULT_NET_SSID);
preferences.end();
String json = "{\"ssid\":\"" + ssid + "\"}";
request->send_P(200, "application/json", json.c_str());
});
server.on("/api/net", HTTP_POST, [&preferences](AsyncWebServerRequest *request){
preferences.begin("net");
preferences.putString("ssid", request->arg("ssid"));
preferences.putString("password", request->arg("password"));
preferences.end();
printSettings();
request->redirect("/");
// request->send_P(200, "text/html", "updated");
});
server.on("/api/artnet", HTTP_GET, [&preferences](AsyncWebServerRequest *request){
preferences.begin("artnet", false);
int numLeds = preferences.getUInt("numLeds", DEFAULT_NUM_LEDS);
int startUniverse = preferences.getUInt("startUniverse", DEFAULT_START_UNIVERSE);
preferences.end();
String json = "{\"numLeds\":" + String(numLeds) + ", \"startUniverse\": " + String(startUniverse) + "}";
request->send_P(200, "application/json", json.c_str());
});
server.on("/api/artnet", HTTP_POST, [&preferences](AsyncWebServerRequest *request){
Serial.println("setting num leds");
preferences.begin("artnet");
preferences.putUInt("numLeds", request->arg("numLeds").toInt());
preferences.putUInt("startUniverse", request->arg("startUniverse").toInt());
preferences.end();
printSettings();
request->redirect("/");
// request->send_P(200, "text/html", "updated");
});
server.on("/api/reboot", HTTP_GET, [&preferences](AsyncWebServerRequest *request){
Serial.println("Restarting in 10 seconds");
request->send_P(200, "text/html", "Restarting in 10 seconds");
delay(10000);
ESP.restart();
});
server.on("/api/reset", HTTP_GET, [&preferences](AsyncWebServerRequest *request){
Serial.println("Resetting to factory defaults, then rebooting");
request->send_P(200, "text/html", "Resetting to factory defaults, then rebooting");
delay(10000);
preferences.begin("artnet");
preferences.clear();
preferences.end();
preferences.begin("net");
preferences.clear();
preferences.end();
ESP.restart();
});
server.onNotFound(notFound);
server.begin();
}