added httpserver with inheritance
This commit is contained in:
parent
006404fccf
commit
ff6b82867c
@ -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);
|
||||
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;
|
||||
}
|
||||
void handleClient() {
|
||||
server.handleClient();
|
||||
Serial.printf("[HTTPServer] Not starting Server, something went wrong !\n\r");
|
||||
return false;
|
||||
}
|
||||
};
|
||||
|
||||
|
||||
|
||||
#endif // __HTTPSERVER_H
|
@ -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;
|
||||
}
|
16
src/main.cpp
16
src/main.cpp
@ -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…
x
Reference in New Issue
Block a user