mirror of
https://github.com/nikdoof/busylight.git
synced 2025-12-15 18:52:24 +00:00
Improve webserver page
This commit is contained in:
46
data/index.html
Normal file
46
data/index.html
Normal file
@@ -0,0 +1,46 @@
|
|||||||
|
<html>
|
||||||
|
<head>
|
||||||
|
<style>
|
||||||
|
body {
|
||||||
|
background-color: black;
|
||||||
|
}
|
||||||
|
a {
|
||||||
|
display: inline-block;
|
||||||
|
padding: 0.35em 1.2em;
|
||||||
|
border: 0.1em solid #FFFFFF;
|
||||||
|
margin: 0 0.3em 0.3em 0;
|
||||||
|
border-radius: 0.12em;
|
||||||
|
box-sizing: border-box;
|
||||||
|
text-decoration: none;
|
||||||
|
font-family: 'Roboto', sans-serif;
|
||||||
|
font-weight: 300;
|
||||||
|
color: #FFFFFF;
|
||||||
|
text-align: center;
|
||||||
|
transition: all 0.2s;
|
||||||
|
width: 100%;
|
||||||
|
}
|
||||||
|
|
||||||
|
a.active {
|
||||||
|
background-color: grey;
|
||||||
|
color: black;
|
||||||
|
}
|
||||||
|
|
||||||
|
a:hover {
|
||||||
|
color: #000000;
|
||||||
|
background-color: #FFFFFF;
|
||||||
|
}
|
||||||
|
|
||||||
|
@media all and (max-width:30em) {
|
||||||
|
a {
|
||||||
|
display: block;
|
||||||
|
margin: 0.4em auto;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
</style>
|
||||||
|
</head>
|
||||||
|
|
||||||
|
<body>
|
||||||
|
~LIST~
|
||||||
|
</body>
|
||||||
|
|
||||||
|
</html>
|
||||||
116
include/ESPTemplateProcessor.h
Normal file
116
include/ESPTemplateProcessor.h
Normal file
@@ -0,0 +1,116 @@
|
|||||||
|
#ifndef ESP_TEMPLATE_PROCESSOR_H
|
||||||
|
#define ESP_TEMPLATE_PROCESSOR_H
|
||||||
|
|
||||||
|
#ifdef ESP8266
|
||||||
|
#define WebServer ESP8266WebServer
|
||||||
|
#include <ESP8266WebServer.h>
|
||||||
|
#else
|
||||||
|
#include <WebServer.h>
|
||||||
|
#endif
|
||||||
|
|
||||||
|
#include <LittleFS.h>
|
||||||
|
|
||||||
|
typedef String ProcessorCallback(const String& key);
|
||||||
|
|
||||||
|
class ESPTemplateProcessor {
|
||||||
|
public:
|
||||||
|
ESPTemplateProcessor(WebServer& _server) :
|
||||||
|
server(_server)
|
||||||
|
{
|
||||||
|
}
|
||||||
|
|
||||||
|
bool send(const String& filePath, ProcessorCallback& processor, char bookend = '~', bool silentSerial = false)
|
||||||
|
{
|
||||||
|
// Open file.
|
||||||
|
if(!LittleFS.exists(filePath)) {
|
||||||
|
if(!silentSerial) {
|
||||||
|
Serial.print("Cannot process "); Serial.print(filePath); Serial.println(": Does not exist.");
|
||||||
|
}
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
File file = LittleFS.open(filePath, "r");
|
||||||
|
if (!file) {
|
||||||
|
if(!silentSerial) {
|
||||||
|
Serial.print("Cannot process "); Serial.print(filePath); Serial.println(": Failed to open.");
|
||||||
|
}
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
server.setContentLength(CONTENT_LENGTH_UNKNOWN);
|
||||||
|
server.sendHeader("Content-Type","text/html",true);
|
||||||
|
server.sendHeader("Cache-Control","no-cache");
|
||||||
|
server.send(200);
|
||||||
|
//server.sendContent(<chunk>)
|
||||||
|
|
||||||
|
// Process!
|
||||||
|
static const uint16_t MAX = 100;
|
||||||
|
String buffer;
|
||||||
|
int bufferLen = 0;
|
||||||
|
String keyBuffer;
|
||||||
|
int val;
|
||||||
|
char ch;
|
||||||
|
while ((val = file.read()) != -1) {
|
||||||
|
ch = char(val);
|
||||||
|
|
||||||
|
// Lookup expansion.
|
||||||
|
if (ch == bookend) {
|
||||||
|
// Clear out buffer.
|
||||||
|
server.sendContent(buffer);
|
||||||
|
buffer = "";
|
||||||
|
bufferLen = 0;
|
||||||
|
|
||||||
|
// Process substitution.
|
||||||
|
keyBuffer = "";
|
||||||
|
bool found = false;
|
||||||
|
while (!found && (val = file.read()) != -1) {
|
||||||
|
ch = char(val);
|
||||||
|
if (ch == bookend) {
|
||||||
|
found = true;
|
||||||
|
} else {
|
||||||
|
keyBuffer += ch;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Check for bad exit.
|
||||||
|
if (val == -1 && !found) {
|
||||||
|
if(!silentSerial) {
|
||||||
|
Serial.print("Cannot process "); Serial.print(filePath); Serial.println(": Unable to parse.");
|
||||||
|
}
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Get substitution
|
||||||
|
String processed = processor(keyBuffer);
|
||||||
|
if(!silentSerial) {
|
||||||
|
Serial.print("Lookup '"); Serial.print(keyBuffer); Serial.print("' received: "); Serial.println(processed);
|
||||||
|
}
|
||||||
|
server.sendContent(processed);
|
||||||
|
} else {
|
||||||
|
bufferLen++;
|
||||||
|
buffer += ch;
|
||||||
|
if (bufferLen >= MAX) {
|
||||||
|
server.sendContent(buffer);
|
||||||
|
bufferLen = 0;
|
||||||
|
buffer = "";
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if (val == -1) {
|
||||||
|
server.sendContent(buffer);
|
||||||
|
server.sendContent("");
|
||||||
|
return true;
|
||||||
|
} else {
|
||||||
|
if(!silentSerial) {
|
||||||
|
Serial.print("Failed to process '"); Serial.print(filePath); Serial.println("': Didn't reach the end of the file.");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
private:
|
||||||
|
WebServer &server;
|
||||||
|
};
|
||||||
|
|
||||||
|
#endif
|
||||||
@@ -11,6 +11,7 @@
|
|||||||
[env:wemos-d1]
|
[env:wemos-d1]
|
||||||
platform = espressif8266
|
platform = espressif8266
|
||||||
board = d1
|
board = d1
|
||||||
|
board_build.filesystem = littlefs
|
||||||
upload_speed = 460800
|
upload_speed = 460800
|
||||||
framework = arduino
|
framework = arduino
|
||||||
lib_deps =
|
lib_deps =
|
||||||
|
|||||||
@@ -12,7 +12,9 @@ WiFiClientSecure wifi_secure;
|
|||||||
char current_state[20];
|
char current_state[20];
|
||||||
|
|
||||||
#ifdef ENABLE_WEBSERVER
|
#ifdef ENABLE_WEBSERVER
|
||||||
|
#include <LittleFS.h>
|
||||||
#include <ESP8266WebServer.h>
|
#include <ESP8266WebServer.h>
|
||||||
|
#include "ESPTemplateProcessor.h"
|
||||||
ESP8266WebServer server;
|
ESP8266WebServer server;
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
@@ -79,22 +81,29 @@ bool Switch_Status(String name) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
#ifdef ENABLE_WEBSERVER
|
#ifdef ENABLE_WEBSERVER
|
||||||
void IndexPage() {
|
String ProcessIndexPage(const String& var) {
|
||||||
char buffer[1024] = "<html><head><title>Busylight</title></head><body>";
|
Serial.println(current_state);
|
||||||
|
if (var == "LIST") {
|
||||||
|
String buffer;
|
||||||
for(int j=0; j<STATUSES_COUNT; j++) {
|
for(int j=0; j<STATUSES_COUNT; j++) {
|
||||||
strcat(buffer, "<a href=/");
|
buffer += "<a href='/";
|
||||||
strcat(buffer, statuses[j].name);
|
buffer += statuses[j].name;
|
||||||
strcat(buffer, ">");
|
buffer += "'";
|
||||||
strcat(buffer, statuses[j].name);
|
if (strcmp(statuses[j].name, current_state) == 0) {
|
||||||
strcat(buffer, "</a><br/>");
|
buffer += " class='active'";
|
||||||
|
}
|
||||||
|
buffer += ">";
|
||||||
|
buffer += statuses[j].name;
|
||||||
|
buffer += "</a><br/>";
|
||||||
|
}
|
||||||
|
return buffer;
|
||||||
}
|
}
|
||||||
strcat(buffer, "</body></html>");
|
|
||||||
server.send(200, "text/html", buffer);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void StatusLookup() {
|
void StatusLookup() {
|
||||||
if (Switch_Status(server.uri().substring(1))) {
|
if (Switch_Status(server.uri().substring(1))) {
|
||||||
server.send(204, "text/plain", "OK");
|
server.sendHeader("Location", String("/"), true);
|
||||||
|
server.send ( 302, "text/plain", "");
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
server.send(404, "text/plain", "Missing");
|
server.send(404, "text/plain", "Missing");
|
||||||
@@ -140,12 +149,14 @@ void setup()
|
|||||||
Serial.println("");
|
Serial.println("");
|
||||||
Serial.println("---");
|
Serial.println("---");
|
||||||
|
|
||||||
|
// Define the output pins
|
||||||
pinMode(RED_LED_PIN, OUTPUT);
|
pinMode(RED_LED_PIN, OUTPUT);
|
||||||
pinMode(GREEN_LED_PIN, OUTPUT);
|
pinMode(GREEN_LED_PIN, OUTPUT);
|
||||||
pinMode(BLUE_LED_PIN, OUTPUT);
|
pinMode(BLUE_LED_PIN, OUTPUT);
|
||||||
|
|
||||||
Serial.println("Config - Pins: (" + String(RED_LED_PIN) + "," + String(GREEN_LED_PIN) + "," + String(BLUE_LED_PIN) + "), PWM: " + String(PWM_MAX_VALUE));
|
Serial.println("Config - Pins: (" + String(RED_LED_PIN) + "," + String(GREEN_LED_PIN) + "," + String(BLUE_LED_PIN) + "), PWM: " + String(PWM_MAX_VALUE));
|
||||||
|
|
||||||
|
// Start Wifi
|
||||||
WiFi.begin(WIFI_SSID, WIFI_PASS);
|
WiFi.begin(WIFI_SSID, WIFI_PASS);
|
||||||
Serial.print("Connecting to " + String(WIFI_SSID));
|
Serial.print("Connecting to " + String(WIFI_SSID));
|
||||||
while(WiFi.status()!= WL_CONNECTED)
|
while(WiFi.status()!= WL_CONNECTED)
|
||||||
@@ -157,6 +168,7 @@ void setup()
|
|||||||
Serial.print("IP Address: ");
|
Serial.print("IP Address: ");
|
||||||
Serial.println(WiFi.localIP());
|
Serial.println(WiFi.localIP());
|
||||||
|
|
||||||
|
// Disable inbuilt LED, and go 'offline'
|
||||||
digitalWrite(LED_BUILTIN, LOW);
|
digitalWrite(LED_BUILTIN, LOW);
|
||||||
Switch_Status("offline");
|
Switch_Status("offline");
|
||||||
|
|
||||||
@@ -166,9 +178,19 @@ void setup()
|
|||||||
#endif
|
#endif
|
||||||
|
|
||||||
#ifdef ENABLE_WEBSERVER
|
#ifdef ENABLE_WEBSERVER
|
||||||
server.on("/",IndexPage);
|
if(!LittleFS.begin()){
|
||||||
server.on("/health", [](){server.send(200,"text/plain","OK");});
|
Serial.println("An Error has occurred while mounting LittleFS");
|
||||||
server.on("/state", [](){server.send(200,"text/plain", current_state);});
|
return;
|
||||||
|
}
|
||||||
|
server.on("/", HTTP_GET, [](){
|
||||||
|
ESPTemplateProcessor(server).send(String("/index.html"), ProcessIndexPage);
|
||||||
|
});
|
||||||
|
server.on("/health", HTTP_GET, [](){
|
||||||
|
server.send(200,"text/plain","OK");
|
||||||
|
});
|
||||||
|
server.on("/state", HTTP_GET, [](){
|
||||||
|
server.send(200,"text/plain", current_state);
|
||||||
|
});
|
||||||
server.onNotFound(StatusLookup);
|
server.onNotFound(StatusLookup);
|
||||||
server.begin();
|
server.begin();
|
||||||
#endif
|
#endif
|
||||||
|
|||||||
Reference in New Issue
Block a user