1
0
mirror of https://github.com/s00500/ESPUI.git synced 2024-11-22 14:20:53 +00:00

Style change again

This commit is contained in:
Lukas Bachschwell 2019-03-24 18:10:21 +01:00
parent 70288fc69f
commit 218ebc67d9
2 changed files with 256 additions and 302 deletions

View File

@ -1,6 +1,7 @@
# ESPUI # ESPUI
![ESPUI](https://github.com/s00500/ESPUI/blob/master/docs/ui_complete.png) ![ESPUI](https://github.com/s00500/ESPUI/blob/master/docs/ui_complete.png) //
TODO: Update Logo
ESPUI is a simple library to make a web user interface for your projects using ESPUI is a simple library to make a web user interface for your projects using
the **ESP8266** or the **ESP32** It uses web sockets and lets you create, the **ESP8266** or the **ESP32** It uses web sockets and lets you create,
@ -14,13 +15,11 @@ So if you either don't know how or just don't want to waste time: this is your
simple solution user interface without the need of internet connectivity or any simple solution user interface without the need of internet connectivity or any
additional servers. additional servers.
I completely rewrote the EasyUI Library created by ayushsharma82 The Library runs fine on any kind of ESP8266 and ESP32 (NodeMCU Boards, usw)
[Here](https://github.com/ayushsharma82/) Now it uses ESPAsyncWebserver and is
mainly to be used with the ESP32 Processor.
# Important notes # Important notes
THIS IS THE 2.0.0 development branch THIS IS THE 2.0.0 DEVELOPMENT BRANCH, NOT GUARANTIED TO WORK
**Roadmap for 2.0.0:** **Roadmap for 2.0.0:**
@ -35,10 +34,10 @@ THIS IS THE 2.0.0 development branch
- Min Max on slider - Min Max on slider
- Accelerometer Widget - Accelerometer Widget
- Cleanup Example - Cleanup Example
- New Documentation - Cleanup and extend Documentation
- Numberfield - Number field
- Textfield - Text field
- Data directory - Data directory
- Graph Usage - Graph Usage
- Accelerometer - Accelerometer
- Slider - Slider
@ -51,10 +50,8 @@ THIS IS THE 2.0.0 development branch
This library is dependent on the following libraries to function properly. This library is dependent on the following libraries to function properly.
- [ESPAsyncWebserver](https://github.com/me-no-dev/ESPAsyncWebServer) - [ESPAsyncWebserver](https://github.com/me-no-dev/ESPAsyncWebServer)
- [ArduinoJson](https://github.com/bblanchon/ArduinoJson) **(VERSIONS 5.x only - [ArduinoJson](https://github.com/bblanchon/ArduinoJson) (Last tested with
currently)** version 6.10.0) **Plus for ESP8266**
**Plus for ESP8266**
- [ESPAsyncTCP](https://github.com/me-no-dev/ESPAsyncTCP) - [ESPAsyncTCP](https://github.com/me-no-dev/ESPAsyncTCP)
@ -92,16 +89,16 @@ Go to Sketch>Include Library>Add .zip Library> Select the Downloaded .zip File.
## Getting started ## Getting started
ESPUI serves several files to the browser to build up its webinterface. This can ESPUI serves several files to the browser to build up its web interface. This
be achieved in 2 ways: _PROGMEM_ or _SPIFFS_ can be achieved in 2 ways: _PROGMEM_ or _SPIFFS_
_When `ESPUI.begin()` is called the default is serving files from Memory and _When `ESPUI.begin()` is called the default is serving files from Memory and
ESPUI should work out of the box!_ ESPUI should work out of the box!_
But if this causes your program to _use too much memory_ you can burn the files **OPTIONAL:** But if this causes your program to _use too much memory_ you can
into the SPIFFS filesystem on the ESP. There are now two ways to do this: you burn the files into the SPIFFS filesystem on the ESP. There are now two ways to
can either use the ESP file upload tool or you use the library function do this: you can either use the ESP file upload tool or you use the library
`ESPUI.prepareFileSystem()` function `ESPUI.prepareFileSystem()`
#### Simple filesystem preparation (_recommended_) #### Simple filesystem preparation (_recommended_)
@ -111,19 +108,8 @@ will create all needed files. Congratulations, you are done, from now on you
just need to to this again when there is a library update, or when you want to just need to to this again when there is a library update, or when you want to
use another chip :-) Now you can upload your normal sketch, when you do not call use another chip :-) Now you can upload your normal sketch, when you do not call
the `ESPUI.prepareFileSystem()` function the compiler will strip out all the the `ESPUI.prepareFileSystem()` function the compiler will strip out all the
unnecessary that is already saved in the chip's filesystem and you have more unnecessary strings that are already saved in the chip's filesystem and you have
programm memory to work with. more program memory to work with.
#### Manual way (mainly for development)
To do this download and install me-no-devs wonderful
[ESP32 sketch data uploader](https://github.com/me-no-dev/arduino-esp32fs-plugin)
or for ESP8266
[ESP8266 sketch data uploader](https://github.com/esp8266/arduino-esp8266fs-plugin)
Then open the **gui** example sketch and select "Upload Sketch Data" from the
Tools menu for your processor. Now you are set to go and use any code you want
to with this library
## User interface Elements ## User interface Elements
@ -235,6 +221,28 @@ realtime data, are touch compatible and can be used to for example control a
Servo. The current value is shown while the slider is dragged in a little bubble Servo. The current value is shown while the slider is dragged in a little bubble
over the handle. over the handle.
#### Number Input
TODO: Add image
The numberinput can be used to directly input numbers to your program. You can
enter a Value into it and when you are done with your change it is sent to the
ESP.
#### Text Input
TODO: Add image
The textinput works very similar like the number input but with a string. You
can enter a String into it and when you are done with your change it is sent to
the ESP.
#### Using Tabs
TODO: Add image
// TODO: add Text for tabs
#### Initialisation of the UI #### Initialisation of the UI
After all the elements are configured you can use `ESPUI.begin(“Some Title”);` After all the elements are configured you can use `ESPUI.begin(“Some Title”);`
@ -244,11 +252,24 @@ example). The web interface can then be used from multiple devices at once and
also shows an connection status in the top bar. The library is designed to be also shows an connection status in the top bar. The library is designed to be
easy to use and can still be extended with a lot of more functionality. easy to use and can still be extended with a lot of more functionality.
#### Log output
ESPUI has several different log levels. You can set them using the
`ESPUI.setVerbosity(Verbosity::VerboseJSON)` function.
Loglevels are:
- Verbosity::Quiet (default)
- Verbosity::Verbose
- Verbosity::VerboseJSON
// TODO: Add some more notes here
# Notes for Development # Notes for Development
If you want to work on the HTML/CSS/JS files, do make changes in the If you want to work on the HTML/CSS/JS files, do make changes in the `data`
`examples/gui/data` directory. When you need to transfer that code to the ESP, directory. When you need to transfer that code to the ESP, run
run `tools/prepare_static_ui_sources.py -a` (this script needs python3 with the `tools/prepare_static_ui_sources.py -a` (this script needs python3 with the
modules htmlmin, jsmin and csscompressor). This will generate a) minified files modules htmlmin, jsmin and csscompressor). This will generate a) minified files
next to the original files to be uploaded with the ESP32 sketch data uploader next to the original files to be uploaded with the ESP32 sketch data uploader
mentioned above and b) the C header files in `src` that contain the minified and mentioned above and b) the C header files in `src` that contain the minified and
@ -271,3 +292,5 @@ commits.
Liked this Library? You can **support** me by sending me a :coffee: Liked this Library? You can **support** me by sending me a :coffee:
[Coffee](https://paypal.me/lukasbachschwell/3). [Coffee](https://paypal.me/lukasbachschwell/3).
Otherwise I really welcome **Pull Requests**.

View File

@ -17,7 +17,7 @@ uint16_t Control::idCounter = 0;
// ################# Spiffs functions // ################# Spiffs functions
#if defined(ESP32) #if defined(ESP32)
void listDir(const char* dirname, uint8_t levels) { void listDir(const char *dirname, uint8_t levels) {
if (ESPUI.verbosity) { if (ESPUI.verbosity) {
Serial.printf("Listing directory: %s\n", dirname); Serial.printf("Listing directory: %s\n", dirname);
} }
@ -66,7 +66,7 @@ void listDir(const char* dirname, uint8_t levels) {
} }
#else #else
void listDir(const char* dirname, uint8_t levels) { void listDir(const char *dirname, uint8_t levels) {
// ignoring levels for esp8266 // ignoring levels for esp8266
Serial.printf("Listing directory: %s\n", dirname); Serial.printf("Listing directory: %s\n", dirname);
@ -105,7 +105,7 @@ void ESPUIClass::list() {
#endif #endif
} }
void deleteFile(const char* path) { void deleteFile(const char *path) {
if (ESPUI.verbosity) { if (ESPUI.verbosity) {
Serial.print(SPIFFS.exists(path)); Serial.print(SPIFFS.exists(path));
} }
@ -133,7 +133,7 @@ void deleteFile(const char* path) {
} }
} }
void writeFile(const char* path, const char* data) { void writeFile(const char *path, const char *data) {
if (ESPUI.verbosity) { if (ESPUI.verbosity) {
Serial.printf("Writing file: %s\n", path); Serial.printf("Writing file: %s\n", path);
} }
@ -253,148 +253,144 @@ void ESPUIClass::prepareFileSystem() {
} }
// Handle Websockets Communication // Handle Websockets Communication
void onWsEvent(AsyncWebSocket* server, AsyncWebSocketClient* client, void onWsEvent(AsyncWebSocket *server, AsyncWebSocketClient *client, AwsEventType type, void *arg, uint8_t *data, size_t len) {
AwsEventType type, void* arg, uint8_t* data, size_t len) {
switch (type) { switch (type) {
case WS_EVT_DISCONNECT: { case WS_EVT_DISCONNECT: {
if (ESPUI.verbosity) { if (ESPUI.verbosity) {
Serial.printf("Disconnected!\n"); Serial.printf("Disconnected!\n");
}
break;
} }
case WS_EVT_PONG: { break;
if (ESPUI.verbosity) { }
Serial.printf("Received PONG!\n");
}
break; case WS_EVT_PONG: {
if (ESPUI.verbosity) {
Serial.printf("Received PONG!\n");
} }
case WS_EVT_ERROR: { break;
if (ESPUI.verbosity) { }
Serial.printf("WebSocket Error!\n");
}
break; case WS_EVT_ERROR: {
if (ESPUI.verbosity) {
Serial.printf("WebSocket Error!\n");
} }
case WS_EVT_CONNECT: { break;
}
case WS_EVT_CONNECT: {
if (ESPUI.verbosity) {
Serial.print("Connected: ");
Serial.println(client->id());
}
ESPUI.jsonDom(client);
if (ESPUI.verbosity) {
Serial.println("JSON Data Sent to Client!");
}
} break;
case WS_EVT_DATA: {
String msg = "";
for (size_t i = 0; i < len; i++) {
msg += (char)data[i];
}
uint16_t id = msg.substring(msg.lastIndexOf(':') + 1).toInt();
if (ESPUI.verbosity >= Verbosity::VerboseJSON) {
Serial.print("WS rec: ");
Serial.println(msg);
Serial.print("WS recognised ID: ");
Serial.println(id);
}
Control *c = ESPUI.getControl(id);
if (c == nullptr) {
if (ESPUI.verbosity) { if (ESPUI.verbosity) {
Serial.print("Connected: "); Serial.print("No control found for ID ");
Serial.println(client->id());
}
ESPUI.jsonDom(client);
if (ESPUI.verbosity) {
Serial.println("JSON Data Sent to Client!");
}
} break;
case WS_EVT_DATA: {
String msg = "";
for (size_t i = 0; i < len; i++) {
msg += (char)data[i];
}
uint16_t id = msg.substring(msg.lastIndexOf(':') + 1).toInt();
if (ESPUI.verbosity >= Verbosity::VerboseJSON) {
Serial.print("WS rec: ");
Serial.println(msg);
Serial.print("WS recognised ID: ");
Serial.println(id); Serial.println(id);
} }
Control* c = ESPUI.getControl(id); return;
}
if (c == nullptr) { if (c->callback == nullptr) {
if (ESPUI.verbosity) { if (ESPUI.verbosity) {
Serial.print("No control found for ID "); Serial.print("No callback found for ID ");
Serial.println(id); Serial.println(id);
}
return;
} }
if (c->callback == nullptr) { return;
if (ESPUI.verbosity) { }
Serial.print("No callback found for ID ");
Serial.println(id);
}
return; if (msg.startsWith("bdown:")) {
c->callback(c, B_DOWN);
} else if (msg.startsWith("bup:")) {
c->callback(c, B_UP);
} else if (msg.startsWith("pfdown:")) {
c->callback(c, P_FOR_DOWN);
} else if (msg.startsWith("pfup:")) {
c->callback(c, P_FOR_UP);
} else if (msg.startsWith("pldown:")) {
c->callback(c, P_LEFT_DOWN);
} else if (msg.startsWith("plup:")) {
c->callback(c, P_LEFT_UP);
} else if (msg.startsWith("prdown:")) {
c->callback(c, P_RIGHT_DOWN);
} else if (msg.startsWith("prup:")) {
c->callback(c, P_RIGHT_UP);
} else if (msg.startsWith("pbdown:")) {
c->callback(c, P_BACK_DOWN);
} else if (msg.startsWith("pbup:")) {
c->callback(c, P_BACK_UP);
} else if (msg.startsWith("pcdown:")) {
c->callback(c, P_CENTER_DOWN);
} else if (msg.startsWith("pcup:")) {
c->callback(c, P_CENTER_UP);
} else if (msg.startsWith("sactive:")) {
ESPUI.updateSwitcher(c->id, true);
c->callback(c, S_ACTIVE);
} else if (msg.startsWith("sinactive:")) {
ESPUI.updateSwitcher(c->id, false);
c->callback(c, S_INACTIVE);
} else if (msg.startsWith("slvalue:")) {
c->value = msg.substring(msg.indexOf(':') + 1, msg.lastIndexOf(':'));
ESPUI.updateControl(c);
c->callback(c, SL_VALUE);
} else if (msg.startsWith("nvalue:")) {
c->value = msg.substring(msg.indexOf(':') + 1, msg.lastIndexOf(':'));
c->callback(c, N_VALUE);
} else if (msg.startsWith("tvalue:")) {
c->value = msg.substring(msg.indexOf(':') + 1, msg.lastIndexOf(':'));
c->callback(c, T_VALUE);
} else if (msg.startsWith("svalue:")) {
c->value = msg.substring(msg.indexOf(':') + 1, msg.lastIndexOf(':'));
c->callback(c, S_VALUE);
} else {
if (ESPUI.verbosity) {
Serial.println("Malformated message from the websocket");
} }
}
} break;
if (msg.startsWith("bdown:")) { default:
c->callback(c, B_DOWN); break;
} else if (msg.startsWith("bup:")) {
c->callback(c, B_UP);
} else if (msg.startsWith("pfdown:")) {
c->callback(c, P_FOR_DOWN);
} else if (msg.startsWith("pfup:")) {
c->callback(c, P_FOR_UP);
} else if (msg.startsWith("pldown:")) {
c->callback(c, P_LEFT_DOWN);
} else if (msg.startsWith("plup:")) {
c->callback(c, P_LEFT_UP);
} else if (msg.startsWith("prdown:")) {
c->callback(c, P_RIGHT_DOWN);
} else if (msg.startsWith("prup:")) {
c->callback(c, P_RIGHT_UP);
} else if (msg.startsWith("pbdown:")) {
c->callback(c, P_BACK_DOWN);
} else if (msg.startsWith("pbup:")) {
c->callback(c, P_BACK_UP);
} else if (msg.startsWith("pcdown:")) {
c->callback(c, P_CENTER_DOWN);
} else if (msg.startsWith("pcup:")) {
c->callback(c, P_CENTER_UP);
} else if (msg.startsWith("sactive:")) {
ESPUI.updateSwitcher(c->id, true);
c->callback(c, S_ACTIVE);
} else if (msg.startsWith("sinactive:")) {
ESPUI.updateSwitcher(c->id, false);
c->callback(c, S_INACTIVE);
} else if (msg.startsWith("slvalue:")) {
c->value = msg.substring(msg.indexOf(':') + 1, msg.lastIndexOf(':'));
ESPUI.updateControl(c);
c->callback(c, SL_VALUE);
} else if (msg.startsWith("nvalue:")) {
c->value = msg.substring(msg.indexOf(':') + 1, msg.lastIndexOf(':'));
c->callback(c, N_VALUE);
} else if (msg.startsWith("tvalue:")) {
c->value = msg.substring(msg.indexOf(':') + 1, msg.lastIndexOf(':'));
c->callback(c, T_VALUE);
} else if (msg.startsWith("svalue:")) {
c->value = msg.substring(msg.indexOf(':') + 1, msg.lastIndexOf(':'));
c->callback(c, S_VALUE);
} else {
if (ESPUI.verbosity) {
Serial.println("Malformated message from the websocket");
}
}
} break;
default:
break;
} }
} }
uint16_t ESPUIClass::addControl(ControlType type, const char* label, uint16_t ESPUIClass::addControl(ControlType type, const char *label, String value, ControlColor color, uint16_t parentControl,
String value, ControlColor color, void (*callback)(Control *, int)) {
uint16_t parentControl, Control *control = new Control(type, label, callback, value, color, parentControl);
void (*callback)(Control*, int)) {
Control* control =
new Control(type, label, callback, value, color, parentControl);
if (this->controls == nullptr) { if (this->controls == nullptr) {
this->controls = control; this->controls = control;
} else { } else {
Control* iterator = this->controls; Control *iterator = this->controls;
while (iterator->next != nullptr) { while (iterator->next != nullptr) {
iterator = iterator->next; iterator = iterator->next;
@ -406,57 +402,40 @@ uint16_t ESPUIClass::addControl(ControlType type, const char* label,
return control->id; return control->id;
} }
int ESPUIClass::label(const char* label, ControlColor color, String value) { int ESPUIClass::label(const char *label, ControlColor color, String value) { return addControl(ControlType::Label, label, value, color); }
return addControl(ControlType::Label, label, value, color);
int ESPUIClass::graph(const char *label, ControlColor color) { return addControl(ControlType::Graph, label, "", color); }
int ESPUIClass::slider(const char *label, void (*callback)(Control *, int), ControlColor color, String value) {
return addControl(ControlType::Slider, label, "", color, Control::noParent, callback);
} }
int ESPUIClass::graph(const char* label, ControlColor color) { int ESPUIClass::button(const char *label, void (*callback)(Control *, int), ControlColor color, String value) {
return addControl(ControlType::Graph, label, "", color); return addControl(ControlType::Button, label, value, color, Control::noParent, callback);
} }
int ESPUIClass::slider(const char* label, void (*callback)(Control*, int), int ESPUIClass::switcher(const char *label, bool startState, void (*callback)(Control *, int), ControlColor color) {
ControlColor color, String value) { return addControl(ControlType::Switcher, label, "", color, Control::noParent, callback);
return addControl(ControlType::Slider, label, "", color, Control::noParent,
callback);
} }
int ESPUIClass::button(const char* label, void (*callback)(Control*, int), int ESPUIClass::pad(const char *label, bool center, void (*callback)(Control *, int), ControlColor color) {
ControlColor color, String value) {
return addControl(ControlType::Button, label, value, color, Control::noParent,
callback);
}
int ESPUIClass::switcher(const char* label, bool startState,
void (*callback)(Control*, int), ControlColor color) {
return addControl(ControlType::Switcher, label, "", color, Control::noParent,
callback);
}
int ESPUIClass::pad(const char* label, bool center,
void (*callback)(Control*, int), ControlColor color) {
if (center) { if (center) {
return addControl(ControlType::PadWithCenter, label, "", color, return addControl(ControlType::PadWithCenter, label, "", color, Control::noParent, callback);
Control::noParent, callback);
} else { } else {
return addControl(ControlType::Pad, label, "", color, Control::noParent, return addControl(ControlType::Pad, label, "", color, Control::noParent, callback);
callback);
} }
} }
int ESPUIClass::number(const char* label, void (*callback)(Control*, int), int ESPUIClass::number(const char *label, void (*callback)(Control *, int), ControlColor color, int number, int min, int max) {
ControlColor color, int number, int min, int max) { return addControl(ControlType::Number, label, String(number), color, Control::noParent, callback);
return addControl(ControlType::Number, label, String(number), color,
Control::noParent, callback);
} }
int ESPUIClass::text(const char* label, void (*callback)(Control*, int), int ESPUIClass::text(const char *label, void (*callback)(Control *, int), ControlColor color, String value) {
ControlColor color, String value) { return addControl(ControlType::Text, label, value, color, Control::noParent, callback);
return addControl(ControlType::Text, label, value, color, Control::noParent,
callback);
} }
Control* ESPUIClass::getControl(uint16_t id) { Control *ESPUIClass::getControl(uint16_t id) {
Control* control = this->controls; Control *control = this->controls;
while (control != nullptr) { while (control != nullptr) {
if (control->id == id) { if (control->id == id) {
@ -469,7 +448,7 @@ Control* ESPUIClass::getControl(uint16_t id) {
return nullptr; return nullptr;
} }
void ESPUIClass::updateControl(Control* control, int clientId) { void ESPUIClass::updateControl(Control *control, int clientId) {
if (!control) { if (!control) {
return; return;
} }
@ -514,19 +493,18 @@ void ESPUIClass::updateControl(Control* control, int clientId) {
} }
void ESPUIClass::updateControl(uint16_t id, int clientId) { void ESPUIClass::updateControl(uint16_t id, int clientId) {
Control* control = getControl(id); Control *control = getControl(id);
if (control) { if (control) {
updateControl(control, clientId); updateControl(control, clientId);
} else { } else {
if (this->verbosity) { if (this->verbosity) {
Serial.println(String("Error: There is no control with ID ") + Serial.println(String("Error: There is no control with ID ") + String(id));
String(id));
} }
} }
} }
void ESPUIClass::updateControl(Control* control, String value, int clientId) { void ESPUIClass::updateControl(Control *control, String value, int clientId) {
if (!control) { if (!control) {
return; return;
} }
@ -536,43 +514,30 @@ void ESPUIClass::updateControl(Control* control, String value, int clientId) {
} }
void ESPUIClass::updateControl(uint16_t id, String value, int clientId) { void ESPUIClass::updateControl(uint16_t id, String value, int clientId) {
Control* control = getControl(id); Control *control = getControl(id);
if (control) { if (control) {
updateControl(control, value, clientId); updateControl(control, value, clientId);
} else { } else {
if (this->verbosity) { if (this->verbosity) {
Serial.println(String("Error: There is no control with ID ") + Serial.println(String("Error: There is no control with ID ") + String(id));
String(id));
} }
} }
} }
void ESPUIClass::print(uint16_t id, String value) { updateControl(id, value); } void ESPUIClass::print(uint16_t id, String value) { updateControl(id, value); }
void ESPUIClass::updateLabel(uint16_t id, String value) { void ESPUIClass::updateLabel(uint16_t id, String value) { updateControl(id, value); }
updateControl(id, value);
}
void ESPUIClass::updateSlider(uint16_t id, int nValue, int clientId) { void ESPUIClass::updateSlider(uint16_t id, int nValue, int clientId) { updateControl(id, String(nValue), clientId); }
updateControl(id, String(nValue), clientId);
}
void ESPUIClass::updateSwitcher(uint16_t id, bool nValue, int clientId) { void ESPUIClass::updateSwitcher(uint16_t id, bool nValue, int clientId) { updateControl(id, String(nValue ? "1" : "0"), clientId); }
updateControl(id, String(nValue ? "1" : "0"), clientId);
}
void ESPUIClass::updateNumber(uint16_t id, int number, int clientId) { void ESPUIClass::updateNumber(uint16_t id, int number, int clientId) { updateControl(id, String(number), clientId); }
updateControl(id, String(number), clientId);
}
void ESPUIClass::updateText(uint16_t id, String text, int clientId) { void ESPUIClass::updateText(uint16_t id, String text, int clientId) { updateControl(id, text, clientId); }
updateControl(id, text, clientId);
}
void ESPUIClass::updateSelect(uint16_t id, String text, int clientId) { void ESPUIClass::updateSelect(uint16_t id, String text, int clientId) { updateControl(id, text, clientId); }
updateControl(id, text, clientId);
}
/* /*
Convert & Transfer Arduino elements to JSON elements Convert & Transfer Arduino elements to JSON elements
@ -580,13 +545,13 @@ Initially this function used to send the control element data individually.
Due to a change in the ESPAsyncWebserver library this had top be changed to be Due to a change in the ESPAsyncWebserver library this had top be changed to be
sent as one blob at the beginning. Therefore a new type is used as well sent as one blob at the beginning. Therefore a new type is used as well
*/ */
void ESPUIClass::jsonDom(AsyncWebSocketClient* client) { void ESPUIClass::jsonDom(AsyncWebSocketClient *client) {
String json; String json;
DynamicJsonDocument document(8000); DynamicJsonDocument document(8000);
document["type"] = (int)UI_INITIAL_GUI; document["type"] = (int)UI_INITIAL_GUI;
JsonArray items = document.createNestedArray("controls"); JsonArray items = document.createNestedArray("controls");
Control* control = this->controls; Control *control = this->controls;
JsonObject titleItem = items.createNestedObject(); JsonObject titleItem = items.createNestedObject();
titleItem["type"] = (int)UI_TITLE; titleItem["type"] = (int)UI_TITLE;
@ -628,8 +593,7 @@ void ESPUIClass::jsonDom(AsyncWebSocketClient* client) {
client->text(json); client->text(json);
} }
void ESPUIClass::beginSPIFFS(const char* _title, const char* username, void ESPUIClass::beginSPIFFS(const char *_title, const char *username, const char *password) {
const char* password) {
ui_title = _title; ui_title = _title;
this->basicAuthUsername = username; this->basicAuthUsername = username;
this->basicAuthPassword = password; this->basicAuthPassword = password;
@ -645,9 +609,7 @@ void ESPUIClass::beginSPIFFS(const char* _title, const char* username,
if (!SPIFFS.begin()) { if (!SPIFFS.begin()) {
if (ESPUI.verbosity) { if (ESPUI.verbosity) {
Serial.println( Serial.println("SPIFFS Mount Failed, PLEASE CHECK THE README ON HOW TO PREPARE YOUR ESP!!!!!!!");
"SPIFFS Mount Failed, PLEASE CHECK THE README ON HOW TO "
"PREPARE YOUR ESP!!!!!!!");
} }
return; return;
@ -659,9 +621,7 @@ void ESPUIClass::beginSPIFFS(const char* _title, const char* username,
if (!SPIFFS.exists("/index.htm")) { if (!SPIFFS.exists("/index.htm")) {
if (ESPUI.verbosity) { if (ESPUI.verbosity) {
Serial.println( Serial.println("Please read the README!!!!!!!, Make sure to ESPUI.prepareFileSystem() once in an empty sketch");
"Please read the README!!!!!!!, Make sure to "
"ESPUI.prepareFileSystem() once in an empty sketch");
} }
return; return;
@ -675,27 +635,21 @@ void ESPUIClass::beginSPIFFS(const char* _title, const char* username,
ws->setAuthentication(ESPUI.basicAuthUsername, ESPUI.basicAuthPassword); ws->setAuthentication(ESPUI.basicAuthUsername, ESPUI.basicAuthPassword);
} }
server->serveStatic("/", SPIFFS, "/") server->serveStatic("/", SPIFFS, "/").setDefaultFile("index.htm").setAuthentication(username, password);
.setDefaultFile("index.htm")
.setAuthentication(username, password);
} else { } else {
server->serveStatic("/", SPIFFS, "/").setDefaultFile("index.htm"); server->serveStatic("/", SPIFFS, "/").setDefaultFile("index.htm");
} }
// Heap for general Servertest // Heap for general Servertest
server->on("/heap", HTTP_GET, [](AsyncWebServerRequest* request) { server->on("/heap", HTTP_GET, [](AsyncWebServerRequest *request) {
if (ESPUI.basicAuth && !request->authenticate(ESPUI.basicAuthUsername, if (ESPUI.basicAuth && !request->authenticate(ESPUI.basicAuthUsername, ESPUI.basicAuthPassword)) {
ESPUI.basicAuthPassword)) {
return request->requestAuthentication(); return request->requestAuthentication();
} }
request->send(200, "text/plain", request->send(200, "text/plain", String(ESP.getFreeHeap()) + " In SPIFFSmode");
String(ESP.getFreeHeap()) + " In SPIFFSmode");
}); });
server->onNotFound( server->onNotFound([](AsyncWebServerRequest *request) { request->send(404); });
[](AsyncWebServerRequest* request) { request->send(404); });
server->begin(); server->begin();
@ -704,8 +658,7 @@ void ESPUIClass::beginSPIFFS(const char* _title, const char* username,
} }
} }
void ESPUIClass::begin(const char* _title, const char* username, void ESPUIClass::begin(const char *_title, const char *username, const char *password) {
const char* password) {
basicAuthUsername = username; basicAuthUsername = username;
basicAuthPassword = password; basicAuthPassword = password;
@ -723,114 +676,92 @@ void ESPUIClass::begin(const char* _title, const char* username,
ws->onEvent(onWsEvent); ws->onEvent(onWsEvent);
server->addHandler(ws); server->addHandler(ws);
if (basicAuth && WS_AUTHENTICATION) ws->setAuthentication(username, password); if (basicAuth && WS_AUTHENTICATION)
ws->setAuthentication(username, password);
server->on("/", HTTP_GET, [](AsyncWebServerRequest* request) { server->on("/", HTTP_GET, [](AsyncWebServerRequest *request) {
if (ESPUI.basicAuth && !request->authenticate(ESPUI.basicAuthUsername, if (ESPUI.basicAuth && !request->authenticate(ESPUI.basicAuthUsername, ESPUI.basicAuthPassword)) {
ESPUI.basicAuthPassword)) {
return request->requestAuthentication(); return request->requestAuthentication();
} }
AsyncWebServerResponse* response = AsyncWebServerResponse *response = request->beginResponse_P(200, "text/html", HTML_INDEX);
request->beginResponse_P(200, "text/html", HTML_INDEX);
request->send(response); request->send(response);
}); });
// Javascript files // Javascript files
server->on("/js/zepto.min.js", HTTP_GET, [](AsyncWebServerRequest* request) { server->on("/js/zepto.min.js", HTTP_GET, [](AsyncWebServerRequest *request) {
if (ESPUI.basicAuth && !request->authenticate(ESPUI.basicAuthUsername, if (ESPUI.basicAuth && !request->authenticate(ESPUI.basicAuthUsername, ESPUI.basicAuthPassword)) {
ESPUI.basicAuthPassword)) {
return request->requestAuthentication(); return request->requestAuthentication();
} }
AsyncWebServerResponse* response = request->beginResponse_P( AsyncWebServerResponse *response = request->beginResponse_P(200, "application/javascript", JS_ZEPTO_GZIP, sizeof(JS_ZEPTO_GZIP));
200, "application/javascript", JS_ZEPTO_GZIP, sizeof(JS_ZEPTO_GZIP));
response->addHeader("Content-Encoding", "gzip"); response->addHeader("Content-Encoding", "gzip");
request->send(response); request->send(response);
}); });
server->on("/js/controls.js", HTTP_GET, [](AsyncWebServerRequest* request) { server->on("/js/controls.js", HTTP_GET, [](AsyncWebServerRequest *request) {
if (ESPUI.basicAuth && !request->authenticate(ESPUI.basicAuthUsername, if (ESPUI.basicAuth && !request->authenticate(ESPUI.basicAuthUsername, ESPUI.basicAuthPassword)) {
ESPUI.basicAuthPassword)) {
return request->requestAuthentication(); return request->requestAuthentication();
} }
AsyncWebServerResponse* response = AsyncWebServerResponse *response = request->beginResponse_P(200, "application/javascript", JS_CONTROLS_GZIP, sizeof(JS_CONTROLS_GZIP));
request->beginResponse_P(200, "application/javascript",
JS_CONTROLS_GZIP, sizeof(JS_CONTROLS_GZIP));
response->addHeader("Content-Encoding", "gzip"); response->addHeader("Content-Encoding", "gzip");
request->send(response); request->send(response);
}); });
server->on("/js/slider.js", HTTP_GET, [](AsyncWebServerRequest* request) { server->on("/js/slider.js", HTTP_GET, [](AsyncWebServerRequest *request) {
if (ESPUI.basicAuth && !request->authenticate(ESPUI.basicAuthUsername, if (ESPUI.basicAuth && !request->authenticate(ESPUI.basicAuthUsername, ESPUI.basicAuthPassword)) {
ESPUI.basicAuthPassword)) {
return request->requestAuthentication(); return request->requestAuthentication();
} }
AsyncWebServerResponse* response = request->beginResponse_P( AsyncWebServerResponse *response = request->beginResponse_P(200, "application/javascript", JS_SLIDER_GZIP, sizeof(JS_SLIDER_GZIP));
200, "application/javascript", JS_SLIDER_GZIP, sizeof(JS_SLIDER_GZIP));
response->addHeader("Content-Encoding", "gzip"); response->addHeader("Content-Encoding", "gzip");
request->send(response); request->send(response);
}); });
server->on("/js/tabbedcontent.js", HTTP_GET, server->on("/js/tabbedcontent.js", HTTP_GET, [](AsyncWebServerRequest *request) {
[](AsyncWebServerRequest* request) { if (ESPUI.basicAuth && !request->authenticate(ESPUI.basicAuthUsername, ESPUI.basicAuthPassword)) {
if (ESPUI.basicAuth && return request->requestAuthentication();
!request->authenticate(ESPUI.basicAuthUsername, }
ESPUI.basicAuthPassword)) {
return request->requestAuthentication();
}
AsyncWebServerResponse* response = request->beginResponse_P( AsyncWebServerResponse *response = request->beginResponse_P(200, "application/javascript", JS_TABBEDCONTENT_GZIP, sizeof(JS_TABBEDCONTENT_GZIP));
200, "application/javascript", JS_TABBEDCONTENT_GZIP, response->addHeader("Content-Encoding", "gzip");
sizeof(JS_TABBEDCONTENT_GZIP)); request->send(response);
response->addHeader("Content-Encoding", "gzip"); });
request->send(response);
});
// Stylesheets // Stylesheets
server->on("/css/style.css", HTTP_GET, [](AsyncWebServerRequest* request) { server->on("/css/style.css", HTTP_GET, [](AsyncWebServerRequest *request) {
if (ESPUI.basicAuth && !request->authenticate(ESPUI.basicAuthUsername, if (ESPUI.basicAuth && !request->authenticate(ESPUI.basicAuthUsername, ESPUI.basicAuthPassword)) {
ESPUI.basicAuthPassword)) {
return request->requestAuthentication(); return request->requestAuthentication();
} }
AsyncWebServerResponse* response = request->beginResponse_P( AsyncWebServerResponse *response = request->beginResponse_P(200, "text/css", CSS_STYLE_GZIP, sizeof(CSS_STYLE_GZIP));
200, "text/css", CSS_STYLE_GZIP, sizeof(CSS_STYLE_GZIP));
response->addHeader("Content-Encoding", "gzip"); response->addHeader("Content-Encoding", "gzip");
request->send(response); request->send(response);
}); });
server->on( server->on("/css/normalize.css", HTTP_GET, [](AsyncWebServerRequest *request) {
"/css/normalize.css", HTTP_GET, [](AsyncWebServerRequest* request) { if (ESPUI.basicAuth && !request->authenticate(ESPUI.basicAuthUsername, ESPUI.basicAuthPassword)) {
if (ESPUI.basicAuth &&
!request->authenticate(ESPUI.basicAuthUsername,
ESPUI.basicAuthPassword)) {
return request->requestAuthentication();
}
AsyncWebServerResponse* response = request->beginResponse_P(
200, "text/css", CSS_NORMALIZE_GZIP, sizeof(CSS_NORMALIZE_GZIP));
response->addHeader("Content-Encoding", "gzip");
request->send(response);
});
// Heap for general Servertest
server->on("/heap", HTTP_GET, [](AsyncWebServerRequest* request) {
if (ESPUI.basicAuth && !request->authenticate(ESPUI.basicAuthUsername,
ESPUI.basicAuthPassword)) {
return request->requestAuthentication(); return request->requestAuthentication();
} }
request->send(200, "text/plain", AsyncWebServerResponse *response = request->beginResponse_P(200, "text/css", CSS_NORMALIZE_GZIP, sizeof(CSS_NORMALIZE_GZIP));
String(ESP.getFreeHeap()) + " In Memorymode"); response->addHeader("Content-Encoding", "gzip");
request->send(response);
}); });
server->onNotFound( // Heap for general Servertest
[](AsyncWebServerRequest* request) { request->send(404); }); server->on("/heap", HTTP_GET, [](AsyncWebServerRequest *request) {
if (ESPUI.basicAuth && !request->authenticate(ESPUI.basicAuthUsername, ESPUI.basicAuthPassword)) {
return request->requestAuthentication();
}
request->send(200, "text/plain", String(ESP.getFreeHeap()) + " In Memorymode");
});
server->onNotFound([](AsyncWebServerRequest *request) { request->send(404); });
server->begin(); server->begin();