From b6d1df48244db14d693c286445ca1384341ae0c1 Mon Sep 17 00:00:00 2001 From: jdlugosz963 Date: Thu, 22 Dec 2022 19:09:34 +0100 Subject: Add webserver and simple webpage --- data/index.html | 31 +++++++++++++++++++++++ data/script.js | 15 +++++++++++ data/style.css | 40 +++++++++++++++++++++++++++++ webserver.cpp | 78 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++ webserver.h | 26 +++++++++++++++++++ 5 files changed, 190 insertions(+) create mode 100644 data/index.html create mode 100644 data/script.js create mode 100644 data/style.css create mode 100644 webserver.cpp create mode 100644 webserver.h diff --git a/data/index.html b/data/index.html new file mode 100644 index 0000000..f3b7068 --- /dev/null +++ b/data/index.html @@ -0,0 +1,31 @@ + + + + + + + Schack v.0.0.1 + + + + +
+

Schack

+ +
+
+ + + + + +
+
+ + +
+ + + diff --git a/data/script.js b/data/script.js new file mode 100644 index 0000000..0f3101f --- /dev/null +++ b/data/script.js @@ -0,0 +1,15 @@ +document.getElementById("button-previous-s").onclick = function() { + fetch("/previouss"); +} + +document.getElementById("button-previous").onclick = function() { + fetch("/previous"); +} + +document.getElementById("button-next").onclick = function() { + fetch("/next"); +} + +document.getElementById("button-next-s").onclick = function() { + fetch("/nexts"); +} diff --git a/data/style.css b/data/style.css new file mode 100644 index 0000000..52dfca6 --- /dev/null +++ b/data/style.css @@ -0,0 +1,40 @@ +:root { + --black: #333333; + --white: #eeeeee; +} + +body { + background-color: var(--black); + color: var(--white); + + font-family: monospace; + letter-spacing: 0.2em; +} + +main { + padding: 2em; +} + +a { + color: var(--white); +} + +h1 { + font-size: 42px; + + margin: 0; +} + +#container { + padding-top: 2em; + padding-bottom: 2em; +} + +input { + padding: 0.5em; + + background-color: var(--white); + color: var(--black); + + border: none; +} diff --git a/webserver.cpp b/webserver.cpp new file mode 100644 index 0000000..36b5a14 --- /dev/null +++ b/webserver.cpp @@ -0,0 +1,78 @@ +#include "webserver.h" + + +WebServer::WebServer(Controller *controller) { + this->controller = controller; + this->server = new ESP8266WebServer(80); + + if(!setupST()) { + Serial.println("Cos sie zjebalo!!"); + return; + } + + if(!LittleFS.begin()){ + Serial.println("An Error has occurred while mounting LittleFS"); + return; + } + setupRoutes(); +} + +WebServer::~WebServer() { + delete server; +} + +void WebServer::setupRoutes() { + server->serveStatic("/", LittleFS, "/index.html", "max-age=43200"); + server->serveStatic("/script.js", LittleFS, "/script.js", "max-age=43200"); + server->serveStatic("/style.css", LittleFS, "/style.css", "max-age=43200"); + + server->on("/next", [this]() { + this->server->send(200, "text/plain", "Next ok!"); + this->controller->next(); + }); + + server->on("/previous", [this]() { + this->server->send(200, "text/plain", "Previous ok!"); + this->controller->previous(); + }); + + server->on("/nexts", [this]() { + this->server->send(200, "text/plain", "Next_S ok!"); + this->controller->nextS(); + }); + + server->on("/previouss", [this]() { + this->server->send(200, "text/plain", "Previous_S ok!"); + this->controller->previousS(); + }); + + server->begin(); +} + +void WebServer::loop() { + server->handleClient(); +} + +bool WebServer::setupAP () { + WiFi.softAP(controller->getAPSSID(), controller->getAPPassword()); + wifiStatus = WiFi.status(); + return wifiStatus; +} + +bool WebServer::setupST () { + Serial.println("Station connecting: "); + + WiFi.begin("OpenWrt2"); + while (WiFi.status() != WL_CONNECTED) + { + delay(500); + Serial.print("."); + } + Serial.println(); + + Serial.print("Connected, IP address: "); + Serial.println(WiFi.localIP()); + + wifiStatus = WiFi.status(); + return wifiStatus; +} diff --git a/webserver.h b/webserver.h new file mode 100644 index 0000000..940e5a5 --- /dev/null +++ b/webserver.h @@ -0,0 +1,26 @@ +//#include +#include +#include + +#include "controller.h" +#include + + +#ifndef WEBSERVER_H +#define WEBSERVER_H + +class WebServer { + ESP8266WebServer *server; + Controller *controller; + + bool setupST(); + bool setupAP(); + void setupRoutes(); + bool wifiStatus = false; +public: + WebServer(Controller *controller); + ~WebServer(); + void loop(); +}; + +#endif -- cgit v1.2.3