ESP8266 Treppenlichtsteuerung mit OTA zum Firmware Upload
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

httpserver.h 1.5KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  1. #pragma once
  2. #include "filesys.h"
  3. #include <ESP8266WebServer.h>
  4. class HTTPServer : public ESP8266WebServer {
  5. private:
  6. const char* rootDir;
  7. bool addRootFileHandler();
  8. bool formatFS() {
  9. return format_fs();
  10. }
  11. void listRoot() {
  12. ls(rootDir);
  13. }
  14. public:
  15. HTTPServer(const int _port, const char* _rootDir) :
  16. ESP8266WebServer(_port), rootDir(_rootDir)
  17. { }
  18. ~HTTPServer()
  19. {
  20. Serial.printf("[HTTPServer] shut down ...\n\r");
  21. }
  22. bool start() {
  23. if(!mount_fs())
  24. return false;
  25. Serial.printf("[HTTPServer] LittleFS mounted !\n\r");
  26. Serial.printf("[HTTPServer] root:\n\r");
  27. this->listRoot();
  28. Serial.printf("\n\r");
  29. // https://stackoverflow.com/questions/7627098/what-is-a-lambda-expression-in-c11
  30. on("/compress.html", [this]() {
  31. File f = open("/compress.html.gz");
  32. if (!f) {
  33. Serial.println("Failed to open file for reading");
  34. }
  35. else
  36. {
  37. Serial.println("Streaming /compress.html.gz to client");
  38. const String cont = "text/html";
  39. streamFile(f, cont);
  40. }
  41. });
  42. if( this->addRootFileHandler() ){
  43. this->begin();
  44. Serial.printf("[HTTPServer] Server active on Port 80 !\n\r");
  45. return true;
  46. }
  47. Serial.printf("[HTTPServer] Not starting Server, something went wrong !\n\r");
  48. return false;
  49. }
  50. };