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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. #include "httpserver.h"
  2. bool HTTPServer::start() {
  3. if(!mount_fs()) {
  4. logf("cant mount filesystem, EXIT !\n\r");
  5. return false;
  6. }
  7. logf("LittleFS mounted !\n\r");
  8. logf("root:\n\r");
  9. this->listRoot();
  10. logf("\n\r");
  11. // default handler
  12. this->onNotFound([this]() {
  13. String message = "File Not Found\n\n";
  14. message += "URI: ";
  15. message += uri();
  16. message += "\nMethod: ";
  17. message += (method() == HTTP_GET) ? "GET" : "POST";
  18. message += "\nArguments: ";
  19. message += args();
  20. message += "\n";
  21. for (uint8_t i = 0; i < args(); i++) {
  22. message += " " + argName(i) + ": " + arg(i) + "\n";
  23. }
  24. send(404, "text/plain", message);
  25. });
  26. // add static root file handler for http
  27. this->serveStatic("/", LittleFS, "/");
  28. this->begin();
  29. Serial.printf("Server active on Port 80 !\n\r");
  30. return true;
  31. }
  32. void HTTPServer::logf(const char *format, ...) {
  33. va_list args;
  34. va_start(args, format);
  35. char temp[64];
  36. size_t len = vsnprintf(temp, sizeof(temp), format, args);
  37. Serial.print(log_prefix);
  38. Serial.write(temp, len);
  39. va_end(args);
  40. }
  41. void HTTPServer::logt(const char *format, ...) {
  42. va_list args;
  43. va_start(args, format);
  44. // append logging string to local buffer
  45. if(tbuf_head < TBUF_LEN) {
  46. size_t len = vsnprintf(tbuf+tbuf_head, TBUF_LEN-tbuf_head, format, args);
  47. tbuf_head += len;
  48. }
  49. va_end(args);
  50. }
  51. void HTTPServer::start_apps() {
  52. // application handler
  53. this->on("/update", HTTP_POST, [this]() {
  54. // Serial.printf("got /update with args()=%d\n", args());
  55. for(int i=0; i<args(); i++) {
  56. Serial.printf("%s=%s\n", argName(i).c_str(), arg(i).c_str());
  57. }
  58. send(200, "text/plain", "accepted");
  59. });
  60. this->on("/terminal", HTTP_POST, [this]() {
  61. // Serial.printf("got /terminal\n");
  62. if(tbuf_head) {
  63. send(200, "text/plain", tbuf);
  64. tbuf_head = 0;
  65. }
  66. else {
  67. send(202, "text/plain", "");
  68. }
  69. });
  70. }