adds a webserver

This commit is contained in:
Torsten 2021-06-23 16:44:37 +03:00
parent 1d7e1d2688
commit d3cd83acc1
4 changed files with 63 additions and 2 deletions

View File

@ -0,0 +1,6 @@
#include <ESP8266WebServer.h>
#include <LittleFS.h>
static ESP8266WebServer server(80);
void server_setup();

View File

@ -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

View File

@ -1,6 +1,7 @@
#include <Arduino.h>
#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());

View File

@ -0,0 +1,52 @@
#include "server.hpp"
#include <FS.h>
#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");
});
}