ugly hack

This commit is contained in:
Simon Schmidt 2021-06-23 15:22:38 +02:00
parent d17091651f
commit 006404fccf
6 changed files with 135 additions and 89 deletions

37
.vscode/settings.json vendored
View File

@ -12,6 +12,41 @@
"memory": "cpp", "memory": "cpp",
"ranges": "cpp", "ranges": "cpp",
"initializer_list": "cpp", "initializer_list": "cpp",
"utility": "cpp" "utility": "cpp",
"atomic": "cpp",
"bit": "cpp",
"cctype": "cpp",
"clocale": "cpp",
"cmath": "cpp",
"compare": "cpp",
"concepts": "cpp",
"cstdarg": "cpp",
"cstddef": "cpp",
"cstdint": "cpp",
"cstdio": "cpp",
"cstdlib": "cpp",
"cstring": "cpp",
"cwchar": "cpp",
"cwctype": "cpp",
"map": "cpp",
"set": "cpp",
"exception": "cpp",
"algorithm": "cpp",
"functional": "cpp",
"iterator": "cpp",
"memory_resource": "cpp",
"numeric": "cpp",
"optional": "cpp",
"system_error": "cpp",
"tuple": "cpp",
"type_traits": "cpp",
"iosfwd": "cpp",
"limits": "cpp",
"new": "cpp",
"ostream": "cpp",
"stdexcept": "cpp",
"streambuf": "cpp",
"cinttypes": "cpp",
"typeinfo": "cpp"
} }
} }

View File

@ -12,4 +12,5 @@ void writeFile(const char * path, const char * message);
void appendFile(const char * path, const char * message); void appendFile(const char * path, const char * message);
void renameFile(const char * path1, const char * path2); void renameFile(const char * path1, const char * path2);
void deleteFile(const char * path); void deleteFile(const char * path);
#endif // __FILESYS_H #endif // __FILESYS_H

View File

@ -1,37 +1,48 @@
#ifndef __HTTPSERVER_H #ifndef __HTTPSERVER_H
#define __HTTPSERVER_H #define __HTTPSERVER_H
#include "filesys.h"
#include <ESP8266WebServer.h> #include <ESP8266WebServer.h>
class HTTPServer { class HTTPServer {
private: private:
int port; bool configured = false;
ESP8266WebServer* server; const int port;
const char* rootDir;
std::function<void ()> callbackFn;
bool addRootFileHandler();
bool formatFS() {
return format_fs();
}
void listRoot() {
ls(rootDir);
}
public: public:
HTTPServer(int port) { ESP8266WebServer server;
server = new ESP8266WebServer;
// server->on("/", handleRootGz); HTTPServer(const int _port, const char* _rootDir, std::function<void ()> fn) :
// server->on("/style.css", handleCssGz); port(_port), rootDir(_rootDir), callbackFn(fn)
// server->on("/favicon.png", handleFaviconGz); { }
// server->onNotFound(handleNotFound); ~HTTPServer()
{
server->begin(port); server.stop();
}
~HTTPServer() {
server->stop();
delete server;
} }
void initialize() { bool start() {
server->begin(port); if(!mount_fs())
return false;
Serial.printf("LittleFS mounted => Starting Webserver on Port %d\n\r", this->port);
server.begin(port);
return true;
} }
void handleClient() { void handleClient() {
server->handleClient(); server.handleClient();
} }
}; };
#endif // __HTTPSERVER_H #endif // __HTTPSERVER_H

View File

@ -9,6 +9,34 @@
FSInfo fsinfo; FSInfo fsinfo;
bool mount_fs() {
if(!LittleFS.begin()) {
Serial.println("[ERROR] LittleFS.info(), reset ...");
return false;
}
if(!LittleFS.info(fsinfo)) {
Serial.println("[ERROR] LittleFS.info(), reset ...");
return false;
}
printf("Filesystem opened:\n\r");
printf("\ttotalBytes:\t%d\n\r", fsinfo.totalBytes);
printf("\tusedBytes:\t%d\n\r", fsinfo.usedBytes);
printf("\tblockSize:\t%d\n\r", fsinfo.blockSize);
printf("\tpageSize:\t%d\n\r", fsinfo.pageSize);
printf("\tmaxOpenFiles:\t%d\n\r", fsinfo.maxOpenFiles);
printf("\tmaxPathLength:\t%d\n\r", fsinfo.maxPathLength);
printf("\n\r");
return true;
}
bool format_fs() {
printf("Formatting FS ! \n\r");
return LittleFS.format();
}
void ls(const char * dirname) { void ls(const char * dirname) {
Serial.printf("ls -l %s\n\r", dirname); Serial.printf("ls -l %s\n\r", dirname);
Dir root = LittleFS.openDir(dirname); Dir root = LittleFS.openDir(dirname);
@ -30,7 +58,6 @@ void ls(const char * dirname) {
Serial.println(); Serial.println();
} }
void readFile(const char * path) { void readFile(const char * path) {
Serial.printf("Reading file: %s\n\r", path); Serial.printf("Reading file: %s\n\r", path);
@ -99,35 +126,5 @@ void deleteFile(const char * path) {
} }
bool mount_fs() {
if(!LittleFS.begin()) {
Serial.println("[ERROR] LittleFS.info(), reset ...");
return false;
}
if(!LittleFS.info(fsinfo)) {
Serial.println("[ERROR] LittleFS.info(), reset ...");
return false;
}
printf("Filesystem opened:\n\r");
printf("\ttotalBytes:\t%d\n\r", fsinfo.totalBytes);
printf("\tusedBytes:\t%d\n\r", fsinfo.usedBytes);
printf("\tblockSize:\t%d\n\r", fsinfo.blockSize);
printf("\tpageSize:\t%d\n\r", fsinfo.pageSize);
printf("\tmaxOpenFiles:\t%d\n\r", fsinfo.maxOpenFiles);
printf("\tmaxPathLength:\t%d\n\r", fsinfo.maxPathLength);
printf("\n\r");
return true;
}
bool format_fs() {
printf("Formatting FS ! \n\r");
return LittleFS.format();
}

20
src/httpserver.cpp Normal file
View File

@ -0,0 +1,20 @@
#include "httpserver.h"
bool HTTPServer::addRootFileHandler() {
// //experimental, see doku.md
// server.serveStatic("/compressed", LittleFS, "/compressed.html.gzip");
server.serveStatic("", LittleFS, "/index.html");
server.serveStatic("/", LittleFS, "/index.html");
server.serveStatic("/#", LittleFS, "/index.html");
server.serveStatic("/style.css", LittleFS, "/style.css");
server.serveStatic("/input.js", LittleFS, "/input.js");
server.serveStatic("/favicon.png", LittleFS, "/favicon.png");
server.onNotFound(callbackFn);
server.begin();
configured = true;
}

View File

@ -19,9 +19,7 @@ extern "C" {
#include <WiFiUdp.h> #include <WiFiUdp.h>
#include <ArduinoOTA.h> #include <ArduinoOTA.h>
#include "filesys.h" #include "httpserver.h"
#include <ESP8266WebServer.h>
#include <WiFiClient.h>
#include "index.html.gz.h" #include "index.html.gz.h"
#include "style.css.gz.h" #include "style.css.gz.h"
@ -40,41 +38,30 @@ void setup_pwm_pca9685();
void handleNotFound(); void handleNotFound();
const int led = 13; const int led = 13;
ESP8266WebServer server(80);
PCA9685 pwmController;
HTTPServer httpServer(80, "/", handleNotFound);
void handleNotFound() { void handleNotFound() {
String message = "File Not Found\n\n"; String message = "File Not Found\n\n";
message += "URI: "; message += "URI: ";
message += server.uri(); message += httpServer.server.uri();
message += "\nMethod: "; message += "\nMethod: ";
message += (server.method() == HTTP_GET) ? "GET" : "POST"; message += (httpServer.server.method() == HTTP_GET) ? "GET" : "POST";
message += "\nArguments: "; message += "\nArguments: ";
message += server.args(); message += httpServer.server.args();
message += "\n"; message += "\n";
for (uint8_t i = 0; i < server.args(); i++) { for (uint8_t i = 0; i < httpServer.server.args(); i++) {
message += " " + server.argName(i) + ": " + server.arg(i) + "\n"; message += " " + httpServer.server.argName(i) + ": " + httpServer.server.arg(i) + "\n";
}
server.send(404, "text/plain", message);
} }
httpServer.server.send(404, "text/plain", message);
void setup_webserver() {
// //experimental, see doku.md
// server.serveStatic("/compressed", LittleFS, "/compressed.html.gzip");
server.serveStatic("", LittleFS, "/index.html");
server.serveStatic("/", LittleFS, "/index.html");
server.serveStatic("/#", LittleFS, "/index.html");
server.serveStatic("/style.css", LittleFS, "/style.css");
server.serveStatic("/index.js", LittleFS, "/index.js");
server.serveStatic("/favicon.png", LittleFS, "/favicon.png");
server.onNotFound(handleNotFound);
server.begin();
} }
PCA9685 pwmController;
void setup_ota() { void setup_ota() {
ArduinoOTA.setPort(8266); ArduinoOTA.setPort(8266);
ArduinoOTA.setHostname("ESP_Treppenlicht"); ArduinoOTA.setHostname("ESP_Treppenlicht");
@ -153,11 +140,6 @@ void setup() {
Serial.println(F("Booting ....")); Serial.println(F("Booting ...."));
mount_fs();
//format_fs();
ls("/");
ls("http/");
// readFile("/test.txt");
pinMode(NODEMCU_LED, OUTPUT); pinMode(NODEMCU_LED, OUTPUT);
pinMode(ESP12_LED, OUTPUT); pinMode(ESP12_LED, OUTPUT);
@ -183,7 +165,7 @@ void setup() {
Serial.println(WiFi.localIP()); Serial.println(WiFi.localIP());
os_timer_setfn(&timer1, timer_callback, &timer_arg); os_timer_setfn(&timer1, timer_callback, &timer_arg);
os_timer_arm(&timer1, 1000, true); os_timer_arm(&timer1, 1, true);
Serial.println("Ready"); Serial.println("Ready");
Serial.print("IP address: "); Serial.print("IP address: ");
@ -191,7 +173,7 @@ void setup() {
setup_ota(); setup_ota();
setup_webserver(); httpServer.start();
Serial.println("HTTP server started !"); Serial.println("HTTP server started !");
setup_pwm_pca9685(); setup_pwm_pca9685();
@ -295,11 +277,11 @@ void loop() {
direction = 0; direction = 0;
} }
TIMEIF_US(ArduinoOTA.handle(), 1000, "OTA"); TIMEIF_US(ArduinoOTA.handle(), 1000, "OTA");
TIMEIF_US(server.handleClient(), 1000, "HTTP"); TIMEIF_US(httpServer.handleClient(), 1000, "HTTP");
if(flag) { if(flag) {
flag = 0; flag = 0;
ledsequence(direction, onoff, 4); ledsequence(direction, onoff, 4);
Serial.printf("[%lu] interrupt\n\r", millis()); //Serial.printf("[%lu] interrupt\n\r", millis());
} }
} }