Browse Source

added httpserver with inheritance

tags/v0.3.0
Simon Schmidt 3 years ago
parent
commit
ff6b82867c
3 changed files with 33 additions and 34 deletions
  1. 17
    17
      include/httpserver.h
  2. 7
    10
      src/httpserver.cpp
  3. 9
    7
      src/main.cpp

+ 17
- 17
include/httpserver.h View File

@@ -5,12 +5,9 @@
#include <ESP8266WebServer.h>


class HTTPServer {
class HTTPServer : public ESP8266WebServer {
private:
bool configured = false;
const int port;
const char* rootDir;
std::function<void ()> callbackFn;
bool addRootFileHandler();
bool formatFS() {
@@ -21,28 +18,31 @@ private:
}

public:
ESP8266WebServer server;

HTTPServer(const int _port, const char* _rootDir, std::function<void ()> fn) :
port(_port), rootDir(_rootDir), callbackFn(fn)
HTTPServer(const int _port, const char* _rootDir) :
ESP8266WebServer(_port), rootDir(_rootDir)
{ }
~HTTPServer()
{
server.stop();
Serial.printf("[HTTPServer] shut down ...\n\r");
}

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();
Serial.printf("[HTTPServer] LittleFS mounted !\n\r");

Serial.printf("[HTTPServer] root:\n\r");
this->listRoot();
Serial.printf("\n\r");

if( this->addRootFileHandler() ){
this->begin();
Serial.printf("[HTTPServer] Server active on Port 80 !\n\r");
return true;
}
Serial.printf("[HTTPServer] Not starting Server, something went wrong !\n\r");
return false;
}
};



#endif // __HTTPSERVER_H

+ 7
- 10
src/httpserver.cpp View File

@@ -6,15 +6,12 @@ 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);
this->serveStatic("", LittleFS, "/index.html");
this->serveStatic("/", LittleFS, "/index.html");
this->serveStatic("/#", LittleFS, "/index.html");
this->serveStatic("/style.css", LittleFS, "/style.css");
this->serveStatic("/input.js", LittleFS, "/input.js");
this->serveStatic("/favicon.png", LittleFS, "/favicon.png");

server.begin();

configured = true;
return true;
}

+ 9
- 7
src/main.cpp View File

@@ -39,22 +39,23 @@ void handleNotFound();

const int led = 13;

HTTPServer httpServer(80, "/", handleNotFound);
HTTPServer httpServer(80, "/");

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

for (uint8_t i = 0; i < httpServer.server.args(); i++) {
message += " " + httpServer.server.argName(i) + ": " + httpServer.server.arg(i) + "\n";
for (uint8_t i = 0; i < httpServer.args(); i++) {
message += " " + httpServer.argName(i) + ": " + httpServer.arg(i) + "\n";
}

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

PCA9685 pwmController;
@@ -174,6 +175,7 @@ void setup() {
setup_ota();

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

setup_pwm_pca9685();

Loading…
Cancel
Save