74 lines
2.4 KiB
C++
74 lines
2.4 KiB
C++
#include "kqm6600ta.h"
|
|
#include "esphome/core/log.h"
|
|
|
|
#include <cinttypes>
|
|
|
|
namespace esphome
|
|
{
|
|
namespace kqm6600ta
|
|
{
|
|
|
|
static const char *const TAG = "kqm6600ta";
|
|
static const uint8_t KQM6600TA_RESPONSE_LENGTH = 8;
|
|
|
|
uint8_t kqm6600ta_checksum(const uint8_t *command)
|
|
{
|
|
uint8_t sum = 0;
|
|
for (uint8_t i = 0; i < KQM6600TA_RESPONSE_LENGTH; i++)
|
|
{
|
|
sum += command[i];
|
|
}
|
|
return sum;
|
|
}
|
|
|
|
void KQM6600TAComponent::setup()
|
|
{
|
|
|
|
}
|
|
|
|
void KQM6600TAComponent::update()
|
|
{
|
|
uint8_t response[KQM6600TA_RESPONSE_LENGTH];
|
|
this->read_array(response, KQM6600TA_RESPONSE_LENGTH);
|
|
if (response[0] != 0x5F)
|
|
{
|
|
ESP_LOGW(TAG, "Invalid preamble from KQM6600TA!");
|
|
this->status_set_warning();
|
|
return;
|
|
}
|
|
|
|
uint8_t checksum = kqm6600ta_checksum(response);
|
|
if (response[7] != checksum)
|
|
{
|
|
ESP_LOGW(TAG, "KQM6600TA Checksum doesn't match: 0x%02X!=0x%02X", response[7], checksum);
|
|
this->status_set_warning();
|
|
return;
|
|
}
|
|
|
|
this->status_clear_warning();
|
|
const float voc = ((uint16_t(response[1]) << 8) | response[2]) * 0.1f;
|
|
const uint32_t formaldehyde = ((uint16_t(response[3]) << 8) | response[4]) * 10;
|
|
const uint16_t co2 = ((uint16_t(response[5]) << 8) | response[6]) * 10;
|
|
|
|
ESP_LOGD(TAG, "KQM6600TA Received VOC=%f Formaldehyde=%dmg/m³ CO₂=%dppm", voc, formaldehyde, co2);
|
|
if (this->voc_sensor_ != nullptr)
|
|
this->voc_sensor_->publish_state(voc);
|
|
if (this->formaldehyde_sensor_ != nullptr)
|
|
this->formaldehyde_sensor_->publish_state(formaldehyde);
|
|
if (this->co2_sensor_ != nullptr)
|
|
this->co2_sensor_->publish_state(co2);
|
|
}
|
|
|
|
float KQM6600TAComponent::get_setup_priority() const { return setup_priority::DATA; }
|
|
|
|
void KQM6600TAComponent::dump_config()
|
|
{
|
|
ESP_LOGCONFIG(TAG, "KQM6600TA:");
|
|
LOG_SENSOR(" ", "CO2", this->co2_sensor_);
|
|
LOG_SENSOR(" ", "VOC", this->voc_sensor_);
|
|
LOG_SENSOR(" ", "Formaldehyde", this->formaldehyde_sensor_);
|
|
this->check_uart_settings(9600);
|
|
}
|
|
|
|
} // namespace kqm6600ta
|
|
} // namespace esphome
|