adds a webserver
This commit is contained in:
parent
1d7e1d2688
commit
d3cd83acc1
6
fan_control/include/server.hpp
Normal file
6
fan_control/include/server.hpp
Normal file
@ -0,0 +1,6 @@
|
||||
#include <ESP8266WebServer.h>
|
||||
#include <LittleFS.h>
|
||||
|
||||
static ESP8266WebServer server(80);
|
||||
|
||||
void server_setup();
|
@ -11,7 +11,7 @@
|
||||
[env:sonoff_basic]
|
||||
platform = espressif8266
|
||||
;board = sonoff_basic
|
||||
board = d1_mini_lite
|
||||
board = sonoff_basic
|
||||
framework = arduino
|
||||
|
||||
lib_deps =
|
||||
|
@ -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());
|
||||
|
||||
|
||||
|
52
fan_control/src/server.cpp
Normal file
52
fan_control/src/server.cpp
Normal 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");
|
||||
});
|
||||
}
|
Loading…
Reference in New Issue
Block a user