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 2.4KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  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. on("/comp.html", [this]() {
  43. File f = open("/comp.html.gz");
  44. if (!f) {
  45. Serial.println("Failed to open file for reading");
  46. }
  47. else
  48. {
  49. Serial.println("Streaming /comp.html.gz to client");
  50. const String cont = "text/html";
  51. streamFile(f, cont);
  52. }
  53. });
  54. onNotFound([this]() {
  55. String message = "File Not Found\n\n";
  56. message += "URI: ";
  57. message += uri();
  58. message += "\nMethod: ";
  59. message += (method() == HTTP_GET) ? "GET" : "POST";
  60. message += "\nArguments: ";
  61. message += args();
  62. message += "\n";
  63. for (uint8_t i = 0; i < args(); i++) {
  64. message += " " + argName(i) + ": " + arg(i) + "\n";
  65. }
  66. send(404, "text/plain", message);
  67. });
  68. if( this->addRootFileHandler() ){
  69. this->begin();
  70. Serial.printf("[HTTPServer] Server active on Port 80 !\n\r");
  71. return true;
  72. }
  73. Serial.printf("[HTTPServer] Not starting Server, something went wrong !\n\r");
  74. return false;
  75. }
  76. };