ESP32 / ESP8266 & BME280 / SHT2x sensor with InfluxDB support
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.

servers.cpp 5.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232
  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. MDNS.begin(hostname.c_str());
  107. updater.setup(&server);
  108. server.on("/", handleRoot);
  109. server.on("/reset", handleReset);
  110. server.on("/calibrate", handleCalibrate);
  111. #ifdef FEATURE_RELAIS
  112. server.on("/on", handleOn);
  113. server.on("/off", handleOff);
  114. #endif // FEATURE_RELAIS
  115. MDNS.addService("http", "tcp", 80);
  116. #ifdef ENABLE_WEBSOCKETS
  117. socket.begin();
  118. #endif // ENABLE_WEBSOCKETS
  119. #endif
  120. server.begin();
  121. }
  122. #if defined(ARDUINO_ARCH_AVR)
  123. static void http_server() {
  124. // listen for incoming clients
  125. WiFiClient client = server.available();
  126. if (client) {
  127. debug.println(F("new http client"));
  128. // an http request ends with a blank line
  129. boolean currentLineIsBlank = true;
  130. while (client.connected()) {
  131. if (client.available()) {
  132. char c = client.read();
  133. debug.write(c);
  134. // if you've gotten to the end of the line (received a newline
  135. // character) and the line is blank, the http request has ended,
  136. // so you can send a reply
  137. if ((c == '\n') && currentLineIsBlank) {
  138. // send a standard http response header
  139. client.println(F("HTTP/1.1 200 OK"));
  140. client.println(F("Content-Type: text/html"));
  141. client.println(F("Connection: close"));
  142. client.println();
  143. // TODO parse path and handle different pages
  144. handleRoot(client);
  145. break;
  146. }
  147. if (c == '\n') {
  148. // you're starting a new line
  149. currentLineIsBlank = true;
  150. } else if (c != '\r') {
  151. // you've gotten a character on the current line
  152. currentLineIsBlank = false;
  153. }
  154. }
  155. }
  156. // give the web browser time to receive the data
  157. delay(10);
  158. // close the connection
  159. client.stop();
  160. debug.println(F("http client disconnected"));
  161. }
  162. }
  163. #endif
  164. static void handleServers() {
  165. #if defined(ARDUINO_ARCH_ESP8266) || defined(ARDUINO_ARCH_ESP32)
  166. server.handleClient();
  167. #else
  168. http_server();
  169. #endif
  170. #if defined(ARDUINO_ARCH_ESP8266)
  171. MDNS.update();
  172. #endif
  173. }
  174. void runServers() {
  175. unsigned long time = millis();
  176. if ((time - last_server_handle_time) >= SERVER_HANDLE_INTERVAL) {
  177. last_server_handle_time = time;
  178. handleServers();
  179. #if defined(ARDUINO_ARCH_ESP8266) || defined(ARDUINO_ARCH_ESP32)
  180. #ifdef ENABLE_WEBSOCKETS
  181. socket.loop();
  182. #endif // ENABLE_WEBSOCKETS
  183. #endif
  184. }
  185. }