8 static const char *
const TAG =
"dht";
11 ESP_LOGCONFIG(TAG,
"Setting up DHT...");
17 ESP_LOGCONFIG(TAG,
"DHT:");
18 LOG_PIN(
" Pin: ", this->
pin_);
22 ESP_LOGCONFIG(TAG,
" Model: DHT11");
24 ESP_LOGCONFIG(TAG,
" Model: DHT22 (or equivalent)");
27 LOG_UPDATE_INTERVAL(
this);
38 success = this->
read_sensor_(&temperature, &humidity,
false);
44 success = this->
read_sensor_(&temperature, &humidity,
true);
48 ESP_LOGD(TAG,
"Got Temperature=%.1f°C Humidity=%.1f%%", temperature, humidity);
58 str =
" and consider manually specifying the DHT model using the model option";
60 ESP_LOGW(TAG,
"Invalid readings! Please check your wiring (pull-up resistor, pin number)%s.", str);
80 uint8_t data[5] = {0, 0, 0, 0, 0};
116 for (i = -1; i < 40; i++) {
117 uint32_t start_time =
micros();
121 if (
micros() - start_time > 90) {
134 uint32_t end_time = start_time;
138 if ((end_time =
micros()) - start_time > 90) {
153 if (end_time - start_time >= 40) {
154 data[byte] |= 1 << bit;
163 if (!report_errors && error_code != 0)
166 switch (error_code) {
168 ESP_LOGW(TAG,
"Waiting for DHT communication to clear failed!");
171 ESP_LOGW(TAG,
"Rising edge for bit %d failed!", i);
174 ESP_LOGW(TAG,
"Requesting data from DHT failed!");
177 ESP_LOGW(TAG,
"Falling edge for bit %d failed!", i);
185 "Data: Hum=0b" BYTE_TO_BINARY_PATTERN BYTE_TO_BINARY_PATTERN
186 ", Temp=0b" BYTE_TO_BINARY_PATTERN BYTE_TO_BINARY_PATTERN
", Checksum=0b" BYTE_TO_BINARY_PATTERN,
187 BYTE_TO_BINARY(data[0]), BYTE_TO_BINARY(data[1]), BYTE_TO_BINARY(data[2]), BYTE_TO_BINARY(data[3]),
188 BYTE_TO_BINARY(data[4]));
190 uint8_t checksum_a = data[0] + data[1] + data[2] + data[3];
195 if (checksum_a != data[4] && checksum_b != data[4]) {
197 ESP_LOGW(TAG,
"Checksum invalid: %u!=%u", checksum_a, data[4]);
203 if (checksum_a == data[4]) {
206 const uint16_t raw_temperature = uint16_t(data[2]) * 10 + (data[3] & 0x7F);
207 *temperature = raw_temperature / 10.0f;
208 if ((data[3] & 0x80) != 0) {
213 const uint16_t raw_humidity = uint16_t(data[0]) * 10 + data[1];
214 *humidity = raw_humidity / 10.0f;
218 *temperature = data[2];
222 uint16_t raw_humidity = (uint16_t(data[0] & 0xFF) << 8) | (data[1] & 0xFF);
223 uint16_t raw_temperature = (uint16_t(data[2] & 0xFF) << 8) | (data[3] & 0xFF);
225 if (raw_temperature & 0x8000) {
226 if (!(raw_temperature & 0x4000))
227 raw_temperature = ~(raw_temperature & 0x7FFF);
228 }
else if (raw_temperature & 0x800) {
229 raw_temperature |= 0xf000;
232 if (raw_temperature == 1 && raw_humidity == 10) {
234 ESP_LOGW(TAG,
"Invalid temperature+humidity! Sensor reported 1°C and 1%% Hum");
239 *humidity = raw_humidity * 0.1f;
242 *temperature = int16_t(raw_temperature) * 0.1f;
245 if (*temperature == 0.0f && (*humidity == 1.0f || *humidity == 2.0f)) {
247 ESP_LOGW(TAG,
"DHT reports invalid data. Is the update interval too high or the sensor damaged?");
virtual void digital_write(bool value)=0
sensor::Sensor * temperature_sensor_
const float DATA
For components that import data from directly connected sensors like DHT.
void set_dht_model(DHTModel model)
Manually select the DHT model.
void status_set_warning(const char *message="unspecified")
sensor::Sensor * humidity_sensor_
virtual void pin_mode(gpio::Flags flags)=0
uint32_t IRAM_ATTR HOT micros()
float get_setup_priority() const override
HARDWARE_LATE setup priority.
void dump_config() override
void status_clear_warning()
void publish_state(float state)
Publish a new state to the front-end.
bool read_sensor_(float *temperature, float *humidity, bool report_errors)
virtual bool digital_read()=0
void setup() override
Set up the pins and check connection.
void update() override
Update sensor values and push them to the frontend.
Helper class to disable interrupts.
Implementation of SPI Controller mode.
void IRAM_ATTR HOT delayMicroseconds(uint32_t us)