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

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