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

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