8 static const char *
const TAG =
"ccs811";
13 #define CHECK_TRUE(f, error_code) \ 15 this->mark_failed(); \ 16 this->error_code_ = (error_code); \ 20 #define CHECKED_IO(f) CHECK_TRUE(f, COMMUNICATION_FAILED) 40 uint8_t meas_mode = 0;
42 if (interval >= 60 * 1000) {
44 }
else if (interval >= 10 * 1000) {
46 }
else if (interval >= 1 * 1000) {
59 auto hardware_version_data = this->read_bytes<1>(0x21);
60 auto bootloader_version_data = this->read_bytes<2>(0x23);
61 auto application_version_data = this->read_bytes<2>(0x24);
63 uint8_t hardware_version = 0;
64 uint16_t bootloader_version = 0;
65 uint16_t application_version = 0;
67 if (hardware_version_data.has_value()) {
68 hardware_version = (*hardware_version_data)[0];
71 if (bootloader_version_data.has_value()) {
72 bootloader_version =
encode_uint16((*bootloader_version_data)[0], (*bootloader_version_data)[1]);
75 if (application_version_data.has_value()) {
76 application_version =
encode_uint16((*application_version_data)[0], (*application_version_data)[1]);
79 ESP_LOGD(TAG,
"hardware_version=0x%x bootloader_version=0x%x application_version=0x%x\n", hardware_version,
80 bootloader_version, application_version);
83 sprintf(version,
"%d.%d.%d (0x%02x)", (application_version >> 12 & 15), (application_version >> 8 & 15),
84 (application_version >> 4 & 15), application_version);
85 ESP_LOGD(TAG,
"publishing version state: %s", version);
91 ESP_LOGD(TAG,
"Status indicates no data ready!");
97 auto alg_data = this->read_bytes<4>(0x02);
98 if (!alg_data.has_value()) {
99 ESP_LOGW(TAG,
"Reading CCS811 data failed!");
103 auto res = *alg_data;
108 auto baseline_data = this->read_bytes<2>(0x11);
109 uint16_t baseline = 0;
110 if (baseline_data.has_value()) {
111 baseline =
encode_uint16((*baseline_data)[0], (*baseline_data)[1]);
114 ESP_LOGD(TAG,
"Got co2=%u ppm, tvoc=%u ppb, baseline=0x%04X", co2, tvoc, baseline);
116 if (this->
co2_ !=
nullptr)
118 if (this->
tvoc_ !=
nullptr)
129 float humidity = NAN;
132 if (std::isnan(humidity) || humidity < 0 || humidity > 100)
137 if (std::isnan(temperature) || temperature < -25 || temperature > 50)
146 uint16_t hum_conv =
static_cast<uint16_t
>(lroundf(humidity * 512.0f + 0.5f));
147 uint16_t temp_conv =
static_cast<uint16_t
>(lroundf(temperature * 512.0f + 0.5f));
148 this->
write_bytes(0x05, {(uint8_t) ((hum_conv >> 8) & 0xff), (uint8_t) ((hum_conv & 0xff)),
149 (uint8_t) ((temp_conv >> 8) & 0xff), (uint8_t) ((temp_conv & 0xff))});
152 ESP_LOGCONFIG(TAG,
"CCS811");
154 LOG_UPDATE_INTERVAL(
this)
155 LOG_SENSOR(
" ",
"CO2 Sensor", this->
co2_)
156 LOG_SENSOR(
" ",
"TVOC Sensor", this->
tvoc_)
157 LOG_TEXT_SENSOR(
" ",
"Firmware Version Sensor", this->
version_)
159 ESP_LOGCONFIG(TAG,
" Baseline: %04X", *this->
baseline_);
161 ESP_LOGCONFIG(TAG,
" Baseline: NOT SET");
164 switch (this->error_code_) {
166 ESP_LOGW(TAG,
"Communication failed! Is the sensor connected?");
169 ESP_LOGW(TAG,
"Sensor reported an invalid ID. Is this a CCS811?");
172 ESP_LOGW(TAG,
"Sensor reported internal error");
175 ESP_LOGW(TAG,
"Sensor reported invalid APP installed.");
178 ESP_LOGW(TAG,
"Sensor reported APP start failed.");
182 ESP_LOGW(TAG,
"Unknown setup error!");
bool read_byte(uint8_t a_register, uint8_t *data, bool stop=true)
void status_set_warning(const char *message="unspecified")
void dump_config() override
sensor::Sensor * humidity_
Input sensor for humidity reading.
bool status_app_is_valid_()
void publish_state(const std::string &state)
sensor::Sensor * temperature_
Input sensor for temperature reading.
text_sensor::TextSensor * version_
void update() override
Schedule temperature+pressure readings.
enum esphome::ccs811::CCS811Component::ErrorCode UNKNOWN
void setup() override
Setup the sensor and test for a connection.
float state
This member variable stores the last state that has passed through all filters.
void status_clear_warning()
constexpr14 std::array< uint8_t, sizeof(T)> decode_value(T val)
Decode a value into its constituent bytes (from most to least significant).
void publish_state(float state)
Publish a new state to the front-end.
optional< uint16_t > baseline_
constexpr uint16_t encode_uint16(uint8_t msb, uint8_t lsb)
Encode a 16-bit value given the most and least significant byte.
virtual uint32_t get_update_interval() const
Get the update interval in ms of this sensor.
bool write_byte(uint8_t a_register, uint8_t data, bool stop=true)
Implementation of SPI Controller mode.
void IRAM_ATTR HOT delay(uint32_t ms)
bool write_bytes(uint8_t a_register, const uint8_t *data, uint8_t len, bool stop=true)