From 7780ddf15cfa21d4706938a5982ee0e1f5632d6c Mon Sep 17 00:00:00 2001 From: KubMakCZ Date: Wed, 12 Mar 2025 13:35:20 +0100 Subject: [PATCH] add littleFS web project --- 10_littlefs_web2led/.gitignore | 5 + 10_littlefs_web2led/.vscode/extensions.json | 10 ++ 10_littlefs_web2led/data/index.html | 22 +++++ 10_littlefs_web2led/platformio.ini | 16 +++ 10_littlefs_web2led/src/main.cpp | 102 ++++++++++++++++++++ 10_littlefs_web2led/test/README | 11 +++ 6 files changed, 166 insertions(+) create mode 100644 10_littlefs_web2led/.gitignore create mode 100644 10_littlefs_web2led/.vscode/extensions.json create mode 100644 10_littlefs_web2led/data/index.html create mode 100644 10_littlefs_web2led/platformio.ini create mode 100644 10_littlefs_web2led/src/main.cpp create mode 100644 10_littlefs_web2led/test/README diff --git a/10_littlefs_web2led/.gitignore b/10_littlefs_web2led/.gitignore new file mode 100644 index 0000000..89cc49c --- /dev/null +++ b/10_littlefs_web2led/.gitignore @@ -0,0 +1,5 @@ +.pio +.vscode/.browse.c_cpp.db* +.vscode/c_cpp_properties.json +.vscode/launch.json +.vscode/ipch diff --git a/10_littlefs_web2led/.vscode/extensions.json b/10_littlefs_web2led/.vscode/extensions.json new file mode 100644 index 0000000..080e70d --- /dev/null +++ b/10_littlefs_web2led/.vscode/extensions.json @@ -0,0 +1,10 @@ +{ + // See http://go.microsoft.com/fwlink/?LinkId=827846 + // for the documentation about the extensions.json format + "recommendations": [ + "platformio.platformio-ide" + ], + "unwantedRecommendations": [ + "ms-vscode.cpptools-extension-pack" + ] +} diff --git a/10_littlefs_web2led/data/index.html b/10_littlefs_web2led/data/index.html new file mode 100644 index 0000000..35391df --- /dev/null +++ b/10_littlefs_web2led/data/index.html @@ -0,0 +1,22 @@ + + + + + + + Document + + +

Ovládání LED

+ +

LED 1

+ ZAPNOUT + VYPNOUT +
stav: STAVLED1
+ +

LED 2

+ ZAPNOUT + VYPNOUT +
stav: STAVLED2
+ + \ No newline at end of file diff --git a/10_littlefs_web2led/platformio.ini b/10_littlefs_web2led/platformio.ini new file mode 100644 index 0000000..f811498 --- /dev/null +++ b/10_littlefs_web2led/platformio.ini @@ -0,0 +1,16 @@ +; PlatformIO Project Configuration File +; +; Build options: build flags, source filter +; Upload options: custom upload port, speed and extra flags +; Library options: dependencies, extra library storages +; Advanced options: extra scripting +; +; Please visit documentation for the other options and examples +; https://docs.platformio.org/page/projectconf.html + +[env:esp32dev] +platform = espressif32 +board = esp32dev +framework = arduino +monitor_speed = 115200 +board_build.filesystem = littlefs \ No newline at end of file diff --git a/10_littlefs_web2led/src/main.cpp b/10_littlefs_web2led/src/main.cpp new file mode 100644 index 0000000..81266fb --- /dev/null +++ b/10_littlefs_web2led/src/main.cpp @@ -0,0 +1,102 @@ +#include +#include +#include +#include + +const char* ssid = "tksteti"; +const char* password = "ProsimTeNevim"; +WebServer server(80); + +const int ledPin1 = 2; +const int ledPin2 = 4; +bool ledState1 = false; +bool ledState2 = false; + +String loadHtml() { + File file = LittleFS.open("/index.html","r"); + if(!file) { + Serial.println("Chyba při otevírání /index.html"); + return "soubor nenalezen"; + } + String html = file.readString(); + file.close(); + //String(ledState1) převede bool na String) + html.replace("STAVLED1", String(ledState1)); + html.replace("STAVLED2", ledState2 ? "ON" : "OFF"); + return html; +} + +void handleRoot(){ + server.send(200,"text/html",loadHtml()); +} + +void handleLED1() { + if(server.hasArg("state")) { + String state = server.arg("state"); + if(state == "on") { + ledState1 = true; + digitalWrite(ledPin1,HIGH); + } else { + ledState1 = false; + digitalWrite(ledPin1,LOW); + } + server.sendHeader("Location","/"); + server.send(302,"text/plain",""); + } else { + server.send(400,"BAD REQUEST"); + } +} + +void handleLED2() { + if(server.hasArg("state")) { + String state = server.arg("state"); + if(state == "on") { + ledState2 = true; + digitalWrite(ledPin2,HIGH); + } else { + ledState2 = false; + digitalWrite(ledPin2,LOW); + } + server.sendHeader("Location","/"); + server.send(302,"text/plain",""); + } else { + server.send(400,"BAD REQUEST"); + } +} + +void setup() { + Serial.begin(115200); + pinMode(ledPin1,OUTPUT); + pinMode(ledPin2,OUTPUT); + digitalWrite(ledPin1,LOW); + digitalWrite(ledPin2,LOW); + + WiFi.setHostname("ESP-Prijmeni"); + WiFi.begin(ssid,password); + while(WiFi.status() != WL_CONNECTED){ + delay(200); + Serial.print("."); + } + Serial.println("WiFi připojeno"); + Serial.print("IP adresa: "); + Serial.print(WiFi.localIP()); + + if(!LittleFS.begin()){ + Serial.println("Chyba při zavádění (mounting) LittleFS"); + return; + } + + server.on("/",handleRoot); + server.on("/led1",handleLED1); + server.on("/led2",handleLED2); + server.onNotFound([](){ + server.send(404,"text/plain","Stranka nenalezena"); + }); + + server.begin(); + Serial.println("HTTP Server spuštěn"); +} + +void loop() { + server.handleClient(); +} \ No newline at end of file diff --git a/10_littlefs_web2led/test/README b/10_littlefs_web2led/test/README new file mode 100644 index 0000000..9b1e87b --- /dev/null +++ b/10_littlefs_web2led/test/README @@ -0,0 +1,11 @@ + +This directory is intended for PlatformIO Test Runner and project tests. + +Unit Testing is a software testing method by which individual units of +source code, sets of one or more MCU program modules together with associated +control data, usage procedures, and operating procedures, are tested to +determine whether they are fit for use. Unit testing finds problems early +in the development cycle. + +More information about PlatformIO Unit Testing: +- https://docs.platformio.org/en/latest/advanced/unit-testing/index.html