2021-04-28 10:22:46 +02:00
|
|
|
#include "sht4x.h"
|
|
|
|
#include "esphome/core/log.h"
|
|
|
|
|
|
|
|
namespace esphome {
|
|
|
|
namespace sht4x {
|
|
|
|
|
2021-06-10 22:19:44 +02:00
|
|
|
static const char *const TAG = "sht4x";
|
2021-04-28 10:22:46 +02:00
|
|
|
|
|
|
|
static const uint8_t MEASURECOMMANDS[] = {0xFD, 0xF6, 0xE0};
|
|
|
|
|
|
|
|
void SHT4XComponent::start_heater_() {
|
|
|
|
uint8_t cmd[] = {MEASURECOMMANDS[this->heater_command_]};
|
|
|
|
|
|
|
|
ESP_LOGD(TAG, "Heater turning on");
|
2021-09-20 11:47:51 +02:00
|
|
|
this->write(cmd, 1);
|
2021-04-28 10:22:46 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void SHT4XComponent::setup() {
|
|
|
|
ESP_LOGCONFIG(TAG, "Setting up sht4x...");
|
|
|
|
|
|
|
|
if (this->duty_cycle_ > 0.0) {
|
2023-03-20 04:38:41 +01:00
|
|
|
uint32_t heater_interval = (uint32_t) (this->heater_time_ / this->duty_cycle_);
|
2021-04-28 10:22:46 +02:00
|
|
|
ESP_LOGD(TAG, "Heater interval: %i", heater_interval);
|
|
|
|
|
|
|
|
if (this->heater_power_ == SHT4X_HEATERPOWER_HIGH) {
|
|
|
|
if (this->heater_time_ == SHT4X_HEATERTIME_LONG) {
|
|
|
|
this->heater_command_ = 0x39;
|
|
|
|
} else {
|
|
|
|
this->heater_command_ = 0x32;
|
|
|
|
}
|
|
|
|
} else if (this->heater_power_ == SHT4X_HEATERPOWER_MED) {
|
|
|
|
if (this->heater_time_ == SHT4X_HEATERTIME_LONG) {
|
|
|
|
this->heater_command_ = 0x2F;
|
|
|
|
} else {
|
|
|
|
this->heater_command_ = 0x24;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if (this->heater_time_ == SHT4X_HEATERTIME_LONG) {
|
|
|
|
this->heater_command_ = 0x1E;
|
|
|
|
} else {
|
|
|
|
this->heater_command_ = 0x15;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
ESP_LOGD(TAG, "Heater command: %x", this->heater_command_);
|
|
|
|
|
|
|
|
this->set_interval(heater_interval, std::bind(&SHT4XComponent::start_heater_, this));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void SHT4XComponent::dump_config() { LOG_I2C_DEVICE(this); }
|
|
|
|
|
|
|
|
void SHT4XComponent::update() {
|
|
|
|
// Send command
|
2022-04-13 00:19:48 +02:00
|
|
|
this->write_command(MEASURECOMMANDS[this->precision_]);
|
2021-04-28 10:22:46 +02:00
|
|
|
|
|
|
|
this->set_timeout(10, [this]() {
|
2022-04-13 00:19:48 +02:00
|
|
|
uint16_t buffer[2];
|
2021-04-28 10:22:46 +02:00
|
|
|
|
|
|
|
// Read measurement
|
2022-04-13 00:19:48 +02:00
|
|
|
bool read_status = this->read_data(buffer, 2);
|
2021-04-28 10:22:46 +02:00
|
|
|
|
|
|
|
if (read_status) {
|
|
|
|
// Evaluate and publish measurements
|
|
|
|
if (this->temp_sensor_ != nullptr) {
|
2022-04-13 00:19:48 +02:00
|
|
|
// Temp is contained in the first result word
|
|
|
|
float sensor_value_temp = buffer[0];
|
2021-04-28 10:22:46 +02:00
|
|
|
float temp = -45 + 175 * sensor_value_temp / 65535;
|
|
|
|
|
|
|
|
this->temp_sensor_->publish_state(temp);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (this->humidity_sensor_ != nullptr) {
|
2022-04-13 00:19:48 +02:00
|
|
|
// Relative humidity is in the second result word
|
|
|
|
float sensor_value_rh = buffer[1];
|
2021-04-28 10:22:46 +02:00
|
|
|
float rh = -6 + 125 * sensor_value_rh / 65535;
|
|
|
|
|
|
|
|
this->humidity_sensor_->publish_state(rh);
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
ESP_LOGD(TAG, "Sensor read failed");
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace sht4x
|
|
|
|
} // namespace esphome
|