Browse Source

ugly hack

tags/v0.3.0
Simon Schmidt 2 years ago
parent
commit
006404fccf
6 changed files with 136 additions and 90 deletions
  1. 36
    1
      .vscode/settings.json
  2. 1
    0
      include/filesys.h
  3. 31
    20
      include/httpserver.h
  4. 34
    37
      src/filesys.cpp
  5. 20
    0
      src/httpserver.cpp
  6. 14
    32
      src/main.cpp

+ 36
- 1
.vscode/settings.json View File

@@ -12,6 +12,41 @@
"memory": "cpp",
"ranges": "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"
}
}

+ 1
- 0
include/filesys.h View File

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

#endif // __FILESYS_H

+ 31
- 20
include/httpserver.h View File

@@ -1,37 +1,48 @@
#ifndef __HTTPSERVER_H
#define __HTTPSERVER_H

#include "filesys.h"
#include <ESP8266WebServer.h>


class HTTPServer {
private:
int port;
ESP8266WebServer* server;
bool configured = false;
const int port;
const char* rootDir;
std::function<void ()> callbackFn;
bool addRootFileHandler();
bool formatFS() {
return format_fs();
}
void listRoot() {
ls(rootDir);
}

public:
HTTPServer(int port) {
server = new ESP8266WebServer;

// server->on("/", handleRootGz);
// server->on("/style.css", handleCssGz);
// server->on("/favicon.png", handleFaviconGz);
// server->onNotFound(handleNotFound);
server->begin(port);
}
~HTTPServer() {
server->stop();
delete server;
ESP8266WebServer server;

HTTPServer(const int _port, const char* _rootDir, std::function<void ()> fn) :
port(_port), rootDir(_rootDir), callbackFn(fn)
{ }
~HTTPServer()
{
server.stop();
}

void initialize() {
server->begin(port);
bool start() {
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() {
server->handleClient();
void handleClient() {
server.handleClient();
}
};



#endif // __HTTPSERVER_H

+ 34
- 37
src/filesys.cpp View File

@@ -1,14 +1,42 @@
#include "filesys.h"

// listDir("/");
// deleteFile("/hello.txt");
// writeFile("/hello.txt", "Hello ");
// appendFile("/hello.txt", "World!\n\r");
// readFile("/hello.txt");
// listDir("/");
// listDir("/");
// deleteFile("/hello.txt");
// writeFile("/hello.txt", "Hello ");
// appendFile("/hello.txt", "World!\n\r");
// readFile("/hello.txt");
// listDir("/");

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) {
Serial.printf("ls -l %s\n\r", dirname);
Dir root = LittleFS.openDir(dirname);
@@ -30,7 +58,6 @@ void ls(const char * dirname) {
Serial.println();
}


void readFile(const char * 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
- 0
src/httpserver.cpp 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;
}

+ 14
- 32
src/main.cpp View File

@@ -19,9 +19,7 @@ extern "C" {
#include <WiFiUdp.h>
#include <ArduinoOTA.h>

#include "filesys.h"
#include <ESP8266WebServer.h>
#include <WiFiClient.h>
#include "httpserver.h"

#include "index.html.gz.h"
#include "style.css.gz.h"
@@ -40,40 +38,29 @@ void setup_pwm_pca9685();
void handleNotFound();

const int led = 13;
ESP8266WebServer server(80);
PCA9685 pwmController;

HTTPServer httpServer(80, "/", handleNotFound);
void handleNotFound() {
String message = "File Not Found\n\n";
message += "URI: ";
message += server.uri();
message += httpServer.server.uri();
message += "\nMethod: ";
message += (server.method() == HTTP_GET) ? "GET" : "POST";
message += (httpServer.server.method() == HTTP_GET) ? "GET" : "POST";
message += "\nArguments: ";
message += server.args();
message += httpServer.server.args();
message += "\n";

for (uint8_t i = 0; i < server.args(); i++) {
message += " " + server.argName(i) + ": " + server.arg(i) + "\n";
for (uint8_t i = 0; i < httpServer.server.args(); i++) {
message += " " + httpServer.server.argName(i) + ": " + httpServer.server.arg(i) + "\n";
}
server.send(404, "text/plain", message);

httpServer.server.send(404, "text/plain", message);
}

PCA9685 pwmController;

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();
}

void setup_ota() {
ArduinoOTA.setPort(8266);
@@ -153,11 +140,6 @@ void setup() {

Serial.println(F("Booting ...."));

mount_fs();
//format_fs();
ls("/");
ls("http/");
// readFile("/test.txt");

pinMode(NODEMCU_LED, OUTPUT);
pinMode(ESP12_LED, OUTPUT);
@@ -183,7 +165,7 @@ void setup() {
Serial.println(WiFi.localIP());

os_timer_setfn(&timer1, timer_callback, &timer_arg);
os_timer_arm(&timer1, 1000, true);
os_timer_arm(&timer1, 1, true);

Serial.println("Ready");
Serial.print("IP address: ");
@@ -191,7 +173,7 @@ void setup() {

setup_ota();

setup_webserver();
httpServer.start();
Serial.println("HTTP server started !");

setup_pwm_pca9685();
@@ -295,11 +277,11 @@ void loop() {
direction = 0;
}
TIMEIF_US(ArduinoOTA.handle(), 1000, "OTA");
TIMEIF_US(server.handleClient(), 1000, "HTTP");
TIMEIF_US(httpServer.handleClient(), 1000, "HTTP");

if(flag) {
flag = 0;
ledsequence(direction, onoff, 4);
Serial.printf("[%lu] interrupt\n\r", millis());
//Serial.printf("[%lu] interrupt\n\r", millis());
}
}

Loading…
Cancel
Save