From fa8b8d3992058457f3f011b853d8767fa4974a20 Mon Sep 17 00:00:00 2001 From: Nils Date: Fri, 15 Dec 2023 02:45:03 +0100 Subject: [PATCH] Added web-server, restructuring --- {src => e-paper-project}/bmp.cpp | 4 +- {src => e-paper-project}/e-paper-project.ino | 29 +- {src => e-paper-project}/epd_spi.cpp | 0 {src => e-paper-project}/include/bmp.h | 2 +- {src => e-paper-project}/include/epd_spi.h | 0 {src => e-paper-project}/include/sd_card.h | 10 +- e-paper-project/include/web_HTML.h | 29 ++ e-paper-project/include/web_server.h | 35 +++ {src => e-paper-project}/sd_card.cpp | 17 ++ e-paper-project/web_server.cpp | 295 +++++++++++++++++++ 10 files changed, 390 insertions(+), 31 deletions(-) rename {src => e-paper-project}/bmp.cpp (92%) rename {src => e-paper-project}/e-paper-project.ino (61%) rename {src => e-paper-project}/epd_spi.cpp (100%) rename {src => e-paper-project}/include/bmp.h (95%) rename {src => e-paper-project}/include/epd_spi.h (100%) rename {src => e-paper-project}/include/sd_card.h (89%) create mode 100644 e-paper-project/include/web_HTML.h create mode 100644 e-paper-project/include/web_server.h rename {src => e-paper-project}/sd_card.cpp (85%) create mode 100644 e-paper-project/web_server.cpp diff --git a/src/bmp.cpp b/e-paper-project/bmp.cpp similarity index 92% rename from src/bmp.cpp rename to e-paper-project/bmp.cpp index e32dfca..6e0798d 100644 --- a/src/bmp.cpp +++ b/e-paper-project/bmp.cpp @@ -19,14 +19,14 @@ BMP::BMP(const char* filename) : bmp_file->seek(data_idx); } -byte BMP::read_pixel() +char BMP::read_pixel() { return bmp_file->read(); } void BMP::read_pixel(const char* buffer, const uint32_t len) { - bmp_file->read(buffer, len); + bmp_file->read((uint8_t*)buffer, len); } uint32_t BMP::position() const diff --git a/src/e-paper-project.ino b/e-paper-project/e-paper-project.ino similarity index 61% rename from src/e-paper-project.ino rename to e-paper-project/e-paper-project.ino index 66d5cd3..7609ffe 100644 --- a/src/e-paper-project.ino +++ b/e-paper-project/e-paper-project.ino @@ -1,6 +1,6 @@ #include "include/sd_card.h" #include "include/bmp.h" -#include "Arduino.h" +#include "include/web_server.h" /** * @file e-paper-project.ino @@ -12,7 +12,7 @@ * via a webserver as long as a physical switch is set high. When switched low, the * device go back to the daily sleep->image->sleep cycle. * - * @version 0.1 + * @version 1.0 * @date 2023-12-13 * * @copyright Copyright (c) 2023 @@ -27,31 +27,16 @@ * CS - [SD to GPIO 5] or [E-paper to GPIO 17] */ - void setup(){ Serial.begin(115200); - if(!SD.begin(5)){ - Serial.println("Card Mount Failed"); - return; - } - uint8_t cardType = SD.cardType(); - - if(cardType == CARD_NONE){ - Serial.println("No SD card attached"); - return; - } + SD_card_fs::init_SD(5); + Web_server::init_wifi(); + Web_server::init_server(); - Serial.printf("SD card initialized!\nSize: %lluMB\n", SD.cardSize() / (1024 * 1024)); } void loop() -{ - BMP bmp_file{"/download.bmp"}; - while (true) - { - Serial.printf("Read pixel of txt file: %x\n", bmp_file.read_pixel() & 0xff); - delay(1000); - } - +{ + Web_server::handle_client(); } \ No newline at end of file diff --git a/src/epd_spi.cpp b/e-paper-project/epd_spi.cpp similarity index 100% rename from src/epd_spi.cpp rename to e-paper-project/epd_spi.cpp diff --git a/src/include/bmp.h b/e-paper-project/include/bmp.h similarity index 95% rename from src/include/bmp.h rename to e-paper-project/include/bmp.h index bcb048c..aeb3624 100644 --- a/src/include/bmp.h +++ b/e-paper-project/include/bmp.h @@ -22,7 +22,7 @@ public: BMP(const char* filename); ~BMP() = default; // Smart pointer closes itself! - byte read_pixel(); + char read_pixel(); void read_pixel(const char* buffer, const uint32_t len); uint32_t position() const; uint32_t available() const; diff --git a/src/include/epd_spi.h b/e-paper-project/include/epd_spi.h similarity index 100% rename from src/include/epd_spi.h rename to e-paper-project/include/epd_spi.h diff --git a/src/include/sd_card.h b/e-paper-project/include/sd_card.h similarity index 89% rename from src/include/sd_card.h rename to e-paper-project/include/sd_card.h index 5677c3c..5544add 100644 --- a/src/include/sd_card.h +++ b/e-paper-project/include/sd_card.h @@ -7,7 +7,6 @@ * @brief * Useful filesystem functions for interacting with an SD-card * - * @version 0.1 * @date 2023-12-13 * * @copyright Copyright (c) 2023 @@ -16,17 +15,16 @@ #include #include -// #include "SPI.h" - -typedef enum +typedef enum : uint8_t { FS_OK = 0, FS_FAIL = 1, FS_WARNING = 2, } fs_error_t; -namespace SD_card_fs { - +namespace SD_card_fs +{ + fs_error_t init_SD(const uint16_t cs); fs_error_t create_dir(const char* path); fs_error_t remove_dir(const char* path); std::unique_ptr get_file(const char* path); diff --git a/e-paper-project/include/web_HTML.h b/e-paper-project/include/web_HTML.h new file mode 100644 index 0000000..af901dd --- /dev/null +++ b/e-paper-project/include/web_HTML.h @@ -0,0 +1,29 @@ +#ifndef WEB_HTML_H_ +#define WEB_HTML_H_ + +/** + * @file web_HTML.h + * @author Nils Forssén (nilsforssen.se) + * @brief + * web-server functionality based on https://github.com/G6EJD/ESP32-ESP8266-File-Download-Upload-Delete-Stream-and-Directory/tree/master + * + * @date 2023-12-14 + * + * @copyright Copyright (c) 2023 + */ + +namespace HTML_data +{ + const char home_page_str[] PROGMEM = ""; + + const char file_upload_str[] PROGMEM = "

Select File to Upload




[Back]

"; + + const char upload_finished_str[] PROGMEM = "

File was successfully uploaded

"; + + const char append_page_header[] PROGMEM = "File Server

File Server

"; + + const char append_page_footer[] PROGMEM = ""; +}; + + +#endif \ No newline at end of file diff --git a/e-paper-project/include/web_server.h b/e-paper-project/include/web_server.h new file mode 100644 index 0000000..7e6d880 --- /dev/null +++ b/e-paper-project/include/web_server.h @@ -0,0 +1,35 @@ +#ifndef WEB_SERVER_H_ +#define WEB_SERVER_H_ + +/** + * @file web_server.h + * @author Nils Forssén (nilsforssen.se) + * @brief + * web-server functionality based on https://github.com/G6EJD/ESP32-ESP8266-File-Download-Upload-Delete-Stream-and-Directory/tree/master + * + * @date 2023-12-14 + * + * @copyright Copyright (c) 2023 + */ + +#ifdef ESP8266 + #include + #include + #include +#else + #include + #include +#endif + +#include "credentials.h" + +#define SERVER_PORT 80 + +namespace Web_server +{ + void init_wifi(); + void init_server(); + void handle_client(); +}; + +#endif \ No newline at end of file diff --git a/src/sd_card.cpp b/e-paper-project/sd_card.cpp similarity index 85% rename from src/sd_card.cpp rename to e-paper-project/sd_card.cpp index e1b292d..e6a940f 100644 --- a/src/sd_card.cpp +++ b/e-paper-project/sd_card.cpp @@ -2,6 +2,23 @@ namespace SD_card_fs { + fs_error_t init_SD(const uint16_t cs) + { + if(!SD.begin(cs)){ + Serial.println("Card Mount Failed"); + return FS_FAIL; + } + uint8_t cardType = SD.cardType(); + + if(cardType == CARD_NONE){ + Serial.println("No SD card attached"); + return FS_FAIL; + } + + Serial.printf("SD card initialized!\nSize: %lluMB\n", SD.cardSize() / (1024 * 1024)); + return FS_OK; + } + fs_error_t create_dir(const char* path) { if(SD.mkdir(path)){ diff --git a/e-paper-project/web_server.cpp b/e-paper-project/web_server.cpp new file mode 100644 index 0000000..eeb8fb9 --- /dev/null +++ b/e-paper-project/web_server.cpp @@ -0,0 +1,295 @@ +#include "include/web_server.h" +#include "include/web_HTML.h" + +#ifdef ESP8266 + static ESP8266WebServer server{SERVER_PORT}; +#else + static ESP32WebServer server{SERVER_PORT}; +#endif + +static void send_HTML_page(const char* webpage) +{ + server.sendHeader("Cache-Control", "no-cache, no-store, must-revalidate"); + server.sendHeader("Pragma", "no-cache"); + server.sendHeader("Expires", "-1"); + server.setContentLength(CONTENT_LENGTH_UNKNOWN); + server.send(200, "text/html", ""); // Empty content inhibits Content-length header so we have to close the socket ourselves. + server.sendContent(HTML_data::append_page_header); + + server.sendContent(webpage); + server.sendContent(""); + server.client().stop(); +} + +static void home_page() +{ + send_HTML_page(HTML_data::home_page_str); +} + +static void file_upload() +{ + send_HTML_page(HTML_data::file_upload_str); +} + +static void handle_file_upload() +{ + HTTPUpload &uploadfile = server.upload(); + + if (uploadfile.status == UPLOAD_FILE_START) + { + String filename {uploadfile.filename}; + if (!filename.startsWith("/")) + filename = "/" + filename; + + Serial.print("Upload File Name: "); + Serial.println(filename); + // SD.remove(filename); // Remove a previous version, otherwise data is appended the file again + // UploadFile = SD.open(filename, FILE_WRITE); // Open the file for writing in SPIFFS (create it, if doesn't exist) + } + else if (uploadfile.status == UPLOAD_FILE_WRITE) + { + Serial.println("Writing file somewhere"); + } + else if (uploadfile.status == UPLOAD_FILE_END) + { + Serial.print("Upload Size: "); + Serial.println(uploadfile.totalSize); + } +} + +namespace Web_server +{ + void init_server() + { + server.on("/", home_page); + server.on("/upload", file_upload); + server.on("/fupload", HTTP_POST,[](){server.send(200);}, handle_file_upload); + //server.on("/delete", file_delete); + //server.on("/dir", SD_dir); + Serial.println("Web-server starting!"); + server.begin(); + } + + void init_wifi() + { + WiFi.mode(WIFI_STA); + WiFi.begin(ssid, passw); + Serial.println("Connecting"); + + while(WiFi.status() != WL_CONNECTED){ + Serial.print("."); + delay(500); + } + + Serial.println("\nConnected to the WiFi network"); + Serial.print("Local ESP32 IP: "); + Serial.println(WiFi.localIP()); + } + + void handle_client() + { + server.handleClient(); + } +}; + + + + + +/* +void Web_server::SD_dir() +{ + if (SD_present) + { + File root = SD.open("/"); + if (root) + { + root.rewindDirectory(); + SendHTML_Header(); + webpage += F("

SD Card Contents


"); + webpage += F(""); + webpage += F(""); + printDirectory("/", 0); + webpage += F("
Name/TypeType File/DirFile Size
"); + SendHTML_Content(); + root.close(); + } + else + { + SendHTML_Header(); + webpage += F("

No Files Found

"); + } + append_page_footer(); + SendHTML_Content(); + SendHTML_Stop(); // Stop is needed because no content length was sent + } + else + ReportSDNotPresent(); +} + + void printDirectory(const char *dirname, uint8_t levels) + { + File root = SD.open(dirname); + #ifdef ESP8266 + root.rewindDirectory(); // Only needed for ESP8266 + #endif + if (!root) + { + return; + } + if (!root.isDirectory()) + { + return; + } + File file = root.openNextFile(); + while (file) + { + if (webpage.length() > 1000) + { + SendHTML_Content(); + } + if (file.isDirectory()) + { + Serial.println(String(file.isDirectory() ? "Dir " : "File ") + String(file.name())); + webpage += "" + String(file.isDirectory() ? "Dir" : "File") + "" + String(file.name()) + ""; + printDirectory(file.name(), levels - 1); + } + else + { + // Serial.print(String(file.name())+"\t"); + webpage += "" + String(file.name()) + ""; + Serial.print(String(file.isDirectory() ? "Dir " : "File ") + String(file.name()) + "\t"); + webpage += "" + String(file.isDirectory() ? "Dir" : "File") + ""; + int bytes = file.size(); + String fsize = ""; + if (bytes < 1024) + fsize = String(bytes) + " B"; + else if (bytes < (1024 * 1024)) + fsize = String(bytes / 1024.0, 3) + " KB"; + else if (bytes < (1024 * 1024 * 1024)) + fsize = String(bytes / 1024.0 / 1024.0, 3) + " MB"; + else + fsize = String(bytes / 1024.0 / 1024.0 / 1024.0, 3) + " GB"; + webpage += "" + fsize + ""; + Serial.println(String(fsize)); + } + file = root.openNextFile(); + } + file.close(); + } + + void SD_file_stream(String filename) + { + if (SD_present) + { + File dataFile = SD.open("/" + filename, FILE_READ); // Now read data from SD Card + Serial.print("Streaming file: "); + Serial.println(filename); + if (dataFile) + { + if (dataFile.available()) + { // If data is available and present + String dataType = "application/octet-stream"; + if (server.streamFile(dataFile, dataType) != dataFile.size()) + { + Serial.print(F("Sent less data than expected!")); + } + } + dataFile.close(); // close the file: + } + else + ReportFileNotPresent("Cstream"); + } + else + ReportSDNotPresent(); + } + + void File_Delete() + { + if (server.args() > 0) + { // Arguments were received + if (server.hasArg("delete")) + SD_file_delete(server.arg(0)); + } + else + SelectInput("Select a File to Delete", "delete", "delete"); + } + + void SD_file_delete(String filename) + { // Delete the file + if (SD_present) + { + SendHTML_Header(); + File dataFile = SD.open("/" + filename, FILE_READ); // Now read data from SD Card + Serial.print("Deleting file: "); + Serial.println(filename); + if (dataFile) + { + if (SD.remove("/" + filename)) + { + Serial.println(F("File deleted successfully")); + webpage += "

File '" + filename + "' has been erased

"; + webpage += F("[Back]

"); + } + else + { + webpage += F("

File was not deleted - error

"); + webpage += F("[Back]

"); + } + } + else + ReportFileNotPresent("delete"); + append_page_footer(); + SendHTML_Content(); + SendHTML_Stop(); + } + else + ReportSDNotPresent(); + } + + void SendHTML_Header() + { + server.sendHeader("Cache-Control", "no-cache, no-store, must-revalidate"); + server.sendHeader("Pragma", "no-cache"); + server.sendHeader("Expires", "-1"); + server.setContentLength(CONTENT_LENGTH_UNKNOWN); + server.send(200, "text/html", ""); // Empty content inhibits Content-length header so we have to close the socket ourselves. + append_page_header(); + server.sendContent(webpage); + webpage = ""; + } + + + void ReportSDNotPresent() + { + SendHTML_Header(); + webpage += F("

No SD Card present

"); + webpage += F("[Back]

"); + append_page_footer(); + SendHTML_Content(); + SendHTML_Stop(); + } + + void ReportFileNotPresent(String target) + { + SendHTML_Header(); + webpage += F("

File does not exist

"); + webpage += F("[Back]

"; + append_page_footer(); + SendHTML_Content(); + SendHTML_Stop(); + } + + void ReportCouldNotCreateFile(String target) + { + SendHTML_Header(); + webpage += F("

Could Not Create Uploaded File (write-protected?)

"); + webpage += F("[Back]

"; + append_page_footer(); + SendHTML_Content(); + SendHTML_Stop(); + } +} +*/ \ No newline at end of file -- 2.30.2