xsns_31_ccs811.ino 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. /*
  2. xsns_31_ccs811.ino - CCS811 gas and air quality sensor support for Sonoff-Tasmota
  3. Copyright (C) 2018 Gerhard Mutz and Theo Arends
  4. This program is free software: you can redistribute it and/or modify
  5. it under the terms of the GNU General Public License as published by
  6. the Free Software Foundation, either version 3 of the License, or
  7. (at your option) any later version.
  8. This program is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. GNU General Public License for more details.
  12. You should have received a copy of the GNU General Public License
  13. along with this program. If not, see <http://www.gnu.org/licenses/>.
  14. */
  15. #ifdef USE_I2C
  16. #ifdef USE_CCS811
  17. /*********************************************************************************************\
  18. * CCS811 - Gas (TVOC - Total Volatile Organic Compounds) and Air Quality (CO2)
  19. *
  20. * Source: Adafruit
  21. *
  22. * I2C Address: 0x5A assumes ADDR connected to Gnd, Wake also must be grounded
  23. \*********************************************************************************************/
  24. #define XSNS_31 31
  25. #include "Adafruit_CCS811.h"
  26. Adafruit_CCS811 ccs;
  27. uint8_t CCS811_ready;
  28. uint8_t CCS811_type;
  29. uint16_t eCO2;
  30. uint16_t TVOC;
  31. uint8_t tcnt = 0;
  32. uint8_t ecnt = 0;
  33. /********************************************************************************************/
  34. #define EVERYNSECONDS 5
  35. void CCS811Update(void) // Perform every n second
  36. {
  37. tcnt++;
  38. if (tcnt >= EVERYNSECONDS) {
  39. tcnt = 0;
  40. CCS811_ready = 0;
  41. if (!CCS811_type) {
  42. sint8_t res = ccs.begin(CCS811_ADDRESS);
  43. if (!res) {
  44. CCS811_type = 1;
  45. snprintf_P(log_data, sizeof(log_data), S_LOG_I2C_FOUND_AT, "CCS811", 0x5A);
  46. AddLog(LOG_LEVEL_DEBUG);
  47. } else {
  48. //snprintf_P(log_data, sizeof(log_data), "CCS811 init failed: %d",res);
  49. //AddLog(LOG_LEVEL_DEBUG);
  50. }
  51. } else {
  52. if (ccs.available()) {
  53. if (!ccs.readData()){
  54. TVOC = ccs.getTVOC();
  55. eCO2 = ccs.geteCO2();
  56. CCS811_ready = 1;
  57. if (global_update) { ccs.setEnvironmentalData((uint8_t)global_humidity, global_temperature); }
  58. ecnt = 0;
  59. }
  60. } else {
  61. // failed, count up
  62. ecnt++;
  63. if (ecnt > 6) {
  64. // after 30 seconds, restart
  65. ccs.begin(CCS811_ADDRESS);
  66. }
  67. }
  68. }
  69. }
  70. }
  71. const char HTTP_SNS_CCS811[] PROGMEM = "%s"
  72. "{s}CCS811 " D_ECO2 "{m}%d " D_UNIT_PARTS_PER_MILLION "{e}" // {s} = <tr><th>, {m} = </th><td>, {e} = </td></tr>
  73. "{s}CCS811 " D_TVOC "{m}%d " D_UNIT_PARTS_PER_BILLION "{e}";
  74. void CCS811Show(boolean json)
  75. {
  76. if (CCS811_ready) {
  77. if (json) {
  78. snprintf_P(mqtt_data, sizeof(mqtt_data), PSTR("%s,\"CCS811\":{\"" D_JSON_ECO2 "\":%d,\"" D_JSON_TVOC "\":%d}"), mqtt_data,eCO2,TVOC);
  79. #ifdef USE_DOMOTICZ
  80. if (0 == tele_period) DomoticzSensor(DZ_AIRQUALITY, eCO2);
  81. #endif // USE_DOMOTICZ
  82. #ifdef USE_WEBSERVER
  83. } else {
  84. snprintf_P(mqtt_data, sizeof(mqtt_data), HTTP_SNS_CCS811, mqtt_data, eCO2, TVOC);
  85. #endif
  86. }
  87. }
  88. }
  89. /*********************************************************************************************\
  90. * Interface
  91. \*********************************************************************************************/
  92. boolean Xsns31(byte function)
  93. {
  94. boolean result = false;
  95. if (i2c_flg) {
  96. switch (function) {
  97. case FUNC_EVERY_SECOND:
  98. CCS811Update();
  99. break;
  100. case FUNC_JSON_APPEND:
  101. CCS811Show(1);
  102. break;
  103. #ifdef USE_WEBSERVER
  104. case FUNC_WEB_APPEND:
  105. CCS811Show(0);
  106. break;
  107. #endif // USE_WEBSERVER
  108. }
  109. }
  110. return result;
  111. }
  112. #endif // USE_CCS811
  113. #endif // USE_I2C