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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292
  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. #ifdef USE_INFLUXDB_LIB
  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. influx.setDb(INFLUXDB_DATABASE);
  43. }
  44. void runInflux() {
  45. unsigned long time = millis();
  46. if ((time - last_db_write_time) >= DB_WRITE_INTERVAL) {
  47. last_db_write_time = time;
  48. writeDatabase();
  49. }
  50. #ifdef INFLUX_MAX_ERRORS_RESET
  51. if (error_count >= INFLUX_MAX_ERRORS_RESET) {
  52. #if defined(ARDUINO_ARCH_ESP8266) || defined(ARDUINO_ARCH_ESP32)
  53. debug.println(F("Resetting due to too many Influx errors"));
  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. #ifndef USE_INFLUXDB_LIB
  76. InfluxData measurement("");
  77. #endif
  78. #ifdef ENABLE_BME280
  79. if (found_bme1) {
  80. #ifndef USE_INFLUXDB_LIB
  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().c_str());
  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. #ifndef USE_INFLUXDB_LIB
  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().c_str());
  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. #endif // ENABLE_BME280
  122. if (found_sht) {
  123. #ifndef USE_INFLUXDB_LIB
  124. measurement.clear();
  125. measurement.setName("environment");
  126. #else
  127. InfluxData measurement("environment");
  128. #endif
  129. measurement.addTag("location", SENSOR_LOCATION);
  130. measurement.addTag("location-id", SENSOR_ID);
  131. measurement.addTag("sensor", "sht21");
  132. #if defined(ARDUINO_ARCH_ESP8266) || defined(ARDUINO_ARCH_ESP32)
  133. measurement.addTag("device", WiFi.macAddress().c_str());
  134. #endif
  135. measurement.addValue("temperature", sht_temp());
  136. measurement.addValue("humidity", sht_humid());
  137. debug.println(F("Writing sht"));
  138. writeMeasurement(measurement);
  139. debug.println(F("Done!"));
  140. }
  141. #ifdef ENABLE_CCS811
  142. if (found_ccs1) {
  143. #ifndef USE_INFLUXDB_LIB
  144. measurement.clear();
  145. measurement.setName("environment");
  146. #else
  147. InfluxData measurement("environment");
  148. #endif
  149. measurement.addTag("location", SENSOR_LOCATION);
  150. measurement.addTag("location-id", SENSOR_ID);
  151. measurement.addTag("placement", "1");
  152. measurement.addTag("sensor", "ccs811");
  153. String err(ccs1_error_code);
  154. measurement.addTag("error", err);
  155. #if defined(ARDUINO_ARCH_ESP8266) || defined(ARDUINO_ARCH_ESP32)
  156. measurement.addTag("device", WiFi.macAddress().c_str());
  157. #endif
  158. measurement.addValue("eco2", ccs1_eco2());
  159. measurement.addValue("tvoc", ccs1_tvoc());
  160. debug.println(F("Writing ccs1"));
  161. writeMeasurement(measurement);
  162. debug.println(F("Done!"));
  163. }
  164. if (found_ccs2) {
  165. #ifndef USE_INFLUXDB_LIB
  166. measurement.clear();
  167. measurement.setName("environment");
  168. #else
  169. InfluxData measurement("environment");
  170. #endif
  171. measurement.addTag("location", SENSOR_LOCATION);
  172. measurement.addTag("location-id", SENSOR_ID);
  173. measurement.addTag("placement", "2");
  174. measurement.addTag("sensor", "ccs811");
  175. String err(ccs2_error_code);
  176. measurement.addTag("error", err);
  177. #if defined(ARDUINO_ARCH_ESP8266) || defined(ARDUINO_ARCH_ESP32)
  178. measurement.addTag("device", WiFi.macAddress().c_str());
  179. #endif
  180. measurement.addValue("eco2", ccs2_eco2());
  181. measurement.addValue("tvoc", ccs2_tvoc());
  182. debug.println(F("Writing ccs2"));
  183. writeMeasurement(measurement);
  184. debug.println(F("Done!"));
  185. }
  186. #endif // ENABLE_CCS811
  187. #ifdef FEATURE_MOISTURE
  188. for (int i = 0; i < moisture_count(); i++) {
  189. int moisture = moisture_read(i);
  190. if (moisture < moisture_max()) {
  191. #ifndef USE_INFLUXDB_LIB
  192. measurement.clear();
  193. measurement.setName("moisture");
  194. #else
  195. InfluxData measurement("moisture");
  196. #endif
  197. measurement.addTag("location", SENSOR_LOCATION);
  198. measurement.addTag("location-id", SENSOR_ID);
  199. String sensor(i + 1, DEC);
  200. measurement.addTag("sensor", sensor);
  201. #if defined(ARDUINO_ARCH_ESP8266) || defined(ARDUINO_ARCH_ESP32)
  202. measurement.addTag("device", WiFi.macAddress().c_str());
  203. #endif
  204. measurement.addValue("value", moisture);
  205. measurement.addValue("maximum", moisture_max());
  206. debug.print(F("Writing moisture "));
  207. debug.println(i);
  208. writeMeasurement(measurement);
  209. debug.println(F("Done!"));
  210. }
  211. }
  212. #endif // FEATURE_MOISTURE
  213. #ifdef FEATURE_RELAIS
  214. for (int i = 0; i < relais_count(); i++) {
  215. InfluxData measurement("relais");
  216. measurement.addTag("location", SENSOR_LOCATION);
  217. measurement.addTag("location-id", SENSOR_ID);
  218. measurement.addTag("id", String(i));
  219. measurement.addTag("name", relais_name(i));
  220. #if defined(ARDUINO_ARCH_ESP8266) || defined(ARDUINO_ARCH_ESP32)
  221. measurement.addTag("device", WiFi.macAddress().c_str());
  222. #endif
  223. measurement.addValue("state", relais_get(i));
  224. writeMeasurement(measurement);
  225. }
  226. #endif // FEATURE_RELAIS
  227. }
  228. #else
  229. void initInflux() { }
  230. void runInflux() { }
  231. void writeDatabase() { }
  232. #endif // ENABLE_INFLUXDB_LOGGING