1
0
mirror of https://github.com/s00500/ESPUI.git synced 2024-11-22 09:10:54 +00:00

Merge pull request #13 from s00500/development

Merging loads of changes
This commit is contained in:
Lukas Bachschwell 2018-05-27 12:01:48 +02:00 committed by GitHub
commit 9717f6c239
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 158 additions and 63 deletions

View File

@ -29,7 +29,7 @@ This library is dependent on the following libraries to function properly.
Make sure all the dependencies are installed, then install like so: Make sure all the dependencies are installed, then install like so:
#### Directly Through Arduino IDE #### Directly Through Arduino IDE (*recommended*)
You can find this Library in the Arduino IDE library manager You can find this Library in the Arduino IDE library manager
Go to Sketch > Include Library > Library Manager > Search for "ESPUI" > Install Go to Sketch > Include Library > Library Manager > Search for "ESPUI" > Install
@ -46,14 +46,19 @@ For macOs: Download the [Repository](https://github.com/s00500/ESPUI/archive/mas
Download the [Repository](https://github.com/s00500/ESPUI/archive/master.zip), Go to Sketch>Include Library>Add .zip Library> Select the Downloaded .zip File. Download the [Repository](https://github.com/s00500/ESPUI/archive/master.zip), Go to Sketch>Include Library>Add .zip Library> Select the Downloaded .zip File.
## Getting started (Filesystem upload) ## Getting started
ESPUI **NEEDS** its files burnt on the SPIFFS filesystem on the ESP. **Without this ESPUI will NOT work at all** ESPUI serves several Files to the browser to build up its webinterface. This can be achieved in 2 wasy:
There are now two ways to do this: you can either use the upload tool or you use the library function `ESPUI.prepareFileSystem()` *PROGMEM* or *SPIFFS*
#### Simple filesystem preparation (recomended) *When ESPUI.begin() is called the default is serving files from Memory and ESPUI should work out of the box!*
Just open the example sketch **prepareFileSystem** and run it on the ESP, (give it 5 - 10 seconds), But if this causes your program to *use too much memory* you can burn the files into the SPIFFS filesystem on the ESP.
There are now two ways to do this: you can either use the ESP file upload tool or you use the library function `ESPUI.prepareFileSystem()`
#### Simple filesystem preparation (*recommended*)
Just open the example sketch **prepareFileSystem** and run it on the ESP, (give it up to 30 seconds, you can see the status on the Serial Monitor),
The library will create all needed files. The library 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 use another chip :-) 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 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 unnecessary that is already saved in the chip's filesystem and you have more programm memory to work with. Now you can upload your normal sketch, when you do not call 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 programm memory to work with.
@ -76,21 +81,33 @@ Now you are set to go and use any code you want to with this library
Checkout the example for the usage Checkout the example for the usage
## Available colors:
- COLOR_TURQUOISE
- COLOR_EMERALD
- COLOR_PETERRIVER
- COLOR_WETASPHALT
- COLOR_SUNFLOWER
- COLOR_CARROT
- COLOR_ALIZARIN
- COLOR_NONE
## Roadmap : ## Roadmap :
- ~~Setup SPIFFS using values in program memory~~ - ~~Setup SPIFFS using values in program memory~~
- ~~ESP8266 support~~ - ~~ESP8266 support~~
- ~~PlattformIO Integration~~ - ~~PlattformIO Integration~~
- ~~Multiline Labels~~
- ~~GZip Files and serve from memory~~
- Datagraph output -> *WIP* - Datagraph output -> *WIP*
- Number input -> *WIP* - Number input -> *WIP*
- GZip Files to improve loadspeed and reduce server load - Text input -> *WIP*
- Document slider - Document slider
- New images in docu - New images in docu
- proper return value (as int and not as string) for slider - proper return value (as int and not as string) for slider
- Maybe a slider range setting, meanwhile please use map() - Maybe a slider range setting, meanwhile please use map()
- Improve slider stability - Improve slider stability
- Improve general stability - Improve general stability
- Multiline Labels
## Documentation ## Documentation
@ -130,6 +147,8 @@ Button pads come in two flavours: with or without a center button. They are very
Labels are a nice tool to get information from the robot to the user interface. This can be done to show states, values of sensors and configuration parameters. To send data from the code use ESP.print(labelId, “Text”); . Labels get a name on creation and a initial value. The name is not changeable once the UI initialised. Labels are a nice tool to get information from the robot to the user interface. This can be done to show states, values of sensors and configuration parameters. To send data from the code use ESP.print(labelId, “Text”); . Labels get a name on creation and a initial value. The name is not changeable once the UI initialised.
Labels automatically wrap your text. If you want them to have multiple lines use the normal `<br>` tag in the string you print to the label
#### Slider #### Slider
There is also an slider component now, needs to be documented though There is also an slider component now, needs to be documented though
@ -140,5 +159,8 @@ After all the elements are configured you can use ESPUI.begin(“Some Title”);
The library is designed to be easy to use and can still be extended with a lot of more functionality. The library is designed to be easy to use and can still be extended with a lot of more functionality.
# Notes for Development
All changes to the client side files can be made in the examples/gui/data directory. Using the file uploader thin can be used for development. After this you have to compress them and then you can gzip them. I wrote a little useful jsfiddle for this, [CHECK IT OUT](https://jsfiddle.net/s00500/yvLbhuuv/)
# Contribute # Contribute
Liked this Library? You can **support** me by sending me a :coffee: [Coffee](https://paypal.me/lukasbachschwell/3). Liked this Library? You can **support** me by sending me a :coffee: [Coffee](https://paypal.me/lukasbachschwell/3).

View File

@ -107,9 +107,9 @@ void setup(void) {
WiFi.mode(WIFI_AP); WiFi.mode(WIFI_AP);
#if defined(ESP32) #if defined(ESP32)
WiFi.setHostname(ssid); WiFi.setHostname(ssid);
#else #else
WiFi.hostname(ssid); WiFi.hostname(ssid);
#endif #endif
WiFi.softAP(ssid); WiFi.softAP(ssid);
@ -120,18 +120,18 @@ void setup(void) {
// change the beginning to this if you want to join an existing network // change the beginning to this if you want to join an existing network
/* /*
Serial.begin(115200); Serial.begin(115200);
WiFi.begin(ssid, password); WiFi.begin(ssid, password);
Serial.println(""); Serial.println("");
// Wait for connection // Wait for connection
while (WiFi.status() != WL_CONNECTED) { while (WiFi.status() != WL_CONNECTED) {
delay(500); delay(500);
Serial.print("."); Serial.print(".");
} }
Serial.println(""); Serial.println("");
Serial.print("IP address: "); Serial.print("IP address: ");
Serial.println(WiFi.localIP()); Serial.println(WiFi.localIP());
*/ */
ESPUI.label("Status:", COLOR_TURQUOISE, "Stop"); ESPUI.label("Status:", COLOR_TURQUOISE, "Stop");
ESPUI.label("Millis:", COLOR_EMERALD, "0"); ESPUI.label("Millis:", COLOR_EMERALD, "0");
@ -144,7 +144,7 @@ void setup(void) {
ESPUI.slider("Slider one", &slider, COLOR_ALIZARIN, "30"); ESPUI.slider("Slider one", &slider, COLOR_ALIZARIN, "30");
ESPUI.slider("Slider two", &slider, COLOR_NONE, "100"); ESPUI.slider("Slider two", &slider, COLOR_NONE, "100");
ESPUI.begin("ESP32 Control"); ESPUI.begin("ESPUI Control");
} }
void loop(void) { void loop(void) {

View File

@ -251,10 +251,13 @@ void onWsEvent(AsyncWebSocket *server, AsyncWebSocketClient *client,
ESPUI.updateSwitcher(c->id, false); ESPUI.updateSwitcher(c->id, false);
c->callback(*c, S_INACTIVE); c->callback(*c, S_INACTIVE);
} else if (msg.startsWith("slvalue:")) { } else if (msg.startsWith("slvalue:")) {
int value = int value = msg.substring(msg.indexOf(':') + 1, msg.lastIndexOf(':')).toInt();
msg.substring(msg.indexOf(':') + 1, msg.lastIndexOf(':')).toInt();
ESPUI.updateSlider(c->id, value, client->id()); ESPUI.updateSlider(c->id, value, client->id());
c->callback(*c, SL_VALUE); c->callback(*c, SL_VALUE);
} else if (msg.startsWith("nvalue:")) {
int value = msg.substring(msg.indexOf(':') + 1, msg.lastIndexOf(':')).toInt();
ESPUI.updateNumber(c->id, value, client->id());
c->callback(*c, N_VALUE);
} }
} }
break; break;
@ -265,9 +268,7 @@ void onWsEvent(AsyncWebSocket *server, AsyncWebSocketClient *client,
int ESPUIClass::label(const char *label, int color, String value) { int ESPUIClass::label(const char *label, int color, String value) {
if (labelExists(label)) { if (labelExists(label)) {
if (debug) if (debug) Serial.println("UI ERROR: Element " + String(label) + " exists, skipping creating element!");
Serial.println("UI ERROR: Element " + String(label) +
" exists, skipping creating element!");
return -1; return -1;
} }
@ -286,9 +287,24 @@ int ESPUIClass::label(const char *label, int color, String value) {
return cIndex - 1; return cIndex - 1;
} }
int ESPUIClass::graph(const char *label, int color) {
if (labelExists(label)) {
if (debug) Serial.println("UI ERROR: Element " + String(label) + " exists, skipping creating element!");
return -1;
}
Control *newG = new Control();
newG->type = UI_GRAPH;
newG->label = label;
newG->color = color;
newG->id = cIndex;
controls[cIndex] = newG;
cIndex++;
return cIndex - 1;
}
// TODO: this still needs a range setting // TODO: this still needs a range setting
int ESPUIClass::slider(const char *label, void (*callBack)(Control, int), int ESPUIClass::slider(const char *label, void (*callBack)(Control, int), int color, String value) {
int color, String value) {
if (labelExists(label)) { if (labelExists(label)) {
if (debug) if (debug)
Serial.println("UI ERROR: Element " + String(label) + Serial.println("UI ERROR: Element " + String(label) +
@ -337,8 +353,7 @@ int ESPUIClass::button(const char *label, void (*callBack)(Control, int),
return cIndex - 1; return cIndex - 1;
} }
int ESPUIClass::switcher(const char *label, bool startState, int ESPUIClass::switcher(const char *label, bool startState, void (*callBack)(Control, int), int color) {
void (*callBack)(Control, int), int color) {
if (labelExists(label)) { if (labelExists(label)) {
if (debug) if (debug)
Serial.println("UI ERROR: Element " + String(label) + Serial.println("UI ERROR: Element " + String(label) +
@ -381,6 +396,26 @@ int ESPUIClass::pad(const char *label, bool center,
return cIndex - 1; return cIndex - 1;
} }
// TODO: min and max need to be saved, they also need to be sent to the frontend
int ESPUIClass::number(const char *label, void (*callBack)(Control, int), int color, int number, int min, int max) {
if (labelExists(label)) {
if (debug)
Serial.println("UI ERROR: Element " + String(label) + " exists, skipping creating element!");
return -1;
}
Control *newN = new Control();
newN->type = UI_NUMBER;
newN->label = label;
newN->color = color;
newN->value = String(number);
newN->callback = callBack;
newN->id = cIndex;
controls[cIndex] = newN;
cIndex++;
return cIndex - 1;
}
void ESPUIClass::print(int id, String value) { void ESPUIClass::print(int id, String value) {
if (id < cIndex && controls[id]->type == UI_LABEL) { if (id < cIndex && controls[id]->type == UI_LABEL) {
controls[id]->value = value; controls[id]->value = value;
@ -408,24 +443,6 @@ void ESPUIClass::print(String label, String value) {
print(getIdByLabel(label), value); print(getIdByLabel(label), value);
} }
void ESPUIClass::updateSwitcher(int id, bool nValue, int clientId) {
if (id < cIndex && controls[id]->type == UI_SWITCHER) {
controls[id]->value = nValue ? 1 : 0;
String json;
StaticJsonBuffer<200> jsonBuffer;
JsonObject &root = jsonBuffer.createObject();
root["type"] = UPDATE_SWITCHER;
root["value"] = nValue ? 1 : 0;
root["id"] = String(id);
root.printTo(json);
textThem(json, clientId);
} else {
if (debug)
Serial.println(String("Error: ") + String(id) +
String(" is no switcher"));
}
}
void ESPUIClass::updateSlider(int id, int nValue, int clientId) { void ESPUIClass::updateSlider(int id, int nValue, int clientId) {
if (id < cIndex && controls[id]->type == UI_SLIDER) { if (id < cIndex && controls[id]->type == UI_SLIDER) {
controls[id]->value = nValue; controls[id]->value = nValue;
@ -443,16 +460,55 @@ void ESPUIClass::updateSlider(int id, int nValue, int clientId) {
} }
} }
void ESPUIClass::updateSwitcher(int id, bool nValue, int clientId) {
if (id < cIndex && controls[id]->type == UI_SWITCHER) {
controls[id]->value = nValue ? 1 : 0;
String json;
StaticJsonBuffer<200> jsonBuffer;
JsonObject &root = jsonBuffer.createObject();
root["type"] = UPDATE_SWITCHER;
root["value"] = nValue ? 1 : 0;
root["id"] = String(id);
root.printTo(json);
textThem(json, clientId);
} else {
if (debug) Serial.println(String("Error: ") + String(id) + String(" is no switcher"));
}
}
void ESPUIClass::updateSwitcher(String label, bool nValue, int clientId) { void ESPUIClass::updateSwitcher(String label, bool nValue, int clientId) {
if (!labelExists(label)) { if (!labelExists(label)) {
if (debug) if (debug)
Serial.println("UI ERROR: Element does not " + String(label) + Serial.println("UI ERROR: Element does not " + String(label) + " exist, cannot update!");
" exist, cannot update!");
return; return;
} }
updateSwitcher(getIdByLabel(label), nValue, clientId); updateSwitcher(getIdByLabel(label), nValue, clientId);
} }
void ESPUIClass::updateNumber(int id, int number, int clientId) {
if (id < cIndex && controls[id]->type == UI_NUMBER) {
controls[id]->value = number;
String json;
StaticJsonBuffer<200> jsonBuffer;
JsonObject &root = jsonBuffer.createObject();
root["type"] = UPDATE_NUMBER;
root["value"] = String(number);
root["id"] = String(id);
root.printTo(json);
textThem(json, clientId);
} else {
if (debug) Serial.println(String("Error: ") + String(id) + String(" is no number"));
}
}
void ESPUIClass::updateNumber(String label, int number, int clientId) {
if (!labelExists(label)) {
if (debug) Serial.println("UI ERROR: Element does not " + String(label) + " exist, cannot update!");
return;
}
updateNumber(getIdByLabel(label), number, clientId);
}
// This is a hacky workaround because ESPAsyncWebServer does not have a function // This is a hacky workaround because ESPAsyncWebServer does not have a function
// like this and it's clients array is private // like this and it's clients array is private
void ESPUIClass::textThem(String text, int clientId) { void ESPUIClass::textThem(String text, int clientId) {

View File

@ -39,7 +39,7 @@ typedef struct Control {
unsigned int color; unsigned int color;
} Control; } Control;
// Types // Message Types (and control types)
#define UI_TITEL 0 #define UI_TITEL 0
#define UI_LABEL 1 #define UI_LABEL 1
#define UI_BUTTON 2 #define UI_BUTTON 2
@ -50,6 +50,12 @@ typedef struct Control {
#define UPDATE_SWITCHER 7 #define UPDATE_SWITCHER 7
#define UI_SLIDER 8 #define UI_SLIDER 8
#define UPDATE_SLIDER 9 #define UPDATE_SLIDER 9
#define UI_NUMBER 10
#define UPDATE_NUMBER 11
#define UI_GRAPH 12
#define CLEAR_GRAPH 13
#define ADD_GRAPH_POINT 14
// Values // Values
#define B_DOWN -1 #define B_DOWN -1
@ -70,7 +76,7 @@ typedef struct Control {
#define S_INACTIVE 7 #define S_INACTIVE 7
#define SL_VALUE 8 #define SL_VALUE 8
#define N_VALUE 9
// Colors // Colors
#define COLOR_TURQUOISE 0 #define COLOR_TURQUOISE 0
@ -92,15 +98,16 @@ void beginSPIFFS(const char *_title); // Setup servers and page in SPIFFSmode
void prepareFileSystem(); // Initially preps the filesystem and loads a lot of stuff into SPIFFS void prepareFileSystem(); // Initially preps the filesystem and loads a lot of stuff into SPIFFS
void list(); void list();
// Creating Elements // Creating Elements
int label(const char *label, int color, String value = ""); // Create Label
int button(const char *label, void (*callBack)(Control, int), int color, int button(const char *label, void (*callBack)(Control, int), int color, String value = ""); // Create Event Button
String value = ""); // Create Event Button int switcher(const char *label, bool startState, void (*callBack)(Control, int), int color); // Create Toggle Button
int switcher(const char *label, bool startState, int pad(const char *label, bool centerButton, void (*callBack)(Control, int), int color); // Create Pad Control
void (*callBack)(Control, int), int slider(const char *label, void (*callBack)(Control, int), int color, String value); // Create Slider Control
int color); // Create Toggle Button int number(const char *label, void (*callBack)(Control, int), int color, int number, int min, int max); // Create a Number Input Control
int pad(const char *label, bool centerButton, void (*callBack)(Control, int),
int color); // Create Pad Control // Output only
int slider(const char *label, void (*callBack)(Control, int), int color, String value); // Create Slider Control int label(const char *label, int color, String value = ""); // Create Label
int graph(const char *label, int color); // Create Graph display
// Update Elements // Update Elements
void print(int id, String value); void print(int id, String value);
@ -112,6 +119,16 @@ void updateSwitcher(String label, bool nValue, int clientId = -1);
void updateSlider(int id, int nValue, int clientId = -1); void updateSlider(int id, int nValue, int clientId = -1);
void updateSlider(String label, int nValue, int clientId = -1); void updateSlider(String label, int nValue, int clientId = -1);
void updateNumber(int id, int nValue, int clientId = -1);
void updateNumber(String label, int nValue, int clientId = -1);
void clearGraph(int id, int clientId = -1);
void clearGraph(String label, int clientId = -1);
void addGraphPoint(int id, int nValue, int clientId = -1);
void addGraphPoint(String label, int nValue, int clientId = -1);
void textThem(String text, int clientId); void textThem(String text, int clientId);
// Variables --- // Variables ---