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

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