mirror of
https://github.com/s00500/ESPUI.git
synced 2025-07-04 01:00:19 +00:00
Compare commits
9 Commits
Author | SHA1 | Date | |
---|---|---|---|
7e4dbd7e03 | |||
6dabc32905 | |||
feedd21413 | |||
851c363aba | |||
c72bff5b2e | |||
679bf1a5c1 | |||
8150ba8f82 | |||
4af48ce87d | |||
ad9033cfe3 |
@ -12,60 +12,6 @@ const char *password = "";
|
|||||||
long oldTime = 0;
|
long oldTime = 0;
|
||||||
bool switchi = false;
|
bool switchi = false;
|
||||||
|
|
||||||
void setup(void) {
|
|
||||||
Serial.begin(115200);
|
|
||||||
WiFi.mode(WIFI_AP);
|
|
||||||
|
|
||||||
#if defined(ESP32)
|
|
||||||
WiFi.setHostname(ssid);
|
|
||||||
#else
|
|
||||||
WiFi.hostname(ssid);
|
|
||||||
#endif
|
|
||||||
|
|
||||||
WiFi.softAP(ssid);
|
|
||||||
// WiFi.softAP(ssid, password);
|
|
||||||
Serial.println("");
|
|
||||||
Serial.print("IP address: ");
|
|
||||||
Serial.println(WiFi.softAPIP());
|
|
||||||
|
|
||||||
// change the beginning to this if you want to join an existing network
|
|
||||||
/*
|
|
||||||
Serial.begin(115200);
|
|
||||||
WiFi.begin(ssid, password);
|
|
||||||
Serial.println("");
|
|
||||||
// Wait for connection
|
|
||||||
while (WiFi.status() != WL_CONNECTED) {
|
|
||||||
delay(500);
|
|
||||||
Serial.print(".");
|
|
||||||
}
|
|
||||||
Serial.println("");
|
|
||||||
Serial.print("IP address: ");
|
|
||||||
Serial.println(WiFi.localIP());
|
|
||||||
*/
|
|
||||||
|
|
||||||
ESPUI.label("Status:", COLOR_TURQUOISE, "Stop");
|
|
||||||
ESPUI.label("Millis:", COLOR_EMERALD, "0");
|
|
||||||
ESPUI.button("Push Button", &buttonCallback, COLOR_PETERRIVER);
|
|
||||||
ESPUI.button("Other Button", &buttonExample, COLOR_WETASPHALT, "Press");
|
|
||||||
ESPUI.pad("Pad with center", true, &padExample, COLOR_SUNFLOWER);
|
|
||||||
ESPUI.pad("Pad without center", false, &padExample, COLOR_CARROT);
|
|
||||||
ESPUI.switcher("Switch one", false, &switchExample, COLOR_ALIZARIN);
|
|
||||||
ESPUI.switcher("Switch two", true, &otherSwitchExample, COLOR_NONE);
|
|
||||||
ESPUI.slider("Slider one", &slider, COLOR_ALIZARIN, "30");
|
|
||||||
ESPUI.slider("Slider two", &slider, COLOR_NONE, "100");
|
|
||||||
|
|
||||||
ESPUI.begin("ESP32 Control");
|
|
||||||
}
|
|
||||||
|
|
||||||
void loop(void) {
|
|
||||||
if (millis() - oldTime > 5000) {
|
|
||||||
ESPUI.print("Millis:", String(millis()));
|
|
||||||
switchi = !switchi;
|
|
||||||
ESPUI.updateSwitcher("Switch one", switchi);
|
|
||||||
oldTime = millis();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
void slider(Control sender, int type) {
|
void slider(Control sender, int type) {
|
||||||
Serial.println(sender.value);
|
Serial.println(sender.value);
|
||||||
}
|
}
|
||||||
@ -155,3 +101,57 @@ void otherSwitchExample(Control sender, int value) {
|
|||||||
Serial.print(" ");
|
Serial.print(" ");
|
||||||
Serial.println(sender.id);
|
Serial.println(sender.id);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void setup(void) {
|
||||||
|
Serial.begin(115200);
|
||||||
|
WiFi.mode(WIFI_AP);
|
||||||
|
|
||||||
|
#if defined(ESP32)
|
||||||
|
WiFi.setHostname(ssid);
|
||||||
|
#else
|
||||||
|
WiFi.hostname(ssid);
|
||||||
|
#endif
|
||||||
|
|
||||||
|
WiFi.softAP(ssid);
|
||||||
|
// WiFi.softAP(ssid, password);
|
||||||
|
Serial.println("");
|
||||||
|
Serial.print("IP address: ");
|
||||||
|
Serial.println(WiFi.softAPIP());
|
||||||
|
|
||||||
|
// change the beginning to this if you want to join an existing network
|
||||||
|
/*
|
||||||
|
Serial.begin(115200);
|
||||||
|
WiFi.begin(ssid, password);
|
||||||
|
Serial.println("");
|
||||||
|
// Wait for connection
|
||||||
|
while (WiFi.status() != WL_CONNECTED) {
|
||||||
|
delay(500);
|
||||||
|
Serial.print(".");
|
||||||
|
}
|
||||||
|
Serial.println("");
|
||||||
|
Serial.print("IP address: ");
|
||||||
|
Serial.println(WiFi.localIP());
|
||||||
|
*/
|
||||||
|
|
||||||
|
ESPUI.label("Status:", COLOR_TURQUOISE, "Stop");
|
||||||
|
ESPUI.label("Millis:", COLOR_EMERALD, "0");
|
||||||
|
ESPUI.button("Push Button", &buttonCallback, COLOR_PETERRIVER);
|
||||||
|
ESPUI.button("Other Button", &buttonExample, COLOR_WETASPHALT, "Press");
|
||||||
|
ESPUI.pad("Pad with center", true, &padExample, COLOR_SUNFLOWER);
|
||||||
|
ESPUI.pad("Pad without center", false, &padExample, COLOR_CARROT);
|
||||||
|
ESPUI.switcher("Switch one", false, &switchExample, COLOR_ALIZARIN);
|
||||||
|
ESPUI.switcher("Switch two", true, &otherSwitchExample, COLOR_NONE);
|
||||||
|
ESPUI.slider("Slider one", &slider, COLOR_ALIZARIN, "30");
|
||||||
|
ESPUI.slider("Slider two", &slider, COLOR_NONE, "100");
|
||||||
|
|
||||||
|
ESPUI.begin("ESP32 Control");
|
||||||
|
}
|
||||||
|
|
||||||
|
void loop(void) {
|
||||||
|
if (millis() - oldTime > 5000) {
|
||||||
|
ESPUI.print("Millis:", String(millis()));
|
||||||
|
switchi = !switchi;
|
||||||
|
ESPUI.updateSwitcher("Switch one", switchi);
|
||||||
|
oldTime = millis();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
@ -1,5 +1,5 @@
|
|||||||
name=ESPUI
|
name=ESPUI
|
||||||
version=1.4.0
|
version=1.4.4
|
||||||
author=Lukas Bachschwell
|
author=Lukas Bachschwell
|
||||||
maintainer=Lukas Bachschwell <lukas@lbsfilm.at>
|
maintainer=Lukas Bachschwell <lukas@lbsfilm.at>
|
||||||
sentence=ESP32 and ESP8266 Web Interface Library
|
sentence=ESP32 and ESP8266 Web Interface Library
|
||||||
|
122
src/ESPUI.cpp
122
src/ESPUI.cpp
@ -14,25 +14,58 @@
|
|||||||
#include <functional>
|
#include <functional>
|
||||||
|
|
||||||
// ################# Spiffs functions
|
// ################# Spiffs functions
|
||||||
void deleteFile(fs::FS &fs, const char * path){
|
|
||||||
if(!fs.exists(path)){
|
void listDir(const char * dirname, uint8_t levels){
|
||||||
|
Serial.printf("Listing directory: %s\n", dirname);
|
||||||
|
|
||||||
|
File root = SPIFFS.open(dirname);
|
||||||
|
if(!root){
|
||||||
|
Serial.println("Failed to open directory");
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
if(!root.isDirectory()){
|
||||||
|
Serial.println("Not a directory");
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
File file = root.openNextFile();
|
||||||
|
while(file){
|
||||||
|
if(file.isDirectory()){
|
||||||
|
Serial.print(" DIR : ");
|
||||||
|
Serial.println(file.name());
|
||||||
|
if(levels){
|
||||||
|
listDir(file.name(), levels -1);
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
Serial.print(" FILE: ");
|
||||||
|
Serial.print(file.name());
|
||||||
|
Serial.print(" SIZE: ");
|
||||||
|
Serial.println(file.size());
|
||||||
|
}
|
||||||
|
file = root.openNextFile();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
void deleteFile(const char * path) {
|
||||||
|
Serial.print(SPIFFS.exists(path));
|
||||||
|
if(!SPIFFS.exists(path)){
|
||||||
Serial.printf("File: %s does not exist, not deleting\n", path);
|
Serial.printf("File: %s does not exist, not deleting\n", path);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
Serial.printf("Deleting file: %s\n", path);
|
Serial.printf("Deleting file: %s\n", path);
|
||||||
|
|
||||||
if(fs.remove(path)){
|
if(SPIFFS.remove(path)){
|
||||||
Serial.println("File deleted");
|
Serial.println("File deleted");
|
||||||
} else {
|
} else {
|
||||||
Serial.println("Delete failed");
|
Serial.println("Delete failed");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void writeFile(fs::FS &fs, const char * path, const char * data){
|
void writeFile(const char * path, const char * data) {
|
||||||
Serial.printf("Writing file: %s\n", path);
|
Serial.printf("Writing file: %s\n", path);
|
||||||
|
|
||||||
File file = fs.open(path, FILE_WRITE);
|
File file = SPIFFS.open(path, FILE_WRITE);
|
||||||
if(!file){
|
if(!file){
|
||||||
Serial.println("Failed to open file for writing");
|
Serial.println("Failed to open file for writing");
|
||||||
return;
|
return;
|
||||||
@ -42,6 +75,7 @@ void writeFile(fs::FS &fs, const char * path, const char * data){
|
|||||||
} else {
|
} else {
|
||||||
Serial.println("Write failed");
|
Serial.println("Write failed");
|
||||||
}
|
}
|
||||||
|
file.close();
|
||||||
}
|
}
|
||||||
|
|
||||||
// end Spiffs functions
|
// end Spiffs functions
|
||||||
@ -49,40 +83,61 @@ void writeFile(fs::FS &fs, const char * path, const char * data){
|
|||||||
void ESPUIClass::prepareFileSystem(){
|
void ESPUIClass::prepareFileSystem(){
|
||||||
// this function should only be used once
|
// this function should only be used once
|
||||||
|
|
||||||
Serial.println('About to prepare filesystem...');
|
Serial.println("About to prepare filesystem...");
|
||||||
|
|
||||||
#if defined(ESP32)
|
#if defined(ESP32)
|
||||||
|
SPIFFS.format();
|
||||||
if(!SPIFFS.begin(true)) {
|
if(!SPIFFS.begin(true)) {
|
||||||
Serial.println("SPIFFS Mount Failed");
|
Serial.println("SPIFFS Mount Failed");
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
Serial.println("SPIFFS Mount ESP32 Done");
|
||||||
#else
|
#else
|
||||||
SPIFFS.begin();
|
|
||||||
SPIFFS.format();
|
SPIFFS.format();
|
||||||
|
SPIFFS.begin();
|
||||||
|
Serial.println("SPIFFS Mount ESP8266 Done");
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
deleteFile(SPIFFS, "/index.htm");
|
listDir("/", 1);
|
||||||
|
|
||||||
deleteFile(SPIFFS, "/css/style.css");
|
//TODO: This is a workaround, have to find out why SPIFFS on ESP32 behaves incredibly strangely, see issue #6
|
||||||
deleteFile(SPIFFS, "/css/normalize.css");
|
/*
|
||||||
|
deleteFile("/index.htm");
|
||||||
|
|
||||||
deleteFile(SPIFFS, "/js/controls.js");
|
deleteFile("/css/style.css");
|
||||||
deleteFile(SPIFFS, "/js/zepto.js");
|
deleteFile("/css/normalize.css");
|
||||||
deleteFile(SPIFFS, "/js/slider.js");
|
|
||||||
|
|
||||||
Serial.println('Cleanup done');
|
deleteFile("/js/zepto.min.js");
|
||||||
|
deleteFile("/js/controls.js");
|
||||||
|
deleteFile("/js/slider.js");
|
||||||
|
*/
|
||||||
|
|
||||||
|
Serial.println("Cleanup done");
|
||||||
|
|
||||||
// Now write
|
// Now write
|
||||||
writeFile(SPIFFS, "/index.htm", HTML_INDEX);
|
writeFile("/index.htm", HTML_INDEX);
|
||||||
|
|
||||||
writeFile(SPIFFS, "/css/style.css", CSS_STYLE);
|
writeFile("/css/style.css", CSS_STYLE);
|
||||||
writeFile(SPIFFS, "/css/normalize.css", CSS_NORMALIZE);
|
writeFile("/css/normalize.css", CSS_NORMALIZE);
|
||||||
|
|
||||||
writeFile(SPIFFS, "/js/zepto.js", JS_ZEPTO);
|
writeFile("/js/zepto.min.js", JS_ZEPTO);
|
||||||
writeFile(SPIFFS, "/js/controls.js", JS_CONTROLS);
|
writeFile("/js/controls.js", JS_CONTROLS);
|
||||||
writeFile(SPIFFS, "/js/slider.js", JS_SLIDER);
|
writeFile("/js/slider.js", JS_SLIDER);
|
||||||
|
|
||||||
Serial.println("Done Initializing filesystem :-)");
|
Serial.println("Done Initializing filesystem :-)");
|
||||||
|
listDir("/", 1);
|
||||||
|
SPIFFS.end();
|
||||||
|
}
|
||||||
|
|
||||||
|
void ESPUIClass::list() {
|
||||||
|
if(!SPIFFS.begin()) {
|
||||||
|
Serial.println("SPIFFS Mount Failed");
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
listDir("/", 1);
|
||||||
|
|
||||||
|
Serial.println(SPIFFS.totalBytes());
|
||||||
|
Serial.println(SPIFFS.usedBytes());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@ -95,12 +150,17 @@ void onWsEvent(AsyncWebSocket *server, AsyncWebSocketClient *client,
|
|||||||
Serial.printf("Disconnected!\n");
|
Serial.printf("Disconnected!\n");
|
||||||
break;
|
break;
|
||||||
case WS_EVT_CONNECT: {
|
case WS_EVT_CONNECT: {
|
||||||
if (debug)
|
if (debug){
|
||||||
Serial.println("Connected");
|
Serial.print("Connected: ");
|
||||||
|
Serial.println(client->id());
|
||||||
|
}
|
||||||
|
|
||||||
ESPUI.jsonDom(client);
|
ESPUI.jsonDom(client);
|
||||||
if (debug)
|
if (debug){
|
||||||
Serial.println("JSON Data Sent to Client!");
|
Serial.println("JSON Data Sent to Client!");
|
||||||
} break;
|
}
|
||||||
|
}
|
||||||
|
break;
|
||||||
case WS_EVT_DATA:
|
case WS_EVT_DATA:
|
||||||
String msg = "";
|
String msg = "";
|
||||||
for (size_t i = 0; i < len; i++) {
|
for (size_t i = 0; i < len; i++) {
|
||||||
@ -337,11 +397,17 @@ void ESPUIClass::updateSwitcher(String label, bool nValue, int clientId) {
|
|||||||
updateSwitcher(getIdByLabel(label), nValue, clientId);
|
updateSwitcher(getIdByLabel(label), nValue, clientId);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// This is a hacky workaround because ESPAsyncWebServer does not have a function like this and it's clients array is private
|
||||||
void ESPUIClass::textThem(String text, int clientId){
|
void ESPUIClass::textThem(String text, int clientId){
|
||||||
for(int i = 1; i <= this->ws->count(); i++){
|
int tryId = 0;
|
||||||
if(clientId!=i){
|
for(int count = 0; count < this->ws->count();){
|
||||||
this->ws->client(i)->text(text);
|
if(this->ws->hasClient(tryId)) {
|
||||||
|
if(clientId!=tryId){
|
||||||
|
this->ws->client(tryId)->text(text);
|
||||||
}
|
}
|
||||||
|
count++;
|
||||||
|
}
|
||||||
|
tryId++;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -387,10 +453,12 @@ void ESPUIClass::begin(const char *_title) {
|
|||||||
ui_title = _title;
|
ui_title = _title;
|
||||||
server = new AsyncWebServer(80);
|
server = new AsyncWebServer(80);
|
||||||
ws = new AsyncWebSocket("/ws");
|
ws = new AsyncWebSocket("/ws");
|
||||||
|
|
||||||
if(!SPIFFS.begin()) {
|
if(!SPIFFS.begin()) {
|
||||||
Serial.println("SPIFFS Mount Failed, PLEASE CHECK THE README ON HOW TO PREPARE YOUR ESP!!!!!!!");
|
Serial.println("SPIFFS Mount Failed, PLEASE CHECK THE README ON HOW TO PREPARE YOUR ESP!!!!!!!");
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
listDir("/", 1);
|
||||||
|
|
||||||
if(!SPIFFS.exists( "/index.htm")) {
|
if(!SPIFFS.exists( "/index.htm")) {
|
||||||
Serial.println("Please read the README!!!!!!!, Make sure to ESPUI.prepareFileSystem() once in an empty sketch");
|
Serial.println("Please read the README!!!!!!!, Make sure to ESPUI.prepareFileSystem() once in an empty sketch");
|
||||||
|
@ -89,7 +89,7 @@ public:
|
|||||||
void begin(const char *_title); // Setup servers and page
|
void begin(const char *_title); // Setup servers and page
|
||||||
|
|
||||||
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();
|
||||||
// Creating Elements
|
// Creating Elements
|
||||||
void label(const char *label, int color, String value = ""); // Create Label
|
void label(const char *label, int color, String value = ""); // Create Label
|
||||||
void button(const char *label, void (*callBack)(Control, int), int color,
|
void button(const char *label, void (*callBack)(Control, int), int color,
|
||||||
|
Reference in New Issue
Block a user