-
Notifications
You must be signed in to change notification settings - Fork 0
/
webserver.ino
268 lines (245 loc) · 9.52 KB
/
webserver.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
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
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
// IPAddress dns(8, 8, 8, 8);
void savesettings() {
Serial.println("attempting save...");
saveConfiguration();
delay(pause);
}
String processor(const String& var){
Serial.print("var is ");
Serial.println(var);
if (var == "CURRENTIP") {
return (IPtoString(WiFi.localIP()));
}
if (var == "CURRENTWIFI") {
return (WiFi.SSID());
}
if (var == "CURRENTUNIVERSE") {
return String(myuniverse);
}
if (var == "CURRENTNODES") {
return String(NUM_LEDS);
}
if (var == "CURRENTCHANNELS") {
return String(numberOfChannels);
}
if (var == "NODENAME") {
return String(HOSTNAME);
}
// this is reponsible for sticking the selected LED type on the drop down
if (var == "selectedled[0]") { return selectedled[0]; Serial.println("led set 0 selected"); }
if (var == "selectedled[1]") { return selectedled[1]; Serial.println("led set 1 selected"); }
if (var == "startupeffects[0]") { return startupeffectsenabled[0]; }
if (var == "startupeffects[1]") { return startupeffectsenabled[1]; }
if (var == "cycleeffects[0]") { return cycleeffects[0]; }
if (var == "cycleeffects[1]") { return cycleeffects[1]; }
if (var == "selected[0]") {
return selected[0];
}
if (var == "selected[1]") {
return selected[1];
}
if (var == "selected[2]") {
return selected[2];
}
if (var == "selected[3]") {
return selected[3];
}
if (var == "selected[4]") {
return selected[4];
}
if (var == "selected[5]") {
return selected[5];
}
if (var == "selected[6]") {
return selected[6];
}
if (var == "selected[7]") {
return selected[7];
}
if (var == "selected[8]") {
return selected[8];
}
if (var == "selected[9]") {
return selected[9];
}
if (var == "selected[10]") {
return selected[10];
}
//if (var == "startupeffects") {
// return String(startupeffects());
//}
if (var == "dp") { return String(DATA_PIN); } // return dp
if (var == "cp") { return String(CLOCK_PIN); } // return cp
if (var == "ver.ver") { Serial.println(vr.ver); return String(vr.ver); }
if (var == "ver.author") { return String(vr.author); }
if (var == "ver.company") { return String(vr.company); }
if (var == "ver.website") { return String(vr.website); }
if (var == "ver.project") { return String(vr.project); }
}
//String startupeffects() {
// if (autoeffects == 1) { return "checked"; } else { return ""; }
//}
String redirect() {
String rd = "http://";
//String rd2 = ".local/";
return (rd + IPtoString(WiFi.localIP()));
}
String IPtoString(const IPAddress& address){
return String() + address[0] + "." + address[1] + "." + address[2] + "." + address[3];
}
void websetup(){
//Serial.begin(115200);
// Initialize SPIFFS
if(!SPIFFS.begin()){
Serial.println("An Error has occurred while mounting SPIFFS");
return;
}
//dns.start(53, "*", WiFi.localIP());
// These two callbacks are required for gen1 and gen3 compatibility
// for wemo - lifted from example program
server.onRequestBody([](AsyncWebServerRequest *request, uint8_t *data, size_t len, size_t index, size_t total) {
if (fauxmo.process(request->client(), request->method() == HTTP_GET, request->url(), String((char *)data))) return;
// Handle any other body request here...
});
server.onNotFound([](AsyncWebServerRequest *request) {
String body = (request->hasParam("body", true)) ? request->getParam("body", true)->value() : String();
if (fauxmo.process(request->client(), request->method() == HTTP_GET, request->url(), body)) return;
// Handle not found request here...
});
server.on("/rand", HTTP_GET, [](AsyncWebServerRequest *request){
request->send(200, "text/plain", String(random(1000)));
});
server.on("/scan", HTTP_GET, [](AsyncWebServerRequest *request){
String json = scanwifi();
request->send(200, "application/json", json);
});
// Route for root / web page
server.on("/", HTTP_GET, [](AsyncWebServerRequest *request){
request->send(SPIFFS, "/index.html", String(), false, processor);
});
// Route for css sheets
server.on("/bootstrap.min.css", HTTP_GET, [](AsyncWebServerRequest *request){
request->send(SPIFFS, "/bootstrap.min.css", "text/css");
});
// routes for config pages
server.on("/led.html", HTTP_GET, [](AsyncWebServerRequest *request){
request->send(SPIFFS, "/led.html", String(), false, processor);
});
server.on("/builtin.html", HTTP_GET, [](AsyncWebServerRequest *request){
request->send(SPIFFS, "/builtin.html", String(), false, processor);
});
server.on("/network.html", HTTP_GET, [](AsyncWebServerRequest *request){
request->send(SPIFFS, "/network.html", String(), false, processor);
});
server.on("/reboot.html", HTTP_GET, [](AsyncWebServerRequest *request){
//call save-before-reboot;
//request->send(SPIFFS, "/reboot.html", String(), false, processor);
request->redirect(redirect());
delay(pause);
ESP.reset();
reboot();
});
server.on("/resetwifi.html", HTTP_GET, [](AsyncWebServerRequest *request){
request->redirect(redirect());
delay(pause);
resetwifi();
//reboot();
});
server.on("/style.css", HTTP_GET, [](AsyncWebServerRequest *request){
request->send(SPIFFS, "/style.css", "text/css");
});
server.on("/jquery.min.js", HTTP_GET, [](AsyncWebServerRequest *request){
request->send(SPIFFS, "/jquery.min.js", "text/javascript");
});
server.on("/bootstrap.min.js", HTTP_GET, [](AsyncWebServerRequest *request){
request->send(SPIFFS, "/bootstrap.min.js", "text/javascript");
});
server.on("/scripts.js", HTTP_GET, [](AsyncWebServerRequest *request){
request->send(SPIFFS, "/scripts.js", "text/javascript");
});
// process submit form
server.on("/save.html", HTTP_GET, [](AsyncWebServerRequest *request){
Serial.println("Checking which parameter to save...");
int params = request->params();
Serial.printf("%d params sent in\n", params); Serial.println("");
for (int i = 0; i < params; i++)
{
AsyncWebParameter *p = request->getParam(i);
if (p->isFile())
{
Serial.printf("_FILE[%s]: %s, size: %u", p->name().c_str(), p->value().c_str(), p->size());
}
else if (p->isPost())
{
Serial.printf("_POST[%s]: %s", p->name().c_str(), p->value().c_str());
}
else
{
Serial.printf("_GET[%s]: %s", p->name().c_str(), p->value().c_str());
}
Serial.println("");
}
//
if (request->hasParam("effect"))
{
Serial.printf("Starting effect %s",request->getParam("effect")->value().c_str());
Serial.println("");
selectedeffectno = request->getParam("effect")->value().toInt();
//effectselected=1;
// code to start the effect
}
// changing the pixel string type
if (request->hasParam("curtype")) { ledtype = request->getParam("curtype")->value().toInt(); }
// startupeffects can be changed from on or off 1 or 0 to determine if on startup
// either the chosen single effect is started or cycling through all builtin effects
if (request->hasParam("startupeffects"))
{
// see what value we have for startup effects on or null
int s = request->getParam("startupeffects")->value().toInt();
if (s == 1) { autoeffects=1; } else { autoeffects=0; }
}
// cater for changing value of cycling effects on startup or the one selected effect
if (request->hasParam("cycleeffects")) {
int s = request->getParam("cycleeffects")->value().toInt();
if (s == 1) { randomeffects = 1; } else { randomeffects=0; }
}
if (request->hasParam("curuniverse"))
{
myuniverse = request->getParam("curuniverse")->value().toInt();
}
if (request->hasParam("curnodes"))
{
NUM_LEDS = request->getParam("curnodes")->value().toInt();
numberOfChannels = NUM_LEDS * 3;
}
if (request->hasParam("curip"))
{
IPAddress ip;
if (ip.fromString(String(request->getParam("curip")->value()))) { // str = char * or String
// it is a valid IP address
ip = ip.fromString(String(request->getParam("curip")->value()));
//WiFi.config(ip,dns,WiFi.gatewayIP(),WiFi.subnetMask());
//WiFi.begin();
//WiFi.config(ip);
}
//myuniverse = request->getParam("curuniverse", true)->value();
}
// check if nodename has been changed
if (request->hasParam("NODENAME")) { strncpy(HOSTNAME,request->getParam("NODENAME")->value().c_str(),20); }
//myuniverse = request->getParam("curuniverse", true)->value();
// do any other saving - ie making changes permanent
//request->send(SPIFFS, "/index.html", "text/html");
savesettings();
//ESP.restart();
request->redirect(redirect());
delay(pause);
delay(pause);
reboot();
//delay(3000);
//Serial.begin(57600); // i think I read that reini serial will restart the sketch?
//request->send(SPIFFS, "/index.html", String(), false, processor);
});
server.begin();
}
void webloop(){
}