file hosting through esp

This commit is contained in:
2022-03-20 10:49:29 +01:00
parent 47ffa00851
commit c2c0315757
3 changed files with 48 additions and 8 deletions

View File

@@ -1,7 +1,10 @@
{
// See http://go.microsoft.com/fwlink/?LinkId=827846
// for the documentation about the extensions.json format
"recommendations": [
"platformio.platformio-ide"
]
}
{
// 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"
]
}

View File

@@ -13,7 +13,10 @@ platform = espressif8266
board = d1_mini_pro
framework = arduino
monitor_speed = 115200
board_build.filesystem = littlefs
board_build.ldscript = eagle.flash.4m3m.ld
lib_deps =
https://git.aterve.com/Frozenverse/SimpleSerialProtocol.git
https://github.com/bblanchon/ArduinoJson.git
https://github.com/bblanchon/ArduinoJson.git
https://github.com/earlephilhower/arduino-esp8266littlefs-plugin.git

View File

@@ -7,6 +7,9 @@
#include <ESP8266WiFi.h>
#include <ESP8266WebServer.h>
#include <FS.h>
#include <LittleFS.h>
#include <SimpleSerialProtocol.h>
#include <StateData.h>
@@ -31,10 +34,12 @@ namespace Connection {
void initWifi();
void handleWifi();
void sendResponse(DynamicJsonDocument response);
void handleWifiStatus();
void onApiStatusRoute();
void getCurrentStateRoute();
void onDeviceRoute();
void onWebpageRoute();
}
// serial
@@ -78,10 +83,24 @@ void Connection::initWifi(){
WiFi.localIP().toString().toCharArray(ipAdress,50);
controller.sendPacket("controller", "wifi_details", ipAdress);
server.on("/", Connection::onWebpageRoute);
server.on("/api/status", Connection::onApiStatusRoute);
server.on("/api/currentstate", Connection::getCurrentStateRoute);
server.on("/api/device", Connection::onDeviceRoute);
LittleFS.begin();
server.serveStatic("/Icon2.ico", LittleFS, "/Icon2.ico");
server.serveStatic("/background.jpg", LittleFS, "/background.jpg");
server.serveStatic("/index.js", LittleFS, "/index.js");
server.serveStatic("/kksign.png", LittleFS, "/kksign.png");
server.serveStatic("/Styles.css", LittleFS, "/Styles.css");
server.onNotFound([]() {
server.send(404, "text/plain", "404: Not Found");
});
server.begin();
}
@@ -100,6 +119,14 @@ void Connection::sendResponse(DynamicJsonDocument response) {
server.send(200, "application/json", serializedJson);
}
void Connection::handleWifiStatus() {
if(WiFi.status() != WL_CONNECTED){
controller.sendPacket("controller", "wifi_status", "reconnecting");
WiFi.disconnect();
WiFi.reconnect();
}
}
void Connection::onApiStatusRoute() {
// Init Json Document
DynamicJsonDocument response(jsonBufferSize);
@@ -157,4 +184,11 @@ void Connection::onDeviceRoute() {
response["msg"] = "Processed command!";
sendResponse(response);
}
void Connection::onWebpageRoute() {
LittleFS.begin();
File file = LittleFS.open("/index.html", "r");
server.streamFile(file, "text/html");
file.close();
}