webserver.cpp 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  1. #include <user_config.h>
  2. #include <SmingCore/SmingCore.h>
  3. #include "../include/configuration.h"
  4. #include "webserver.h"
  5. bool serverStarted = false;
  6. HttpServer server;
  7. extern String StrT, StrRH, StrHI, StrCR, StrCF; // Sensors string values
  8. void onIndex(HttpRequest &request, HttpResponse &response)
  9. {
  10. TemplateFileStream *tmpl = new TemplateFileStream("index.html");
  11. auto &vars = tmpl->variables();
  12. vars["T"] = StrT;
  13. vars["RH"] = StrRH;
  14. vars["HI"] = StrHI;
  15. vars["CR"] = StrCR;
  16. vars["CF"] = StrCF;
  17. vars["ADC"] = String(system_adc_read());
  18. vars["VDD"] = String(system_get_vdd33());
  19. vars["TIME"] = SystemClock.getSystemTimeString();
  20. response.sendTemplate(tmpl);
  21. }
  22. void onConfiguration(HttpRequest &request, HttpResponse &response)
  23. {
  24. MeteoConfig cfg = loadConfig();
  25. if (request.getRequestMethod() == RequestMethod::POST)
  26. {
  27. // debugf("Update config");
  28. // Update config
  29. if (request.getPostParameter("SSID").length() > 0) // Network
  30. {
  31. cfg.NetworkSSID = request.getPostParameter("SSID");
  32. cfg.NetworkPassword = request.getPostParameter("Password");
  33. }
  34. if (request.getPostParameter("TZ").length() > 0) // Correction
  35. {
  36. cfg.AddT = request.getPostParameter("T").toFloat();
  37. cfg.AddRH = request.getPostParameter("RH").toFloat();
  38. cfg.AddTZ = request.getPostParameter("TZ").toFloat();
  39. }
  40. if (request.getPostParameter("Trigger").length() > 0) // Trigger
  41. {
  42. cfg.Trigger = (TriggerType)request.getPostParameter("Trigger").toInt();
  43. cfg.RangeMin = request.getPostParameter("RMin").toFloat();
  44. cfg.RangeMax = request.getPostParameter("RMax").toFloat();
  45. }
  46. saveConfig(cfg);
  47. SystemClock.setTimeZone(cfg.AddTZ);
  48. response.redirect();
  49. }
  50. // debugf("Send template");
  51. TemplateFileStream *tmpl = new TemplateFileStream("config.html");
  52. auto &vars = tmpl->variables();
  53. vars["SSID"] = cfg.NetworkSSID;
  54. vars["T"] = String(cfg.AddT, 2);
  55. vars["RH"] = String(cfg.AddRH, 2);
  56. vars["TZ"] = String(cfg.AddTZ, 2);
  57. vars["Trigger"] = String((int)cfg.Trigger);
  58. vars["RMin"] = String(cfg.RangeMin, 2);
  59. vars["RMax"] = String(cfg.RangeMax, 2);
  60. response.sendTemplate(tmpl);
  61. }
  62. void onFile(HttpRequest &request, HttpResponse &response)
  63. {
  64. String file = request.getPath();
  65. if (file[0] == '/')
  66. file = file.substring(1);
  67. if (file[0] == '.')
  68. response.forbidden();
  69. else
  70. {
  71. response.setCache(86400, true); // It's important to use cache for better performance.
  72. response.sendFile(file);
  73. }
  74. }
  75. /// API ///
  76. void onApiDoc(HttpRequest &request, HttpResponse &response)
  77. {
  78. TemplateFileStream *tmpl = new TemplateFileStream("api.html");
  79. auto &vars = tmpl->variables();
  80. vars["IP"] = (WifiStation.isConnected() ? WifiStation.getIP() : WifiAccessPoint.getIP()).toString();
  81. response.sendTemplate(tmpl);
  82. }
  83. void onApiSensors(HttpRequest &request, HttpResponse &response)
  84. {
  85. JsonObjectStream* stream = new JsonObjectStream();
  86. JsonObject& json = stream->getRoot();
  87. json["status"] = (bool)true;
  88. JsonObject& sensors = json.createNestedObject("sensors");
  89. sensors["temperature"] = StrT.c_str();
  90. sensors["humidity"] = StrRH.c_str();
  91. sensors["heatindex"] = StrHI.c_str();
  92. sensors["comfortp"] = StrCR.c_str();
  93. sensors["comforts"] = StrCF.c_str();
  94. sensors["adcvalue"] = String(system_adc_read());
  95. sensors["vddvalue"] = String(system_get_vdd33());
  96. sensors["datetime"] = SystemClock.getSystemTimeString();
  97. response.sendJsonObject(stream);
  98. }
  99. void onApiOutput(HttpRequest &request, HttpResponse &response)
  100. {
  101. int val = request.getQueryParameter("control", "-1").toInt();
  102. if (val == 0 || val == 1)
  103. digitalWrite(CONTROL_PIN, val == 1);
  104. else
  105. val = -1;
  106. JsonObjectStream* stream = new JsonObjectStream();
  107. JsonObject& json = stream->getRoot();
  108. json["status"] = val != -1;
  109. if (val == -1) json["error"] = "Wrong control parameter value, please use: ?control=0|1";
  110. response.sendJsonObject(stream);
  111. }
  112. void startWebServer()
  113. {
  114. if (serverStarted) return;
  115. server.listen(80);
  116. server.addPath("/", onIndex);
  117. server.addPath("/api", onApiDoc);
  118. server.addPath("/api/sensors", onApiSensors);
  119. server.addPath("/api/output", onApiOutput);
  120. server.addPath("/config", onConfiguration);
  121. server.setDefaultHandler(onFile);
  122. serverStarted = true;
  123. /*
  124. if (WifiStation.isEnabled())
  125. debugf("STA: %s", WifiStation.getIP().toString().c_str());
  126. if (WifiAccessPoint.isEnabled())
  127. debugf("AP: %s", WifiAccessPoint.getIP().toString().c_str());
  128. */
  129. }