forked from jasoncoon/esp32-fastled-webserver
-
Notifications
You must be signed in to change notification settings - Fork 0
/
web.h
87 lines (74 loc) · 2.94 KB
/
web.h
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
/*
ESP32 FastLED WebServer: https://github.com/jasoncoon/esp32-fastled-webserver
Copyright (C) 2017 Jason Coon
Built upon the amazing FastLED work of Daniel Garcia and Mark Kriegsman:
https://github.com/FastLED/FastLED
ESP32 support provided by the hard work of Sam Guyer:
https://github.com/samguyer/FastLED
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
void setupWeb() {
webServer.on("/all", HTTP_GET, []() {
digitalWrite(led, 0);
String json = getFieldsJson(fields, fieldCount);
webServer.send(200, "text/json", json);
digitalWrite(led, 1);
});
webServer.on("/fieldValue", HTTP_GET, []() {
digitalWrite(led, 0);
String name = webServer.arg("name");
String value = getFieldValue(name, fields, fieldCount);
webServer.send(200, "text/json", value);
digitalWrite(led, 1);
});
webServer.on("/fieldValue", HTTP_POST, []() {
digitalWrite(led, 0);
String name = webServer.arg("name");
String value = webServer.arg("value");
String newValue = setFieldValue(name, value, fields, fieldCount);
webServer.send(200, "text/json", newValue);
digitalWrite(led, 1);
});
webServer.serveStatic("/", SPIFFS, "/index.htm", "max-age=86400");
webServer.serveStatic("/index.htm", SPIFFS, "/index.htm", "max-age=86400");
webServer.serveStatic("/favicon.ico", SPIFFS, "/favicon.ico", "max-age=86400");
webServer.serveStatic("/css/styles.css", SPIFFS, "/css/styles.css", "max-age=86400");
webServer.serveStatic("/js/app.js", SPIFFS, "/js/app.js", "max-age=86400");
webServer.serveStatic("/images/atom196.png", SPIFFS, "/images/atom196.png", "max-age=86400");
webServer.begin();
Serial.println ( "HTTP server started" );
}
void handleWeb() {
static bool webServerStarted = false;
// check for connection
if ( WiFi.status() == WL_CONNECTED ) {
if (!webServerStarted) {
// turn off the board's LED when connected to wifi
digitalWrite(led, 1);
Serial.println();
Serial.println("WiFi connected");
Serial.print("IP address: ");
Serial.println(WiFi.localIP());
webServerStarted = true;
setupWeb();
}
webServer.handleClient();
} else {
// blink the board's LED while connecting to wifi
static uint8_t ledState = 0;
EVERY_N_MILLIS(125) {
ledState = ledState == 0 ? 1 : 0;
digitalWrite(led, ledState);
Serial.print (".");
}
}
}