ESP32 / ESP8266 & BME280 / SHT2x sensor with InfluxDB support
Vous ne pouvez pas sélectionner plus de 25 sujets Les noms de sujets doivent commencer par une lettre ou un nombre, peuvent contenir des tirets ('-') et peuvent comporter jusqu'à 35 caractères.

servers.cpp 5.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237
  1. /*
  2. * servers.cpp
  3. *
  4. * ESP8266 / ESP32 Environmental Sensor
  5. *
  6. * ----------------------------------------------------------------------------
  7. * "THE BEER-WARE LICENSE" (Revision 42):
  8. * <xythobuz@xythobuz.de> wrote this file. As long as you retain this notice
  9. * you can do whatever you want with this stuff. If we meet some day, and you
  10. * think this stuff is worth it, you can buy me a beer in return. Thomas Buck
  11. * ----------------------------------------------------------------------------
  12. */
  13. #include <Arduino.h>
  14. #if defined(ARDUINO_ARCH_ESP8266)
  15. #include <ESP8266WiFi.h>
  16. #include <ESP8266WebServer.h>
  17. #include <ESP8266mDNS.h>
  18. #elif defined(ARDUINO_ARCH_ESP32)
  19. #include <WiFi.h>
  20. #include <WebServer.h>
  21. #include <ESPmDNS.h>
  22. #endif
  23. #if defined(ARDUINO_ARCH_ESP8266) || defined(ARDUINO_ARCH_ESP32)
  24. #include "SimpleUpdater.h"
  25. UPDATE_WEB_SERVER server(80);
  26. SimpleUpdater updater;
  27. #ifdef ENABLE_WEBSOCKETS
  28. #include <WebSocketsServer.h>
  29. WebSocketsServer socket(81);
  30. #endif // ENABLE_WEBSOCKETS
  31. #elif defined(ARDUINO_ARCH_AVR)
  32. #include <UnoWiFiDevEdSerial1.h>
  33. #include <WiFiLink.h>
  34. WiFiServer server(80);
  35. #endif
  36. #include "config.h"
  37. #include "DebugLog.h"
  38. #include "moisture.h"
  39. #include "sensors.h"
  40. #include "relais.h"
  41. #include "memory.h"
  42. #include "influx.h"
  43. #include "mqtt.h"
  44. #include "html.h"
  45. static unsigned long last_server_handle_time = 0;
  46. void wifi_send_websocket(String s) {
  47. #ifdef ENABLE_WEBSOCKETS
  48. socket.broadcastTXT(s);
  49. #endif // ENABLE_WEBSOCKETS
  50. }
  51. #ifdef FEATURE_RELAIS
  52. #if defined(ARDUINO_ARCH_ESP8266) || defined(ARDUINO_ARCH_ESP32)
  53. static void handleOn() {
  54. #else
  55. static void handleOn(WiFiClient &client) {
  56. #endif
  57. String id_string = server.arg("id");
  58. int id = id_string.toInt();
  59. if ((id >= 0) && (id < relais_count())) {
  60. relais_set(id, 1);
  61. } else {
  62. for (int i = 0; i < relais_count(); i++) {
  63. relais_set(i, 1);
  64. }
  65. }
  66. writeDatabase();
  67. #if defined(ARDUINO_ARCH_ESP8266) || defined(ARDUINO_ARCH_ESP32)
  68. handlePage(1, id);
  69. #else
  70. handlePage(client, 1, id);
  71. #endif
  72. }
  73. #if defined(ARDUINO_ARCH_ESP8266) || defined(ARDUINO_ARCH_ESP32)
  74. static void handleOff() {
  75. #else
  76. static void handleOff(WiFiClient &client) {
  77. #endif
  78. String id_string = server.arg("id");
  79. int id = id_string.toInt();
  80. if ((id >= 0) && (id < relais_count())) {
  81. relais_set(id, 0);
  82. } else {
  83. for (int i = 0; i < relais_count(); i++) {
  84. relais_set(i, 0);
  85. }
  86. }
  87. writeDatabase();
  88. #if defined(ARDUINO_ARCH_ESP8266) || defined(ARDUINO_ARCH_ESP32)
  89. handlePage(0, id);
  90. #else
  91. handlePage(client, 0, id);
  92. #endif
  93. }
  94. #endif // FEATURE_RELAIS
  95. #if defined(ARDUINO_ARCH_ESP8266) || defined(ARDUINO_ARCH_ESP32)
  96. static void handleRoot() {
  97. handlePage();
  98. #else
  99. static void handleRoot(WiFiClient &client) {
  100. handlePage(client);
  101. #endif
  102. }
  103. void initServers(String hostname) {
  104. #if defined(ARDUINO_ARCH_ESP8266) || defined(ARDUINO_ARCH_ESP32)
  105. // Setup HTTP Server
  106. debug.println(F("HTTP"));
  107. MDNS.begin(hostname.c_str());
  108. updater.setup(&server);
  109. server.on("/", handleRoot);
  110. server.on("/reset", handleReset);
  111. server.on("/calibrate", handleCalibrate);
  112. #ifdef FEATURE_RELAIS
  113. server.on("/on", handleOn);
  114. server.on("/off", handleOff);
  115. #endif // FEATURE_RELAIS
  116. MDNS.addService("http", "tcp", 80);
  117. #ifdef ENABLE_WEBSOCKETS
  118. socket.begin();
  119. #endif // ENABLE_WEBSOCKETS
  120. #endif
  121. server.begin();
  122. }
  123. #if defined(ARDUINO_ARCH_AVR)
  124. static void http_server() {
  125. // listen for incoming clients
  126. WiFiClient client = server.available();
  127. if (client) {
  128. debug.println(F("new http client"));
  129. // an http request ends with a blank line
  130. boolean currentLineIsBlank = true;
  131. while (client.connected()) {
  132. if (client.available()) {
  133. char c = client.read();
  134. debug.write(c);
  135. // if you've gotten to the end of the line (received a newline
  136. // character) and the line is blank, the http request has ended,
  137. // so you can send a reply
  138. if ((c == '\n') && currentLineIsBlank) {
  139. // send a standard http response header
  140. client.println(F("HTTP/1.1 200 OK"));
  141. client.println(F("Content-Type: text/html"));
  142. client.println(F("Connection: close"));
  143. client.println();
  144. // TODO parse path and handle different pages
  145. handleRoot(client);
  146. break;
  147. }
  148. if (c == '\n') {
  149. // you're starting a new line
  150. currentLineIsBlank = true;
  151. } else if (c != '\r') {
  152. // you've gotten a character on the current line
  153. currentLineIsBlank = false;
  154. }
  155. }
  156. }
  157. // give the web browser time to receive the data
  158. delay(10);
  159. // close the connection
  160. client.stop();
  161. debug.println(F("http client disconnected"));
  162. }
  163. }
  164. #endif
  165. static void handleServers() {
  166. #if defined(ARDUINO_ARCH_ESP8266) || defined(ARDUINO_ARCH_ESP32)
  167. server.handleClient();
  168. #else
  169. http_server();
  170. #endif
  171. #if defined(ARDUINO_ARCH_ESP8266)
  172. MDNS.update();
  173. #endif
  174. }
  175. void runServers() {
  176. unsigned long time = millis();
  177. if ((time - last_server_handle_time) >= SERVER_HANDLE_INTERVAL) {
  178. last_server_handle_time = time;
  179. handleServers();
  180. #if defined(ARDUINO_ARCH_ESP8266) || defined(ARDUINO_ARCH_ESP32)
  181. #ifdef ENABLE_WEBSOCKETS
  182. socket.loop();
  183. #endif // ENABLE_WEBSOCKETS
  184. #endif
  185. }
  186. runSensors();
  187. runMQTT();
  188. runInflux();
  189. }