9static const char *
const TAG =
"ccs811";
14#define CHECK_TRUE(f, error_code) \
16 this->mark_failed(); \
17 this->error_code_ = (error_code); \
21#define CHECKED_IO(f) CHECK_TRUE(f, COMMUNICATION_FAILED)
41 uint8_t meas_mode = 0;
43 if (interval >= 60 * 1000) {
45 }
else if (interval >= 10 * 1000) {
47 }
else if (interval >= 1 * 1000) {
64 uint8_t hardware_version = 0;
65 uint16_t bootloader_version = 0;
66 uint16_t application_version = 0;
68 if (hardware_version_data.has_value()) {
69 hardware_version = (*hardware_version_data)[0];
72 if (bootloader_version_data.has_value()) {
73 bootloader_version =
encode_uint16((*bootloader_version_data)[0], (*bootloader_version_data)[1]);
76 if (application_version_data.has_value()) {
77 application_version =
encode_uint16((*application_version_data)[0], (*application_version_data)[1]);
80 ESP_LOGD(TAG,
"hardware_version=0x%x bootloader_version=0x%x application_version=0x%x\n", hardware_version,
81 bootloader_version, application_version);
84 sprintf(version,
"%d.%d.%d (0x%02x)", (application_version >> 12 & 15), (application_version >> 8 & 15),
85 (application_version >> 4 & 15), application_version);
86 ESP_LOGD(TAG,
"publishing version state: %s", version);
92 ESP_LOGD(TAG,
"Status indicates no data ready!");
99 if (!alg_data.has_value()) {
100 ESP_LOGW(TAG,
"Reading CCS811 data failed!");
104 auto res = *alg_data;
110 uint16_t baseline = 0;
111 if (baseline_data.has_value()) {
112 baseline =
encode_uint16((*baseline_data)[0], (*baseline_data)[1]);
115 ESP_LOGD(TAG,
"Got co2=%u ppm, tvoc=%u ppb, baseline=0x%04X", co2, tvoc, baseline);
117 if (this->
co2_ !=
nullptr)
119 if (this->
tvoc_ !=
nullptr)
130 float humidity = NAN;
133 if (std::isnan(humidity) || humidity < 0 || humidity > 100)
147 uint16_t hum_conv =
static_cast<uint16_t
>(lroundf(humidity * 512.0f + 0.5f));
148 uint16_t temp_conv =
static_cast<uint16_t
>(lroundf(
temperature * 512.0f + 0.5f));
149 this->
write_bytes(0x05, {(uint8_t) ((hum_conv >> 8) & 0xff), (uint8_t) ((hum_conv & 0xff)),
150 (uint8_t) ((temp_conv >> 8) & 0xff), (uint8_t) ((temp_conv & 0xff))});
153 ESP_LOGCONFIG(TAG,
"CCS811");
155 LOG_UPDATE_INTERVAL(
this)
156 LOG_SENSOR(
" ",
"CO2 Sensor", this->
co2_)
157 LOG_SENSOR(
" ",
"TVOC Sensor", this->
tvoc_)
158 LOG_TEXT_SENSOR(
" ",
"Firmware Version Sensor", this->
version_)
160 ESP_LOGCONFIG(TAG,
" Baseline: %04X", *this->
baseline_);
162 ESP_LOGCONFIG(TAG,
" Baseline: NOT SET");
165 switch (this->error_code_) {
167 ESP_LOGW(TAG, ESP_LOG_MSG_COMM_FAIL);
170 ESP_LOGW(TAG,
"Sensor reported an invalid ID. Is this a CCS811?");
173 ESP_LOGW(TAG,
"Sensor reported internal error");
176 ESP_LOGW(TAG,
"Sensor reported invalid APP installed.");
179 ESP_LOGW(TAG,
"Sensor reported APP start failed.");
183 ESP_LOGW(TAG,
"Unknown setup error!");
void status_set_warning(const char *message="unspecified")
void status_clear_warning()
virtual uint32_t get_update_interval() const
Get the update interval in ms of this sensor.
enum esphome::ccs811::CCS811Component::ErrorCode UNKNOWN
void dump_config() override
optional< uint16_t > baseline_
text_sensor::TextSensor * version_
sensor::Sensor * temperature_
Input sensor for temperature reading.
sensor::Sensor * humidity_
Input sensor for humidity reading.
void setup() override
Setup the sensor and test for a connection.
bool status_app_is_valid_()
void update() override
Schedule temperature+pressure readings.
bool write_bytes(uint8_t a_register, const uint8_t *data, uint8_t len, bool stop=true)
bool write_byte(uint8_t a_register, uint8_t data, bool stop=true)
bool read_byte(uint8_t a_register, uint8_t *data, bool stop=true)
bool read_bytes(uint8_t a_register, uint8_t *data, uint8_t len)
Compat APIs All methods below have been added for compatibility reasons.
void publish_state(float state)
Publish a new state to the front-end.
float state
This member variable stores the last state that has passed through all filters.
void publish_state(const std::string &state)
Providing packet encoding functions for exchanging data with a remote host.
constexpr14 std::array< uint8_t, sizeof(T)> decode_value(T val)
Decode a value into its constituent bytes (from most to least significant).
constexpr uint16_t encode_uint16(uint8_t msb, uint8_t lsb)
Encode a 16-bit value given the most and least significant byte.
void IRAM_ATTR HOT delay(uint32_t ms)