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 8.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294
  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. #ifdef ENABLE_BME280
  80. if (found_bme1) {
  81. #if defined(ARDUINO_ARCH_AVR)
  82. measurement.clear();
  83. measurement.setName("environment");
  84. #else
  85. InfluxData measurement("environment");
  86. #endif
  87. measurement.addTag("location", SENSOR_LOCATION);
  88. measurement.addTag("location-id", SENSOR_ID);
  89. measurement.addTag("placement", "1");
  90. measurement.addTag("sensor", "bme280");
  91. #if defined(ARDUINO_ARCH_ESP8266) || defined(ARDUINO_ARCH_ESP32)
  92. measurement.addTag("device", WiFi.macAddress());
  93. #endif
  94. measurement.addValue("temperature", bme1_temp());
  95. measurement.addValue("pressure", bme1_pressure());
  96. measurement.addValue("humidity", bme1_humid());
  97. debug.println(F("Writing bme1"));
  98. writeMeasurement(measurement);
  99. debug.println(F("Done!"));
  100. }
  101. if (found_bme2) {
  102. #if defined(ARDUINO_ARCH_AVR)
  103. measurement.clear();
  104. measurement.setName("environment");
  105. #else
  106. InfluxData measurement("environment");
  107. #endif
  108. measurement.addTag("location", SENSOR_LOCATION);
  109. measurement.addTag("location-id", SENSOR_ID);
  110. measurement.addTag("placement", "2");
  111. measurement.addTag("sensor", "bme280");
  112. #if defined(ARDUINO_ARCH_ESP8266) || defined(ARDUINO_ARCH_ESP32)
  113. measurement.addTag("device", WiFi.macAddress());
  114. #endif
  115. measurement.addValue("temperature", bme2_temp());
  116. measurement.addValue("pressure", bme2_pressure());
  117. measurement.addValue("humidity", bme2_humid());
  118. debug.println(F("Writing bme2"));
  119. writeMeasurement(measurement);
  120. debug.println(F("Done!"));
  121. }
  122. #endif // ENABLE_BME280
  123. if (found_sht) {
  124. #if defined(ARDUINO_ARCH_AVR)
  125. measurement.clear();
  126. measurement.setName("environment");
  127. #else
  128. InfluxData measurement("environment");
  129. #endif
  130. measurement.addTag("location", SENSOR_LOCATION);
  131. measurement.addTag("location-id", SENSOR_ID);
  132. measurement.addTag("sensor", "sht21");
  133. #if defined(ARDUINO_ARCH_ESP8266) || defined(ARDUINO_ARCH_ESP32)
  134. measurement.addTag("device", WiFi.macAddress());
  135. #endif
  136. measurement.addValue("temperature", sht_temp());
  137. measurement.addValue("humidity", sht_humid());
  138. debug.println(F("Writing sht"));
  139. writeMeasurement(measurement);
  140. debug.println(F("Done!"));
  141. }
  142. #ifdef ENABLE_CCS811
  143. if (found_ccs1) {
  144. #if defined(ARDUINO_ARCH_AVR)
  145. measurement.clear();
  146. measurement.setName("environment");
  147. #else
  148. InfluxData measurement("environment");
  149. #endif
  150. measurement.addTag("location", SENSOR_LOCATION);
  151. measurement.addTag("location-id", SENSOR_ID);
  152. measurement.addTag("placement", "1");
  153. measurement.addTag("sensor", "ccs811");
  154. String err(ccs1_error_code);
  155. measurement.addTag("error", err);
  156. #if defined(ARDUINO_ARCH_ESP8266) || defined(ARDUINO_ARCH_ESP32)
  157. measurement.addTag("device", WiFi.macAddress());
  158. #endif
  159. measurement.addValue("eco2", ccs1_eco2());
  160. measurement.addValue("tvoc", ccs1_tvoc());
  161. debug.println(F("Writing ccs1"));
  162. writeMeasurement(measurement);
  163. debug.println(F("Done!"));
  164. }
  165. if (found_ccs2) {
  166. #if defined(ARDUINO_ARCH_AVR)
  167. measurement.clear();
  168. measurement.setName("environment");
  169. #else
  170. InfluxData measurement("environment");
  171. #endif
  172. measurement.addTag("location", SENSOR_LOCATION);
  173. measurement.addTag("location-id", SENSOR_ID);
  174. measurement.addTag("placement", "2");
  175. measurement.addTag("sensor", "ccs811");
  176. String err(ccs2_error_code);
  177. measurement.addTag("error", err);
  178. #if defined(ARDUINO_ARCH_ESP8266) || defined(ARDUINO_ARCH_ESP32)
  179. measurement.addTag("device", WiFi.macAddress());
  180. #endif
  181. measurement.addValue("eco2", ccs2_eco2());
  182. measurement.addValue("tvoc", ccs2_tvoc());
  183. debug.println(F("Writing ccs2"));
  184. writeMeasurement(measurement);
  185. debug.println(F("Done!"));
  186. }
  187. #endif // ENABLE_CCS811
  188. #ifdef FEATURE_MOISTURE
  189. for (int i = 0; i < moisture_count(); i++) {
  190. int moisture = moisture_read(i);
  191. if (moisture < moisture_max()) {
  192. #if defined(ARDUINO_ARCH_AVR)
  193. measurement.clear();
  194. measurement.setName("moisture");
  195. #else
  196. InfluxData measurement("moisture");
  197. #endif
  198. measurement.addTag("location", SENSOR_LOCATION);
  199. measurement.addTag("location-id", SENSOR_ID);
  200. String sensor(i + 1, DEC);
  201. measurement.addTag("sensor", sensor);
  202. #if defined(ARDUINO_ARCH_ESP8266) || defined(ARDUINO_ARCH_ESP32)
  203. measurement.addTag("device", WiFi.macAddress());
  204. #endif
  205. measurement.addValue("value", moisture);
  206. measurement.addValue("maximum", moisture_max());
  207. debug.print(F("Writing moisture "));
  208. debug.println(i);
  209. writeMeasurement(measurement);
  210. debug.println(F("Done!"));
  211. }
  212. }
  213. #endif // FEATURE_MOISTURE
  214. #ifdef FEATURE_RELAIS
  215. for (int i = 0; i < relais_count(); i++) {
  216. InfluxData measurement("relais");
  217. measurement.addTag("location", SENSOR_LOCATION);
  218. measurement.addTag("location-id", SENSOR_ID);
  219. measurement.addTag("id", String(i));
  220. measurement.addTag("name", relais_name(i));
  221. #if defined(ARDUINO_ARCH_ESP8266) || defined(ARDUINO_ARCH_ESP32)
  222. measurement.addTag("device", WiFi.macAddress());
  223. #endif
  224. measurement.addValue("state", relais_get(i));
  225. writeMeasurement(measurement);
  226. }
  227. #endif // FEATURE_RELAIS
  228. }
  229. #else
  230. void initInflux() { }
  231. void runInflux() { }
  232. void writeDatabase() { }
  233. #endif // ENABLE_INFLUXDB_LOGGING