ESP32 / ESP8266 & BME280 / SHT2x sensor with InfluxDB support
Vous ne pouvez pas sélectionner plus de 25 sujets Les noms de sujets doivent commencer par une lettre ou un nombre, peuvent contenir des tirets ('-') et peuvent comporter jusqu'à 35 caractères.

sensors.cpp 7.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284
  1. /*
  2. * sensors.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 <SHT2x.h>
  15. #ifdef ENABLE_BME280
  16. #include <Adafruit_BME280.h>
  17. #endif // ENABLE_BME280
  18. #ifdef ENABLE_CCS811
  19. #include <Adafruit_CCS811.h>
  20. #endif // ENABLE_CCS811
  21. #include "config.h"
  22. #include "DebugLog.h"
  23. #include "memory.h"
  24. #include "servers.h"
  25. #include "html.h"
  26. #include "sensors.h"
  27. #if defined(ARDUINO_ARCH_ESP8266) || defined(ARDUINO_ARCH_ESP32)
  28. #include <Wire.h>
  29. #endif
  30. #define SHT_I2C_ADDRESS HTDU21D_ADDRESS
  31. #define BME_I2C_ADDRESS_1 0x76
  32. #define BME_I2C_ADDRESS_2 0x77
  33. #define CCS811_ADDRESS_1 0x5A
  34. #define CCS811_ADDRESS_2 0x5B
  35. #if defined(ARDUINO_ARCH_ESP8266)
  36. #define I2C_SDA_PIN 2
  37. #define I2C_SCL_PIN 0
  38. static TwoWire Wire2;
  39. static SHT2x sht(SHT_I2C_ADDRESS, &Wire2);
  40. #elif defined(ARDUINO_ARCH_ESP32) || defined(ARDUINO_ARCH_AVR)
  41. static SHT2x sht(SHT_I2C_ADDRESS, &Wire);
  42. #endif
  43. #ifdef ENABLE_BME280
  44. static Adafruit_BME280 bme1, bme2;
  45. bool found_bme1 = false;
  46. bool found_bme2 = false;
  47. #endif // ENABLE_BME280
  48. bool found_sht = false;
  49. #ifdef ENABLE_CCS811
  50. static Adafruit_CCS811 ccs1, ccs2;
  51. bool found_ccs1 = false;
  52. bool found_ccs2 = false;
  53. bool ccs1_data_valid = false;
  54. bool ccs2_data_valid = false;
  55. int ccs1_error_code = 0;
  56. int ccs2_error_code = 0;
  57. #endif // ENABLE_CCS811
  58. static unsigned long last_sensor_handle_time = 0;
  59. #define DEF_SENSOR_READ_FUNC(n, v) \
  60. float n(void) { \
  61. while (1) { \
  62. float a = v; \
  63. float b = v; \
  64. \
  65. if ((a > b) && ((a - b) < 2.0)) { \
  66. return (a + b) / 2.0; \
  67. } \
  68. \
  69. if ((a < b) && ((b - a) < 2.0)) { \
  70. return (a + b) / 2.0; \
  71. } \
  72. \
  73. /* to keep wdt happy */ \
  74. delay(1); \
  75. } \
  76. return 0.0; \
  77. }
  78. #ifdef ENABLE_BME280
  79. DEF_SENSOR_READ_FUNC(bme1_temp, bme1.readTemperature())
  80. DEF_SENSOR_READ_FUNC(bme2_temp, bme2.readTemperature())
  81. DEF_SENSOR_READ_FUNC(bme1_humid, bme1.readHumidity())
  82. DEF_SENSOR_READ_FUNC(bme2_humid, bme2.readHumidity())
  83. DEF_SENSOR_READ_FUNC(bme1_pressure, bme1.readPressure())
  84. DEF_SENSOR_READ_FUNC(bme2_pressure, bme2.readPressure())
  85. #endif // ENABLE_BME280
  86. DEF_SENSOR_READ_FUNC(sht_temp, sht.GetTemperature() + config.sht_temp_off)
  87. DEF_SENSOR_READ_FUNC(sht_humid, sht.GetHumidity())
  88. #ifdef ENABLE_CCS811
  89. float ccs1_eco2(void) {
  90. return ccs1.geteCO2();
  91. }
  92. float ccs1_tvoc(void) {
  93. return ccs1.getTVOC();
  94. }
  95. float ccs2_eco2(void) {
  96. return ccs2.geteCO2();
  97. }
  98. float ccs2_tvoc(void) {
  99. return ccs2.getTVOC();
  100. }
  101. static void ccs_update() {
  102. if (found_ccs1) {
  103. if (ccs1.available()) {
  104. if (found_sht) {
  105. ccs1.setEnvironmentalData(sht_humid(), sht_temp());
  106. #ifdef ENABLE_BME280
  107. } else if (found_bme1) {
  108. ccs1.setEnvironmentalData(bme1_humid(), bme1_temp());
  109. } else if (found_bme2) {
  110. ccs1.setEnvironmentalData(bme2_humid(), bme2_temp());
  111. #endif // ENABLE_BME280
  112. }
  113. ccs1_error_code = ccs1.readData();
  114. ccs1_data_valid = (ccs1_error_code == 0);
  115. }
  116. }
  117. if (found_ccs2) {
  118. if (ccs2.available()) {
  119. if (found_sht) {
  120. ccs2.setEnvironmentalData(sht_humid(), sht_temp());
  121. #ifdef ENABLE_BME280
  122. } else if (found_bme1) {
  123. ccs2.setEnvironmentalData(bme1_humid(), bme1_temp());
  124. } else if (found_bme2) {
  125. ccs2.setEnvironmentalData(bme2_humid(), bme2_temp());
  126. #endif // ENABLE_BME280
  127. }
  128. ccs2_error_code = ccs2.readData();
  129. ccs2_data_valid = (ccs2_error_code == 0);
  130. }
  131. }
  132. }
  133. #endif // ENABLE_CCS811
  134. #if defined(ARDUINO_ARCH_ESP8266) || defined(ARDUINO_ARCH_ESP32)
  135. void handleCalibrate() {
  136. bool diff = false;
  137. #ifdef ENABLE_BME280
  138. if (server.hasArg("bme1")) {
  139. diff = true;
  140. String off_string = server.arg("bme1");
  141. double real_temp = off_string.toDouble();
  142. double meas_temp = bme1_temp() - config.bme1_temp_off;
  143. config.bme1_temp_off = real_temp - meas_temp;
  144. }
  145. if (server.hasArg("bme2")) {
  146. diff = true;
  147. String off_string = server.arg("bme2");
  148. double real_temp = off_string.toDouble();
  149. double meas_temp = bme2_temp() - config.bme2_temp_off;
  150. config.bme2_temp_off = real_temp - meas_temp;
  151. }
  152. #endif // ENABLE_BME280
  153. if (server.hasArg("sht")) {
  154. diff = true;
  155. String off_string = server.arg("sht");
  156. double real_temp = off_string.toDouble();
  157. double meas_temp = sht_temp() - config.sht_temp_off;
  158. config.sht_temp_off = real_temp - meas_temp;
  159. }
  160. if (diff) {
  161. #ifdef ENABLE_BME280
  162. if (found_bme1) {
  163. bme1.setTemperatureCompensation(config.bme1_temp_off);
  164. }
  165. if (found_bme2) {
  166. bme2.setTemperatureCompensation(config.bme2_temp_off);
  167. }
  168. #endif // ENABLE_BME280
  169. mem_write(config);
  170. handlePage(42);
  171. } else {
  172. handlePage();
  173. }
  174. }
  175. #endif
  176. void initSensors() {
  177. // Init I2C and try to connect to sensors
  178. #if defined(ARDUINO_ARCH_ESP8266)
  179. #if defined(ENABLE_BME280) || defined(ENABLE_CCS811)
  180. debug.println(F("Wire2"));
  181. Wire2.begin(I2C_SDA_PIN, I2C_SCL_PIN);
  182. #endif
  183. #ifdef ENABLE_BME280
  184. debug.println(F("BME"));
  185. found_bme1 = (!bme1.begin(BME_I2C_ADDRESS_1, &Wire2)) ? false : true;
  186. found_bme2 = (!bme2.begin(BME_I2C_ADDRESS_2, &Wire2)) ? false : true;
  187. #endif // ENABLE_BME280
  188. #ifdef ENABLE_CCS811
  189. debug.println(F("CCS"));
  190. found_ccs1 = ccs1.begin(CCS811_ADDRESS_1, &Wire2);
  191. found_ccs2 = ccs2.begin(CCS811_ADDRESS_2, &Wire2);
  192. #endif // ENABLE_CCS811
  193. #elif defined(ARDUINO_ARCH_ESP32) || defined(ARDUINO_ARCH_AVR)
  194. #if defined(ARDUINO_ARCH_ESP32) && (defined(ENABLE_BME280) || defined(ENABLE_CCS811))
  195. debug.println(F("Wire"));
  196. Wire.begin();
  197. #endif
  198. #ifdef ENABLE_BME280
  199. debug.println(F("BME"));
  200. found_bme1 = (!bme1.begin(BME_I2C_ADDRESS_1, &Wire)) ? false : true;
  201. found_bme2 = (!bme2.begin(BME_I2C_ADDRESS_2, &Wire)) ? false : true;
  202. #endif // ENABLE_BME280
  203. #ifdef ENABLE_CCS811
  204. debug.println(F("CCS"));
  205. found_ccs1 = ccs1.begin(CCS811_ADDRESS_1, &Wire);
  206. found_ccs2 = ccs2.begin(CCS811_ADDRESS_2, &Wire);
  207. #endif // ENABLE_CCS811
  208. #endif
  209. debug.println(F("SHT"));
  210. found_sht = sht.GetAlive();
  211. #ifdef ENABLE_BME280
  212. // initialize temperature offsets
  213. if (found_bme1) {
  214. bme1.setTemperatureCompensation(config.bme1_temp_off);
  215. }
  216. if (found_bme2) {
  217. bme2.setTemperatureCompensation(config.bme2_temp_off);
  218. }
  219. #endif // ENABLE_BME280
  220. }
  221. void runSensors() {
  222. unsigned long time = millis();
  223. if ((time - last_sensor_handle_time) >= SENSOR_HANDLE_INTERVAL) {
  224. last_sensor_handle_time = time;
  225. #ifdef ENABLE_CCS811
  226. if (found_ccs1 || found_ccs2) {
  227. ccs_update();
  228. }
  229. #endif // ENABLE_CCS811
  230. }
  231. }