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

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. #include "httpserver.h"
  2. auto HTTPServer::start() -> bool {
  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::start_apps() {
  33. // application handler
  34. this->on("/update", HTTP_POST, [this]() {
  35. if (args()) {
  36. for (int i = 0; i < args() - 1; i++) {
  37. Serial.printf("%s=%s\n", argName(i).c_str(), arg(i).c_str());
  38. if (argName(i).equals("range_act_pwm")) {
  39. treppe->set_active_pwm(arg(i).toInt(), VORGABE_PROZENT);
  40. logt("set_active_pwm = %d %\n", arg(i).toInt());
  41. }
  42. else if (argName(i).equals("range_idl_pwm")) {
  43. treppe->set_idle_pwm_max(arg(i).toInt(), VORGABE_PROZENT);
  44. logt("set_idle_pwm_max = %d %\n", arg(i).toInt());
  45. }
  46. else if (argName(i).equals("range_tim_sta")) {
  47. treppe->set_time_per_stair(arg(i).toInt());
  48. logt("set_time_per_stair = %d\n", arg(i).toInt());
  49. }
  50. else if (argName(i).equals("range_tim_ldr")) {
  51. treppe->set_time_ldr(arg(i).toInt());
  52. logt("set_time_ldr = %d\n", arg(i).toInt());
  53. }
  54. else if (argName(i).equals("range_ldr_shw")) {
  55. treppe->set_ldr_schwelle(arg(i).toInt(), VORGABE_PROZENT);
  56. logt("set_ldr_schwelle = %d %\n", arg(i).toInt());
  57. }
  58. }
  59. }
  60. send(200, "text/plain", "accepted");
  61. });
  62. this->on("/terminal", HTTP_POST, [this]() {
  63. // Serial.printf("got /terminal\n");
  64. if (tbuf_head) {
  65. send(200, "text/plain", tbuf);
  66. tbuf_head = 0;
  67. } else {
  68. send(202, "text/plain", "");
  69. }
  70. });
  71. }
  72. template <class... Args>
  73. void HTTPServer::logf(const char *format, Args... args) const {
  74. Serial.print(log_prefix);
  75. Serial.printf(format, args...);
  76. }
  77. void HTTPServer::logt(const char *format, ...) {
  78. va_list args;
  79. va_start(args, format);
  80. // append logging string to local buffer
  81. if (tbuf_head < TBUF_LEN) {
  82. tbuf_head +=
  83. vsnprintf(tbuf + tbuf_head, TBUF_LEN - tbuf_head, format, args);
  84. }
  85. va_end(args);
  86. }