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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  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. logf("Server active on Port 80 !\n\r");
  30. return true;
  31. }
  32. void HTTPServer::start_apps() {
  33. this->on("/action", HTTP_POST, [this]() {
  34. if (args()) {
  35. logf("%s=%s\n", argName(0).c_str(), arg(0).c_str());
  36. if (argName(0).equals("control")) {
  37. if (arg(0).equals("s_oben")) {
  38. treppe->overwrite_sensors(true, false);
  39. logt("control => s_oben !\n");
  40. }
  41. else if (arg(0).equals("s_unten")) {
  42. treppe->overwrite_sensors(false, true);
  43. logt("control => s_unten !\n");
  44. }
  45. }
  46. }
  47. send(200, "text/plain", "accepted");
  48. });
  49. this->on("/update", HTTP_POST, [this]() {
  50. if (args()) {
  51. for (int i = 0; i < args() - 1; i++) {
  52. logf("%s=%s\n", argName(i).c_str(), arg(i).c_str());
  53. if (argName(i).equals("range_act_pwm")) {
  54. treppe->set_active_pwm(arg(i).toInt(), VORGABE_PROZENT);
  55. logt("set_active_pwm = %d %\n", arg(i).toInt());
  56. }
  57. else if (argName(i).equals("range_idl_pwm")) {
  58. treppe->set_idle_pwm_max(arg(i).toInt(), VORGABE_PROZENT);
  59. logt("set_idle_pwm_max = %d %\n", arg(i).toInt());
  60. }
  61. else if (argName(i).equals("range_tim_sta")) {
  62. treppe->set_time_per_stair(arg(i).toInt());
  63. logt("set_time_per_stair = %d\n", arg(i).toInt());
  64. }
  65. else if (argName(i).equals("range_tim_ldr")) {
  66. treppe->set_time_ldr(arg(i).toInt());
  67. logt("set_time_ldr = %d\n", arg(i).toInt());
  68. }
  69. else if (argName(i).equals("range_ldr_shw")) {
  70. treppe->set_ldr_schwelle(arg(i).toInt(), VORGABE_12BIT);
  71. logt("set_ldr_schwelle = %d %\n", arg(i).toInt());
  72. }
  73. }
  74. }
  75. send(200, "text/plain", "accepted");
  76. });
  77. this->on("/terminal", HTTP_POST, [this]() {
  78. // logf("got /terminal\n");
  79. if (tbuf_head) {
  80. send(200, "text/plain", tbuf);
  81. tbuf_head = 0;
  82. } else {
  83. send(202, "text/plain", "");
  84. }
  85. });
  86. }
  87. template <class... Args>
  88. void HTTPServer::logf(const char *format, Args... args) const {
  89. Serial.print(log_prefix);
  90. Serial.printf(format, args...);
  91. }
  92. void HTTPServer::logt(const char *format, ...) {
  93. va_list args;
  94. va_start(args, format);
  95. // append logging string to local buffer
  96. if (tbuf_head < TBUF_LEN) {
  97. tbuf_head +=
  98. vsnprintf(tbuf + tbuf_head, TBUF_LEN - tbuf_head, format, args);
  99. }
  100. va_end(args);
  101. }