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.

influx.cpp 7.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290
  1. /*
  2. * influx.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. #include "config.h"
  15. #include "DebugLog.h"
  16. #include "sensors.h"
  17. #include "relais.h"
  18. #include "moisture.h"
  19. #include "influx.h"
  20. #ifdef ENABLE_INFLUXDB_LOGGING
  21. #if defined(ARDUINO_ARCH_ESP8266)
  22. #include <ESP8266WiFi.h>
  23. #include <ESP8266WebServer.h>
  24. #include <ESP8266mDNS.h>
  25. #elif defined(ARDUINO_ARCH_ESP32)
  26. #include <WiFi.h>
  27. #include <WebServer.h>
  28. #include <ESPmDNS.h>
  29. #elif defined(ARDUINO_ARCH_AVR)
  30. #include <UnoWiFiDevEdSerial1.h>
  31. #include <WiFiLink.h>
  32. #endif
  33. #if defined(ARDUINO_ARCH_ESP8266) || defined(ARDUINO_ARCH_ESP32)
  34. #include <InfluxDb.h>
  35. #else
  36. #include "SimpleInflux.h"
  37. #endif
  38. static Influxdb influx(INFLUXDB_HOST, INFLUXDB_PORT);
  39. static int error_count = 0;
  40. static unsigned long last_db_write_time = 0;
  41. void initInflux() {
  42. debug.println(F("Influx"));
  43. influx.setDb(INFLUXDB_DATABASE);
  44. }
  45. void runInflux() {
  46. unsigned long time = millis();
  47. if ((time - last_db_write_time) >= DB_WRITE_INTERVAL) {
  48. last_db_write_time = time;
  49. writeDatabase();
  50. }
  51. #ifdef INFLUX_MAX_ERRORS_RESET
  52. if (error_count >= INFLUX_MAX_ERRORS_RESET) {
  53. #if defined(ARDUINO_ARCH_ESP8266) || defined(ARDUINO_ARCH_ESP32)
  54. ESP.restart();
  55. #else
  56. // TODO implement watchdog reset for AVR
  57. #endif
  58. }
  59. #endif // INFLUX_MAX_ERRORS_RESET
  60. }
  61. static boolean writeMeasurement(InfluxData &measurement) {
  62. boolean success = influx.write(measurement);
  63. if (!success) {
  64. error_count++;
  65. for (int i = 0; i < 10; i++) {
  66. digitalWrite(BUILTIN_LED_PIN, LOW); // LED on
  67. delay(LED_ERROR_BLINK_INTERVAL);
  68. digitalWrite(BUILTIN_LED_PIN, HIGH); // LED off
  69. delay(LED_ERROR_BLINK_INTERVAL);
  70. }
  71. }
  72. return success;
  73. }
  74. void writeDatabase() {
  75. #if defined(ARDUINO_ARCH_AVR)
  76. debug.println(F("Writing to InfluxDB"));
  77. InfluxData measurement("");
  78. #endif
  79. if (found_bme1) {
  80. #if defined(ARDUINO_ARCH_AVR)
  81. measurement.clear();
  82. measurement.setName("environment");
  83. #else
  84. InfluxData measurement("environment");
  85. #endif
  86. measurement.addTag("location", SENSOR_LOCATION);
  87. measurement.addTag("location-id", SENSOR_ID);
  88. measurement.addTag("placement", "1");
  89. measurement.addTag("sensor", "bme280");
  90. #if defined(ARDUINO_ARCH_ESP8266) || defined(ARDUINO_ARCH_ESP32)
  91. measurement.addTag("device", WiFi.macAddress());
  92. #endif
  93. measurement.addValue("temperature", bme1_temp());
  94. measurement.addValue("pressure", bme1_pressure());
  95. measurement.addValue("humidity", bme1_humid());
  96. debug.println(F("Writing bme1"));
  97. writeMeasurement(measurement);
  98. debug.println(F("Done!"));
  99. }
  100. if (found_bme2) {
  101. #if defined(ARDUINO_ARCH_AVR)
  102. measurement.clear();
  103. measurement.setName("environment");
  104. #else
  105. InfluxData measurement("environment");
  106. #endif
  107. measurement.addTag("location", SENSOR_LOCATION);
  108. measurement.addTag("location-id", SENSOR_ID);
  109. measurement.addTag("placement", "2");
  110. measurement.addTag("sensor", "bme280");
  111. #if defined(ARDUINO_ARCH_ESP8266) || defined(ARDUINO_ARCH_ESP32)
  112. measurement.addTag("device", WiFi.macAddress());
  113. #endif
  114. measurement.addValue("temperature", bme2_temp());
  115. measurement.addValue("pressure", bme2_pressure());
  116. measurement.addValue("humidity", bme2_humid());
  117. debug.println(F("Writing bme2"));
  118. writeMeasurement(measurement);
  119. debug.println(F("Done!"));
  120. }
  121. if (found_sht) {
  122. #if defined(ARDUINO_ARCH_AVR)
  123. measurement.clear();
  124. measurement.setName("environment");
  125. #else
  126. InfluxData measurement("environment");
  127. #endif
  128. measurement.addTag("location", SENSOR_LOCATION);
  129. measurement.addTag("location-id", SENSOR_ID);
  130. measurement.addTag("sensor", "sht21");
  131. #if defined(ARDUINO_ARCH_ESP8266) || defined(ARDUINO_ARCH_ESP32)
  132. measurement.addTag("device", WiFi.macAddress());
  133. #endif
  134. measurement.addValue("temperature", sht_temp());
  135. measurement.addValue("humidity", sht_humid());
  136. debug.println(F("Writing sht"));
  137. writeMeasurement(measurement);
  138. debug.println(F("Done!"));
  139. }
  140. #ifdef ENABLE_CCS811
  141. if (found_ccs1) {
  142. #if defined(ARDUINO_ARCH_AVR)
  143. measurement.clear();
  144. measurement.setName("environment");
  145. #else
  146. InfluxData measurement("environment");
  147. #endif
  148. measurement.addTag("location", SENSOR_LOCATION);
  149. measurement.addTag("location-id", SENSOR_ID);
  150. measurement.addTag("placement", "1");
  151. measurement.addTag("sensor", "ccs811");
  152. String err(ccs1_error_code);
  153. measurement.addTag("error", err);
  154. #if defined(ARDUINO_ARCH_ESP8266) || defined(ARDUINO_ARCH_ESP32)
  155. measurement.addTag("device", WiFi.macAddress());
  156. #endif
  157. measurement.addValue("eco2", ccs1_eco2());
  158. measurement.addValue("tvoc", ccs1_tvoc());
  159. debug.println(F("Writing ccs1"));
  160. writeMeasurement(measurement);
  161. debug.println(F("Done!"));
  162. }
  163. if (found_ccs2) {
  164. #if defined(ARDUINO_ARCH_AVR)
  165. measurement.clear();
  166. measurement.setName("environment");
  167. #else
  168. InfluxData measurement("environment");
  169. #endif
  170. measurement.addTag("location", SENSOR_LOCATION);
  171. measurement.addTag("location-id", SENSOR_ID);
  172. measurement.addTag("placement", "2");
  173. measurement.addTag("sensor", "ccs811");
  174. String err(ccs2_error_code);
  175. measurement.addTag("error", err);
  176. #if defined(ARDUINO_ARCH_ESP8266) || defined(ARDUINO_ARCH_ESP32)
  177. measurement.addTag("device", WiFi.macAddress());
  178. #endif
  179. measurement.addValue("eco2", ccs2_eco2());
  180. measurement.addValue("tvoc", ccs2_tvoc());
  181. debug.println(F("Writing ccs2"));
  182. writeMeasurement(measurement);
  183. debug.println(F("Done!"));
  184. }
  185. #endif // ENABLE_CCS811
  186. #ifdef FEATURE_MOISTURE
  187. for (int i = 0; i < moisture_count(); i++) {
  188. int moisture = moisture_read(i);
  189. if (moisture < moisture_max()) {
  190. #if defined(ARDUINO_ARCH_AVR)
  191. measurement.clear();
  192. measurement.setName("moisture");
  193. #else
  194. InfluxData measurement("moisture");
  195. #endif
  196. measurement.addTag("location", SENSOR_LOCATION);
  197. measurement.addTag("location-id", SENSOR_ID);
  198. String sensor(i + 1, DEC);
  199. measurement.addTag("sensor", sensor);
  200. #if defined(ARDUINO_ARCH_ESP8266) || defined(ARDUINO_ARCH_ESP32)
  201. measurement.addTag("device", WiFi.macAddress());
  202. #endif
  203. measurement.addValue("value", moisture);
  204. measurement.addValue("maximum", moisture_max());
  205. debug.print(F("Writing moisture "));
  206. debug.println(i);
  207. writeMeasurement(measurement);
  208. debug.println(F("Done!"));
  209. }
  210. }
  211. #endif // FEATURE_MOISTURE
  212. #ifdef FEATURE_RELAIS
  213. for (int i = 0; i < relais_count(); i++) {
  214. InfluxData measurement("relais");
  215. measurement.addTag("location", SENSOR_LOCATION);
  216. measurement.addTag("location-id", SENSOR_ID);
  217. measurement.addTag("id", String(i));
  218. measurement.addTag("name", relais_name(i));
  219. #if defined(ARDUINO_ARCH_ESP8266) || defined(ARDUINO_ARCH_ESP32)
  220. measurement.addTag("device", WiFi.macAddress());
  221. #endif
  222. measurement.addValue("state", relais_get(i));
  223. writeMeasurement(measurement);
  224. }
  225. #endif // FEATURE_RELAIS
  226. }
  227. #else
  228. void initInflux() { }
  229. void runInflux() { }
  230. void writeDatabase() { }
  231. #endif // ENABLE_INFLUXDB_LOGGING