Browse Source

added httpserver with inheritance

tags/v0.3.0
Simon Schmidt 2 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

#include <ESP8266WebServer.h> #include <ESP8266WebServer.h>




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


public: 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() ~HTTPServer()
{ {
server.stop();
Serial.printf("[HTTPServer] shut down ...\n\r");
} }


bool start() { bool start() {
if(!mount_fs()) if(!mount_fs())
return false; 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 #endif // __HTTPSERVER_H

+ 7
- 10
src/httpserver.cpp View File

// //experimental, see doku.md // //experimental, see doku.md
// server.serveStatic("/compressed", LittleFS, "/compressed.html.gzip"); // 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



const int led = 13; const int led = 13;


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

void handleNotFound() { void handleNotFound() {
String message = "File Not Found\n\n"; String message = "File Not Found\n\n";
message += "URI: "; message += "URI: ";
message += httpServer.server.uri();
message += httpServer.uri();
message += "\nMethod: "; message += "\nMethod: ";
message += (httpServer.server.method() == HTTP_GET) ? "GET" : "POST";
message += (httpServer.method() == HTTP_GET) ? "GET" : "POST";
message += "\nArguments: "; message += "\nArguments: ";
message += httpServer.server.args();
message += httpServer.args();
message += "\n"; 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; PCA9685 pwmController;
setup_ota(); setup_ota();


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


setup_pwm_pca9685(); setup_pwm_pca9685();

Loading…
Cancel
Save