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.

sensors.cpp 8.9KB

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