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

"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"
} }
} }

+ 1
- 0
include/filesys.h View File

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

+ 31
- 20
include/httpserver.h View File

#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;
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: 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 #endif // __HTTPSERVER_H

+ 34
- 37
src/filesys.cpp View File

#include "filesys.h" #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; 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);
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);


} }




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

#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

#include <WiFiUdp.h> #include <WiFiUdp.h>
#include <ArduinoOTA.h> #include <ArduinoOTA.h>


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


#include "index.html.gz.h" #include "index.html.gz.h"
#include "style.css.gz.h" #include "style.css.gz.h"
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++) {
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() { void setup_ota() {
ArduinoOTA.setPort(8266); ArduinoOTA.setPort(8266);


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);
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: ");


setup_ota(); setup_ota();


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


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

Loading…
Cancel
Save