xsns_21_sgp30.ino 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. /*
  2. xsns_21_sgp30.ino - SGP30 gas and air quality sensor support for Sonoff-Tasmota
  3. Copyright (C) 2018 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_SGP30
  17. /*********************************************************************************************\
  18. * SGP30 - Gas (TVOC - Total Volatile Organic Compounds) and Air Quality (CO2)
  19. *
  20. * Source: Gerhard Mutz and Adafruit Industries
  21. *
  22. * I2C Address: 0x58
  23. \*********************************************************************************************/
  24. #define XSNS_21 21
  25. #include "Adafruit_SGP30.h"
  26. Adafruit_SGP30 sgp;
  27. uint8_t sgp30_type = 0;
  28. uint8_t sgp30_ready = 0;
  29. uint8_t sgp30_counter = 0;
  30. /********************************************************************************************/
  31. void Sgp30Update(void) // Perform every second to ensure proper operation of the baseline compensation algorithm
  32. {
  33. sgp30_ready = 0;
  34. if (!sgp30_type) {
  35. if (sgp.begin()) {
  36. sgp30_type = 1;
  37. // snprintf_P(log_data, sizeof(log_data), PSTR("SGP: Serialnumber 0x%04X-0x%04X-0x%04X"), sgp.serialnumber[0], sgp.serialnumber[1], sgp.serialnumber[2]);
  38. // AddLog(LOG_LEVEL_DEBUG);
  39. snprintf_P(log_data, sizeof(log_data), S_LOG_I2C_FOUND_AT, "SGP30", 0x58);
  40. AddLog(LOG_LEVEL_DEBUG);
  41. }
  42. } else {
  43. if (!sgp.IAQmeasure()) return; // Measurement failed
  44. sgp30_counter++;
  45. if (30 == sgp30_counter) {
  46. sgp30_counter = 0;
  47. uint16_t TVOC_base;
  48. uint16_t eCO2_base;
  49. if (!sgp.getIAQBaseline(&eCO2_base, &TVOC_base)) return; // Failed to get baseline readings
  50. // snprintf_P(log_data, sizeof(log_data), PSTR("SGP: Baseline values eCO2 0x%04X, TVOC 0x%04X"), eCO2_base, TVOC_base);
  51. // AddLog(LOG_LEVEL_DEBUG);
  52. }
  53. sgp30_ready = 1;
  54. }
  55. }
  56. const char HTTP_SNS_SGP30[] PROGMEM = "%s"
  57. "{s}SGP30 " D_ECO2 "{m}%d " D_UNIT_PARTS_PER_MILLION "{e}" // {s} = <tr><th>, {m} = </th><td>, {e} = </td></tr>
  58. "{s}SGP30 " D_TVOC "{m}%d " D_UNIT_PARTS_PER_BILLION "{e}";
  59. void Sgp30Show(boolean json)
  60. {
  61. if (sgp30_ready) {
  62. if (json) {
  63. snprintf_P(mqtt_data, sizeof(mqtt_data), PSTR("%s,\"SGP30\":{\"" D_JSON_ECO2 "\":%d,\"" D_JSON_TVOC "\":%d}"), mqtt_data, sgp.eCO2, sgp.TVOC);
  64. #ifdef USE_DOMOTICZ
  65. if (0 == tele_period) DomoticzSensor(DZ_AIRQUALITY, sgp.eCO2);
  66. #endif // USE_DOMOTICZ
  67. #ifdef USE_WEBSERVER
  68. } else {
  69. snprintf_P(mqtt_data, sizeof(mqtt_data), HTTP_SNS_SGP30, mqtt_data, sgp.eCO2, sgp.TVOC);
  70. #endif
  71. }
  72. }
  73. }
  74. /*********************************************************************************************\
  75. * Interface
  76. \*********************************************************************************************/
  77. boolean Xsns21(byte function)
  78. {
  79. boolean result = false;
  80. if (i2c_flg) {
  81. switch (function) {
  82. case FUNC_EVERY_SECOND:
  83. Sgp30Update();
  84. break;
  85. case FUNC_JSON_APPEND:
  86. Sgp30Show(1);
  87. break;
  88. #ifdef USE_WEBSERVER
  89. case FUNC_WEB_APPEND:
  90. Sgp30Show(0);
  91. break;
  92. #endif // USE_WEBSERVER
  93. }
  94. }
  95. return result;
  96. }
  97. #endif // USE_SGP30
  98. #endif // USE_I2C