diff --git a/.github/copilot-instructions.md b/.github/copilot-instructions.md new file mode 100644 index 0000000..ac9ef9b --- /dev/null +++ b/.github/copilot-instructions.md @@ -0,0 +1,50 @@ +--- +# Workspace instructions for the KeyPatch ESP8266 project + +This project is a PlatformIO/Arduino sketch for an ESP8266 (Wemos D1 mini) that +implements a captive-portal Wi‑Fi configurator, filesystem manager, and NeoPixel +handler. The code is organised into multiple `.ino` tabs under `src/` and uses +several libraries (LittleFS, ESP8266WebServer, DNSServer, Adafruit NeoPixel, PCF8575, +etc.). + +## Building and flashing + +- Build with PlatformIO via the task or from the command line: + + ```powershell + & "C:\Users\User\.platformio\penv\Scripts\platformio.exe" run + ``` + + or `pio run`/`platformio run` from a shell that has the `platformio` command. +- Upload to the board using `platformio run -t upload` or through the built-in VS Code task. +- If you see `exit code 1` from the upload task, the problem is generally a connection issue to the + device, not a compilation error; the project compiles cleanly with the current sources. + +## Code conventions + +- All Arduino headers are included in the main tab (`KeyPatch.ino`). Helper tabs + such as `Connect.ino`, `LittleFS.ino`, etc. depend on those includes being present. +- `setup()` must call `connectWifi()` and `setupFS()` as indicated in the comments. +- Serial debugging is used heavily; the baud rate is 115200 by default. +- When the soft‑AP named `EspConfig` is started, the IP address is printed to the + serial monitor (e.g. `AP-IP-Adresse: 172.217.28.1`). + +## Common tasks + +1. Change Wi‑Fi parameters in `Connect.ino` or use the captive portal. +2. Upload `fs.html` via the web interface to manage LittleFS content. +3. Edit hardware configurations under `hardware/` (FreeCAD files). + +## Troubleshooting + +- Compilation errors are rare; if you encounter them, run the verbose build + (`platformio run -v`) and inspect `compile.log` for `error:` messages. +- LittleFS operations require `LittleFS.begin()` to succeed; the helper tab + `LittleFS.ino` includes debug prints. + +## Personalisation + +This file is intended to help any contributor or future self understand the +project layout, build commands, and where to look for the various features. +Feel free to update it with new instructions as the sketch evolves. +--- diff --git a/compile.log b/compile.log new file mode 100644 index 0000000..aed9db5 Binary files /dev/null and b/compile.log differ diff --git a/platformio.ini b/platformio.ini index d264d6e..6faa5a6 100644 --- a/platformio.ini +++ b/platformio.ini @@ -2,6 +2,7 @@ platform = espressif8266 board = d1_mini framework = arduino +monitor_speed = 115200 lib_deps = adafruit/Adafruit NeoPixel @ ^1.11.0 https://github.com/RobTillaart/PCF8575 \ No newline at end of file diff --git a/src/Admin.ino b/src/Admin.ino new file mode 100644 index 0000000..43b80be --- /dev/null +++ b/src/Admin.ino @@ -0,0 +1,101 @@ + +// **************************************************************** +// Sketch Esp8266 Admin Modular(Tab) +// created: Jens Fleischer, 2019-12-17 +// last mod: Jens Fleischer, 2021-06-09 +// For more information visit: https://fipsok.de +// **************************************************************** +// Hardware: Esp8266 +// Software: Esp8266 Arduino Core 2.6.1 - 3.1.0 +// Geprüft: von 1MB bis 16MB Flash +// Getestet auf: Nodemcu, Wemos D1 Mini Pro, Sonoff Switch, Sonoff Dual +/****************************************************************** + Copyright (c) 2019 Jens Fleischer. All rights reserved. + + This file is free software; you can redistribute it and/or + modify it under the terms of the GNU Lesser General Public + License as published by the Free Software Foundation; either + version 2.1 of the License, or (at your option) any later version. + This file is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + Lesser General Public License for more details. +*******************************************************************/ +// Diese Version von Admin sollte als Tab eingebunden werden. +// #include /#include #include müssen im Haupttab aufgerufen werden +// Die Funktionalität des ESP8266 Webservers ist erforderlich. +// Die Spiffs.ino muss im ESP8266 Webserver enthalten sein +// Funktion "admin();" muss im setup() nach setupFS()/spiffs() und dem Verbindungsaufbau aufgerufen werden. +// Die Funktion "runtime();" muss mindestens zweimal innerhalb 49 Tage aufgerufen werden. +// Entweder durch den Client(Webseite) oder zur Sicherheit im "loop();" +/**************************************************************************************/ + +//#define LittleFS SPIFFS // Einkommentieren wenn SPIFFS als Filesystem genutzt wird + +const char* const PROGMEM flashChipMode[] = {"QIO", "QOUT", "DIO", "DOUT", "Unbekannt"}; + +void admin() { // Funktionsaufruf "admin();" muss im Setup eingebunden werden + File file = LittleFS.open("/config.json", "r"); + if (file) { + String newhostname = file.readStringUntil('\n'); + if (newhostname != "") { + WiFi.hostname(newhostname.substring(1, newhostname.length() - 1)); + file.close(); + ArduinoOTA.setHostname(WiFi.hostname().c_str()); + } + } + server.on("/admin/renew", handlerenew); + server.on("/admin/once", handleonce); + server.on("/reconnect", []() { + server.send(304, "message/http"); + WiFi.reconnect(); + }); + server.on("/restart", []() { + server.send(304, "message/http"); + //save(); //Wenn Werte vor dem Neustart gespeichert werden sollen + ESP.restart(); + }); +} + +//Es kann entweder die Spannung am ADC-Pin oder die Modulversorgungsspannung (VCC) ausgegeben werden. + +void handlerenew() { // Um die am ADC-Pin anliegende externe Spannung zu lesen, verwende analogRead (A0) + server.send(200, "application/json", "[\"" + runtime() + "\",\"" + WiFi.RSSI() + "\",\"" + analogRead(A0) + "\"]"); // Json als Array +} +/* +ADC_MODE(ADC_VCC); +void handlerenew() { // Zum Lesen der Modulversorgungsspannung (VCC), verwende ESP.getVcc() + server.send(200, "application/json", "[\"" + runtime() + "\",\"" + WiFi.RSSI() + "\",\"" + ESP.getVcc() / 1024.0 + " V" + "\"]"); +} +*/ +void handleonce() { + if (server.arg(0) != "") { + WiFi.hostname(server.arg(0)); + File f = LittleFS.open("/config.json", "w"); // Datei zum schreiben öffnen + f.printf("\"%s\"\n", WiFi.hostname().c_str()); + f.close(); + } + String temp = "{\"File\":\"" + sketchName() + "\", \"Build\":\"" + __DATE__ + " " + __TIME__ + "\", \"SketchSize\":\"" + formatBytes(ESP.getSketchSize()) + + "\", \"SketchSpace\":\"" + formatBytes(ESP.getFreeSketchSpace()) + "\", \"LocalIP\":\"" + WiFi.localIP().toString() + + "\", \"Hostname\":\"" + WiFi.hostname() + "\", \"SSID\":\"" + WiFi.SSID() + "\", \"GatewayIP\":\"" + WiFi.gatewayIP().toString() + + "\", \"Channel\":\"" + WiFi.channel() + "\", \"MacAddress\":\"" + WiFi.macAddress() + "\", \"SubnetMask\":\"" + WiFi.subnetMask().toString() + + "\", \"BSSID\":\"" + WiFi.BSSIDstr() + "\", \"ClientIP\":\"" + server.client().remoteIP().toString() + "\", \"DnsIP\":\"" + WiFi.dnsIP().toString() + + "\", \"ResetReason\":\"" + ESP.getResetReason() + "\", \"CpuFreqMHz\":\"" + F_CPU / 1000000 + "\", \"FreeHeap\":\"" + formatBytes(ESP.getFreeHeap()) + + "\", \"HeapFrag\":\"" + ESP.getHeapFragmentation() + "\", \"ChipSize\":\"" + formatBytes(ESP.getFlashChipSize()) + + "\", \"ChipSpeed\":\"" + ESP.getFlashChipSpeed() / 1000000 + "\", \"ChipMode\":\"" + flashChipMode[ESP.getFlashChipMode()] + + "\", \"IdeVersion\":\"" + ARDUINO + "\", \"CoreVersion\":\"" + ESP.getCoreVersion() + "\", \"SdkVersion\":\"" + ESP.getSdkVersion() + "\"}"; + server.send(200, "application/json", temp); // Json als Objekt +} + +String runtime() { + static uint8_t rolloverCounter; + static uint32_t previousMillis; + uint32_t currentMillis {millis()}; + if (currentMillis < previousMillis) rolloverCounter++; // prüft millis() auf Überlauf + previousMillis = currentMillis; + uint32_t sec {(0xFFFFFFFF / 1000) * rolloverCounter + (currentMillis / 1000)}; + char buf[20]; + snprintf(buf, sizeof(buf), "%*.d %.*s %02d:%02d:%02d", + sec < 86400 ? 0 : 1, sec / 86400, sec < 86400 ? 0 : sec >= 172800 ? 4 : 3, "Tage", sec / 3600 % 24, sec / 60 % 60, sec % 60); + return buf; +} diff --git a/src/Config.ino b/src/Config.ino index 690cfe5..6f6541b 100644 --- a/src/Config.ino +++ b/src/Config.ino @@ -5,4 +5,5 @@ #define NEOPIXEL_PIN D6 #define BRIGHTNESS 100 #define BLINK_INTERVAL 500 // Blink interval in milliseconds (0.5 seconds) +#define SERIAL_SPEED 115200 // Serial Baudrate for ESP8266 diff --git a/src/Connect.ino b/src/Connect.ino new file mode 100644 index 0000000..f95a38a --- /dev/null +++ b/src/Connect.ino @@ -0,0 +1,185 @@ + +// **************************************************************** +// Sketch Esp8266 Login Manager mit Captive Portal und optischer Anzeige +// created: Jens Fleischer, 2021-01-05 +// last mod: Jens Fleischer, 2021-11-29 +// For more information visit: https://fipsok.de +// **************************************************************** +// Hardware: Esp8266 +// Software: Esp8266 Arduino Core 2.6.3 / 2.7.4 / 3.0.2 +// Getestet auf: Nodemcu, Wemos D1 Mini Pro +/****************************************************************** + Copyright (c) 2021 Jens Fleischer. All rights reserved. + + This file is free software; you can redistribute it and/or + modify it under the terms of the GNU Lesser General Public + License as published by the Free Software Foundation; either + version 2.1 of the License, or (at your option) any later version. + This file is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + Lesser General Public License for more details. +*******************************************************************/ +// Diese Version von Login Manager sollte als Tab eingebunden werden. +// #include #include müssen im Haupttab aufgerufen werden +// Die Funktionalität des ESP8266 Webservers und des LittleFS Tab ist erforderlich. +// Die Funktion "connectWifi();" muss im Setup eingebunden werden. +// Die Oneboard LED blinkt beim Verbindungsaufbau zum Netzwerk und leuchtet im AP Modus dauerhaft. +// Die Zugangsdaten werden nicht menschenlesbar im Dateisystem gespeichert. +/**************************************************************************************/ + +/** + Folgendes muss im Webserver Tab vor dem "setup()" eingefügt werden. + + #include + const byte DNS_PORT = 53; + DNSServer dnsServer; + + Der DNS Server muss im loop aufgerufen werden. + + void loop() { + dnsServer.processNextRequest(); + reStation(); + } +*/ + +//#define CONFIG // Einkommentieren wenn der ESP dem Router die IP mitteilen soll. + +#ifdef CONFIG +IPAddress staticIP(192, 168, 178, 99); // statische IP des NodeMCU ESP8266 +IPAddress gateway(192, 168, 178, 1); // IP-Adresse des Router +IPAddress subnet(255, 255, 255, 0); // Subnetzmaske des Netzwerkes +IPAddress dns(192, 168, 178, 1); // DNS Server +#endif + +const char HTML[] PROGMEM = R"( + + + + + + Login Manager + + +

Zugangsdaten

+
+

+ +

+

+ +

+
+ + + +)"; +const char JSON[] PROGMEM = R"("

Die Zugangsdaten wurden übertragen. Eine Verbindung zum Netzwerk wird hergestellt.

")"; + +char ssid[33] {" "}; +char password[65]; +constexpr char key {129}; + +void connectWifi() { + IPAddress apIP(172, 217, 28, 1); + IPAddress netMsk(255, 255, 255, 0); + File file = LittleFS.open("/wifi.dat", "r"); + if (file) { + file.read(reinterpret_cast(&ssid), sizeof(ssid)); + file.read(reinterpret_cast(&password), sizeof(password)); + file.close(); + for (auto &c : ssid) c ^= key; // Dechiffrierung SSID + for (auto &c : password) c ^= key; // Dechiffrierung Passwort + } + WiFi.disconnect(); + WiFi.persistent(false); // Auskommentieren wenn Netzwerkname und Passwort in den Flash geschrieben werden sollen. + WiFi.mode(WIFI_STA); // Station-Modus + WiFi.begin(ssid, password); +#ifdef CONFIG + WiFi.config(staticIP, gateway, subnet, dns); +#endif + uint8_t i {0}; + while (WiFi.status() != WL_CONNECTED) { + pinMode(LED_BUILTIN, OUTPUT); // OnBoardLed Nodemcu, Wemos D1 Mini Pro + digitalWrite(LED_BUILTIN, 0); // Led blinkt während des Verbindungsaufbaus + delay(500); + digitalWrite(LED_BUILTIN, 1); + delay(500); + Serial.printf(" %i sek\n", ++i); + if (WiFi.status() == WL_NO_SSID_AVAIL || i > 29) { // Ist die SSID nicht erreichbar, wird ein eigenes Netzwerk erstellt. + digitalWrite(LED_BUILTIN, 0); // Dauerleuchten der Led zeigt den AP Modus an. + WiFi.disconnect(); + WiFi.mode(WIFI_AP); // Soft-Access-Point-Modus + Serial.println(PSTR("\nVerbindung zum Router fehlgeschlagen !\nStarte Soft AP")); + WiFi.softAPConfig(apIP, apIP, netMsk); + if (WiFi.softAP("EspConfig")) { + Serial.println(PSTR("Verbinde dich mit dem Netzwerk \"EspConfig\".\n")); + } + break; + } + } + if (WiFi.status() == WL_CONNECTED) { + Serial.printf(PSTR("\nVerbunden mit: %s\nEsp8266 IP: %s\n"), WiFi.SSID().c_str(), WiFi.localIP().toString().c_str()); + } + dnsServer.start(DNS_PORT, "*", apIP); + server.on("/wifisave", HTTP_POST, handleWifiSave); + server.onNotFound(handleRoot); +} + +void handleWifiSave() { + if (server.hasArg("ssid") && server.hasArg("passwort")) { + strcpy(ssid, server.arg(0).c_str()); + strcpy(password, server.arg(1).c_str()); + for (auto &c : ssid) c ^= key; // Chiffrierung SSID + for (auto &c : password) c ^= key; // Chiffrierung Passwort + File file = LittleFS.open("/wifi.dat", "w"); + file.write(reinterpret_cast(&ssid), sizeof(ssid)); + file.write(reinterpret_cast(&password), sizeof(password)); + file.close(); + server.send(200, "application/json", JSON); + delay(500); + connectWifi(); + } +} + +void handleRoot() { + if (WiFi.status() != WL_CONNECTED) { // Besteht keine Verbindung zur Station wird das Formular gesendet. + server.sendHeader("Cache-Control", "no-cache, no-store, must-revalidate"); + server.sendHeader("Pragma", "no-cache"); + server.sendHeader("Expires", "-1"); + server.send(200, "text/html", HTML); + } + else { + if (!handleFile(server.urlDecode(server.uri()))) { + if (server.urlDecode(server.uri()).endsWith("/")) sendResponce(); + server.send(404, "text/plain", "FileNotFound"); + } + } +} + +void reStation() { // Der Funktionsaufruf "reStation();" sollte im "loop" stehen. + static unsigned long previousMillis; // Nach Stromausfall startet der Esp.. schneller als der Router. + constexpr unsigned long INTERVAL (3e5); // Im AP Modus aller 5 Minuten prüfen ob der Router verfügbar ist. + if (millis() - previousMillis >= INTERVAL) { + previousMillis += INTERVAL; + if (WiFi.status() != WL_CONNECTED) connectWifi(); + } +} diff --git a/src/KeyPatch.ino b/src/KeyPatch.ino index fe61a28..8c2be68 100644 --- a/src/KeyPatch.ino +++ b/src/KeyPatch.ino @@ -1,9 +1,24 @@ #include #include #include +#include +#include +#include #include "Config.ino" +#include -#define AMOUNTOFPORTS 8 // currently max 16 ports are supported +// Globale Deklarationen für alle Tabs +ESP8266WebServer server(80); +const byte DNS_PORT = 53; +DNSServer dnsServer; + +String sketchName() { // Dateiname für den Admin Tab ab EspCoreVersion 2.6.0 + char file[sizeof(__FILE__)] = __FILE__; + char * pos = strrchr(file, '.'); *pos = '\0'; + return file; +} + +#define AMOUNTOFPORTS 16 // currently max 16 ports are supported #define EXPANDER1ADDRESS 0x21 // address of expander 1 PCF8575 expander1(EXPANDER1ADDRESS); @@ -14,8 +29,12 @@ byte number=0; extern Adafruit_NeoPixel* pixels; void setup() { - Serial.begin(9600); - Serial.println("Starte 16-Port IO Erweiterung..."); + Serial.begin(SERIAL_SPEED); + delay(100); + Serial.printf("\nSketchname: %s\nBuild: %s\t\tIDE: %d.%d.%d\n%s\n\n", + (__FILE__), (__TIMESTAMP__), ARDUINO / 10000, ARDUINO % 10000 / 100, ARDUINO % 100 / 10 ? ARDUINO % 100 : ARDUINO % 10, ESP.getFullVersion().c_str()); + + Serial.printf("\nKeyPatch ESP8266 WebServer\n"); //Set UP 12C Communication Wire.begin(); for (byte adress=8; adress<120; adress++) @@ -57,10 +76,29 @@ void setup() { }; pixels->show(); +// SetUp WebServer + setupFS(); // setupFS(); oder spiffs(); je nach Dateisystem + connectWifi(); + admin(); + ArduinoOTA.onStart([]() { + //save(); // Wenn Werte vor dem Neustart gespeichert werden sollen + }); + ArduinoOTA.begin(); + server.begin(); + + + Serial.println("Setup End..."); } void loop() { + ArduinoOTA.handle(); + server.handleClient(); + if (millis() < 0x2FFF || millis() > 0xFFFFF0FF) { // Die Funktion "runtime()" wird nur für den Admin Tab gebraucht. + runtime(); // Auskommentieren falls du den Admin Tab nicht nutzen möchtest. + } + dnsServer.processNextRequest(); + reStation(); // Erst 16 Ports aus PCF8575 einlesen for (uint8_t i = 0; i < AMOUNTOFPORTS; i++) { portStates[i] = expander1.read(i); diff --git a/src/LittleFS.ino b/src/LittleFS.ino new file mode 100644 index 0000000..e106855 --- /dev/null +++ b/src/LittleFS.ino @@ -0,0 +1,173 @@ + +// **************************************************************** +// Sketch Esp8266 Filesystem Manager spezifisch sortiert Modular(Tab) +// created: Jens Fleischer, 2020-06-08 +// last mod: Jens Fleischer, 2020-09-02 +// For more information visit: https://fipsok.de +// **************************************************************** +// Hardware: Esp8266 +// Software: Esp8266 Arduino Core 2.6.0 - 2.7.4 +// Getestet auf: Nodemcu +/****************************************************************** + Copyright (c) 2020 Jens Fleischer. All rights reserved. + + This file is free software; you can redistribute it and/or + modify it under the terms of the GNU Lesser General Public + License as published by the Free Software Foundation; either + version 2.1 of the License, or (at your option) any later version. + This file is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + Lesser General Public License for more details. +*******************************************************************/ +// Diese Version von LittleFS sollte als Tab eingebunden werden. +// #include #include müssen im Haupttab aufgerufen werden +// Die Funktionalität des ESP8266 Webservers ist erforderlich. +// "server.onNotFound()" darf nicht im Setup des ESP8266 Webserver stehen. +// Die Funktion "setupFS();" muss im Setup aufgerufen werden. +/**************************************************************************************/ + +#include +#include + +const char WARNING[] PROGMEM = R"(

Der Sketch wurde mit "FS:none" kompilliert!)"; +const char HELPER[] PROGMEM = R"(
+
Lade die fs.html hoch.)"; + +void setupFS() { // Funktionsaufruf "setupFS();" muss im Setup eingebunden werden + LittleFS.begin(); + server.on("/format", formatFS); + server.on("/upload", HTTP_POST, sendResponce, handleUpload); + server.onNotFound([]() { + if (!handleFile(server.urlDecode(server.uri()))) + server.send(404, "text/plain", "FileNotFound"); + }); +} + +bool handleList() { // Senden aller Daten an den Client + FSInfo fs_info; LittleFS.info(fs_info); // Füllt FSInfo Struktur mit Informationen über das Dateisystem + Dir dir = LittleFS.openDir("/"); + using namespace std; + typedef tuple records; + list dirList; + while (dir.next()) { // Ordner und Dateien zur Liste hinzufügen + if (dir.isDirectory()) { + uint8_t ran {0}; + Dir fold = LittleFS.openDir(dir.fileName()); + while (fold.next()) { + ran++; + dirList.emplace_back(dir.fileName(), fold.fileName(), fold.fileSize()); + } + if (!ran) dirList.emplace_back(dir.fileName(), "", 0); + } + else { + dirList.emplace_back("", dir.fileName(), dir.fileSize()); + } + } + dirList.sort([](const records & f, const records & l) { // Dateien sortieren + if (server.arg(0) == "1") { + return get<2>(f) > get<2>(l); + } else { + for (uint8_t i = 0; i < 31; i++) { + if (tolower(get<1>(f)[i]) < tolower(get<1>(l)[i])) return true; + else if (tolower(get<1>(f)[i]) > tolower(get<1>(l)[i])) return false; + } + return false; + } + }); + dirList.sort([](const records & f, const records & l) { // Ordner sortieren + if (get<0>(f)[0] != 0x00 || get<0>(l)[0] != 0x00) { + for (uint8_t i = 0; i < 31; i++) { + if (tolower(get<0>(f)[i]) < tolower(get<0>(l)[i])) return true; + else if (tolower(get<0>(f)[i]) > tolower(get<0>(l)[i])) return false; + } + } + return false; + }); + String temp = "["; + for (auto& t : dirList) { + if (temp != "[") temp += ','; + temp += "{\"folder\":\"" + get<0>(t) + "\",\"name\":\"" + get<1>(t) + "\",\"size\":\"" + formatBytes(get<2>(t)) + "\"}"; + } + temp += ",{\"usedBytes\":\"" + formatBytes(fs_info.usedBytes) + // Berechnet den verwendeten Speicherplatz + "\",\"totalBytes\":\"" + formatBytes(fs_info.totalBytes) + // Zeigt die Größe des Speichers + "\",\"freeBytes\":\"" + (fs_info.totalBytes - fs_info.usedBytes) + "\"}]"; // Berechnet den freien Speicherplatz + server.send(200, "application/json", temp); + return true; +} + +void deleteRecursive(const String &path) { + if (LittleFS.remove(path)) { + LittleFS.open(path.substring(0, path.lastIndexOf('/')) + "/", "w"); + return; + } + Dir dir = LittleFS.openDir(path); + while (dir.next()) { + deleteRecursive(path + '/' + dir.fileName()); + } + LittleFS.rmdir(path); +} + +bool handleFile(String &&path) { + if (server.hasArg("new")) { + String folderName {server.arg("new")}; + for (auto& c : {34, 37, 38, 47, 58, 59, 92}) for (auto& e : folderName) if (e == c) e = 95; // Ersetzen der nicht erlaubten Zeichen + LittleFS.mkdir(folderName); + } + if (server.hasArg("sort")) return handleList(); + if (server.hasArg("delete")) { + deleteRecursive(server.arg("delete")); + sendResponce(); + return true; + } + if (!LittleFS.exists("fs.html")) server.send(200, "text/html", LittleFS.begin() ? HELPER : WARNING); // ermöglicht das hochladen der fs.html + if (path.endsWith("/")) path += "index.html"; + if (path == "/spiffs.html") sendResponce(); // Vorrübergehend für den Admin Tab + return LittleFS.exists(path) ? ({File f = LittleFS.open(path, "r"); server.streamFile(f, getContentType(path)); f.close(); true;}) : false; +} + +void handleUpload() { // Dateien ins Filesystem schreiben + static File fsUploadFile; + HTTPUpload& upload = server.upload(); + if (upload.status == UPLOAD_FILE_START) { + if (upload.filename.length() > 31) { // Dateinamen kürzen + upload.filename = upload.filename.substring(upload.filename.length() - 31, upload.filename.length()); + } + printf(PSTR("handleFileUpload Name: /%s\n"), upload.filename.c_str()); + fsUploadFile = LittleFS.open(server.arg(0) + "/" + server.urlDecode(upload.filename), "w"); + } else if (upload.status == UPLOAD_FILE_WRITE) { + printf(PSTR("handleFileUpload Data: %u\n"), upload.currentSize); + fsUploadFile.write(upload.buf, upload.currentSize); + } else if (upload.status == UPLOAD_FILE_END) { + printf(PSTR("handleFileUpload Size: %u\n"), upload.totalSize); + fsUploadFile.close(); + } +} + +void formatFS() { // Formatiert das Filesystem + LittleFS.format(); + sendResponce(); +} + +void sendResponce() { + server.sendHeader("Location", "fs.html"); + server.send(303, "message/http"); +} + +const String formatBytes(size_t const& bytes) { // lesbare Anzeige der Speichergrößen + return bytes < 1024 ? static_cast(bytes) + " Byte" : bytes < 1048576 ? static_cast(bytes / 1024.0) + " KB" : static_cast(bytes / 1048576.0) + " MB"; +} + +const String getContentType(const String & path) { // ermittelt den MIME-Type + using namespace mime; + char buff[sizeof(mimeTable[0].mimeType)]; + for (size_t i = 0; i < maxType - 1; i++) { + strcpy_P(buff, mimeTable[i].endsWith); + if (path.endsWith(buff)) { + strcpy_P(buff, mimeTable[i].mimeType); + return static_cast(buff); + } + } + strcpy_P(buff, mimeTable[maxType - 1].mimeType); + return static_cast(buff); +} diff --git a/src/Webserver.ino b/src/Webserver.ino new file mode 100644 index 0000000..8526588 --- /dev/null +++ b/src/Webserver.ino @@ -0,0 +1,54 @@ +// **************************************************************** +// Sketch Esp8266 Webserver Modular(Tab) +// created: Jens Fleischer, 2018-05-16 +// last mod: Jens Fleischer, 2020-12-28 +// For more information visit: https://fipsok.de +// **************************************************************** +// Hardware: Esp8266 +// Software: Esp8266 Arduino Core 2.4.2 - 3.1.2 +// Getestet auf: Nodemcu, Wemos D1 Mini Pro, Sonoff Switch, Sonoff Dual +/****************************************************************** + Copyright (c) 2018 Jens Fleischer. All rights reserved. + + This file is free software; you can redistribute it and/or + modify it under the terms of the GNU Lesser General Public + License as published by the Free Software Foundation; either + version 2.1 of the License, or (at your option) any later version. + This file is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + Lesser General Public License for more details. +*******************************************************************/ +// Der WebServer Tab ist der Haupt Tab mit "setup" und "loop". +// #include bzw. #include und #include +// müssen im Haupttab aufgerufen werden. +// Ein Connect Tab ist erforderlich. +// Inklusive Arduino OTA-Updates (Erfordert freien Flash-Speicher) +/**************************************************************************************/ + +#include +#include // https://arduino-esp8266.readthedocs.io/en/latest/ota_updates/readme.html +#include // Library für Dateisystem LittleFS +#include +//#include // Library für Dateisystem Spiffs einkommentieren wenn erforderlich + +// ESP8266WebServer server(80); // Jetzt in KeyPatch.ino deklariert +// DNSServer dnsServer; // Jetzt in KeyPatch.ino deklariert +// const byte DNS_PORT = 53; // Jetzt in KeyPatch.ino deklariert + + +void setupWebserver() { + setupFS(); // Filesystem setup + + ArduinoOTA.onStart([]() { + // Hier können Werte vor dem Neustart gespeichert werden + }); + ArduinoOTA.begin(); +} + +void handleWebserver() { + ArduinoOTA.handle(); + server.handleClient(); + dnsServer.processNextRequest(); + reStation(); +} diff --git a/src/html/admin.html b/src/html/admin.html new file mode 100644 index 0000000..e8eb51f --- /dev/null +++ b/src/html/admin.html @@ -0,0 +1,172 @@ + + + + + + + + ESP8266 Admin + + + +

ESP8266 Admin Page

+
+ + +
+
+ + +
+
+
+
+ + +
+
+
+ + +
+ + diff --git a/src/html/fs.html b/src/html/fs.html new file mode 100644 index 0000000..6aa6629 --- /dev/null +++ b/src/html/fs.html @@ -0,0 +1,77 @@ + + + + + + + + Filesystem Manager + + + +

ESP8266 Filesystem Manager

+
+ + +
+
+ + +
+
+
+ +
+ + diff --git a/src/style.css b/src/style.css new file mode 100644 index 0000000..45838e8 --- /dev/null +++ b/src/style.css @@ -0,0 +1,120 @@ + +/* For more information visit:https://fipsok.de */ +body { + font-family: sans-serif; + background-color: #87cefa; + display: flex; + flex-flow: column; + align-items: center; +} +h1,h2 { + color: #e1e1e1; + text-shadow: 2px 2px 2px black; +} +li { + background-color: #feb1e2; + list-style-type: none; + margin-bottom: 10px; + padding: 2px 5px 1px 0; + box-shadow: 5px 5px 5px rgba(0,0,0,0.7); +} +li a:first-child, li b { + background-color: #8f05a5; + font-weight: bold; + color: white; + text-decoration:none; + padding: 2px 5px; + text-shadow: 2px 2px 1px black; + cursor:pointer; +} +li strong { + color: red; +} +input { + height:35px; + font-size:14px; + padding-left: .3em; +} +label + a { + text-decoration: none; +} +h1 + main { + display: flex; +} +aside { + display: flex; + flex-direction: column; + padding: 0.2em; +} +button { + height:40px; + width:130px; + font-size:16px; + margin-top: 1em; + box-shadow: 5px 5px 5px rgba(0,0,0,0.7); +} +div button { + background-color: #7bff97; +} +nav { + display: flex; + align-items: baseline; + justify-content: space-between; +} +#left { + align-items:flex-end; + text-shadow: 0.5px 0.5px 1px #757474; +} +#cr { + font-weight: bold; + cursor:pointer; + font-size: 1.5em; +} +#up { + width: auto; +} +.note { + background-color: #fecdee; + padding: 0.5em; + margin-top: 1em; + text-align: center; + max-width: 320px; + border-radius: 0.5em; +} +.no { + display: none; +} +form [title] { + background-color: skyblue; + font-size: 1em; + width: 120px; +} +form:nth-of-type(2) { + margin-bottom: 1em; +} +[value*=Format] { + margin-top: 1em; + box-shadow: 5px 5px 5px rgba(0,0,0,0.7); +} +[name="group"] { + display: none; +} +[name="group"] + label { + font-size: 1.5em; + margin-right: 5px; +} +[name="group"] + label::before { + content: "\002610"; +} +[name="group"]:checked + label::before { + content: '\002611\0027A5'; +} +@media only screen and (max-width: 500px) { + .ip { + right: 6em; + position: relative; + } + aside { + max-width: 50vw; + } +}