diff --git a/fan_control/include/server.hpp b/fan_control/include/server.hpp new file mode 100644 index 0000000..f8da722 --- /dev/null +++ b/fan_control/include/server.hpp @@ -0,0 +1,6 @@ +#include +#include + +static ESP8266WebServer server(80); + +void server_setup(); diff --git a/fan_control/platformio.ini b/fan_control/platformio.ini index 30786b4..ccbde96 100644 --- a/fan_control/platformio.ini +++ b/fan_control/platformio.ini @@ -11,13 +11,13 @@ [env:sonoff_basic] platform = espressif8266 ;board = sonoff_basic -board = d1_mini_lite +board = sonoff_basic framework = arduino lib_deps = ESP8266WiFi@1.0 adafruit/DHT sensor library - + monitor_speed = 115200 diff --git a/fan_control/src/main.cpp b/fan_control/src/main.cpp index b0e6d1e..dda9587 100644 --- a/fan_control/src/main.cpp +++ b/fan_control/src/main.cpp @@ -1,6 +1,7 @@ #include #include "ota.hpp" +#include "server.hpp" // DHT Temperature & Humidity Sensor // Unified Sensor Library Example @@ -25,9 +26,11 @@ uint32_t delayMS = 3000 ; const char* ssid = "fan_XXX"; + void setup() { Serial.begin(115200); ota_setup(ssid); + server_setup(); Serial.println(WiFi.softAPIP()); diff --git a/fan_control/src/server.cpp b/fan_control/src/server.cpp new file mode 100644 index 0000000..1f37673 --- /dev/null +++ b/fan_control/src/server.cpp @@ -0,0 +1,52 @@ +#include "server.hpp" +#include + +#define DBG_OUTPUT_PORT Serial + +ESP8266WebServer* serv = NULL; + + +String getContentType(String filename){ + if(filename.endsWith(".htm")) return "text/html"; + else if(filename.endsWith(".html")) return "text/html"; + else if(filename.endsWith(".css")) return "text/css"; + else if(filename.endsWith(".js")) return "application/javascript"; + else if(filename.endsWith(".png")) return "image/png"; + else if(filename.endsWith(".gif")) return "image/gif"; + else if(filename.endsWith(".jpg")) return "image/jpeg"; + else if(filename.endsWith(".ico")) return "image/x-icon"; + else if(filename.endsWith(".xml")) return "text/xml"; + else if(filename.endsWith(".pdf")) return "application/x-pdf"; + else if(filename.endsWith(".zip")) return "application/x-zip"; + else if(filename.endsWith(".gz")) return "application/x-gzip"; + return "text/plain"; +} + + +bool handleFileRead(String path ){ + DBG_OUTPUT_PORT.println("handleFileRead: " + path); + if(path.endsWith("/")) path += "index.htm"; + String contentType = getContentType(path); + String pathWithGz = path + ".gz"; + if(LittleFS.exists(pathWithGz) || LittleFS.exists(path)){ + if(LittleFS.exists(pathWithGz)) + path += ".gz"; + File file = LittleFS.open(path, "r"); + file.close(); + return true; + } + return false; +} + +void handleHours(){ + +} + +void server_setup(){ + LittleFS.begin(); + server.on("/hour", HTTP_GET, handleHours); + server.onNotFound([](){ + if(!handleFileRead(server.uri())) + server.send(404, "text/plain", "FileNotFound"); + }); +}