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;
139 if (end_time - start_time > 90) {
154 if (end_time - start_time >= 40) {
155 data[byte] |= 1 << bit;
164 if (!report_errors && error_code != 0)
167 switch (error_code) {
169 ESP_LOGW(TAG,
"Waiting for DHT communication to clear failed!");
172 ESP_LOGW(TAG,
"Rising edge for bit %d failed!", i);
175 ESP_LOGW(TAG,
"Requesting data from DHT failed!");
178 ESP_LOGW(TAG,
"Falling edge for bit %d failed!", i);
186 "Data: Hum=0b" BYTE_TO_BINARY_PATTERN BYTE_TO_BINARY_PATTERN
187 ", Temp=0b" BYTE_TO_BINARY_PATTERN BYTE_TO_BINARY_PATTERN
", Checksum=0b" BYTE_TO_BINARY_PATTERN,
188 BYTE_TO_BINARY(data[0]), BYTE_TO_BINARY(data[1]), BYTE_TO_BINARY(data[2]), BYTE_TO_BINARY(data[3]),
189 BYTE_TO_BINARY(data[4]));
191 uint8_t checksum_a = data[0] + data[1] + data[2] + data[3];
196 if (checksum_a != data[4] && checksum_b != data[4]) {
198 ESP_LOGW(TAG,
"Checksum invalid: %u!=%u", checksum_a, data[4]);
204 if (checksum_a == data[4]) {
207 const uint16_t raw_temperature = uint16_t(data[2]) * 10 + (data[3] & 0x7F);
208 *temperature = raw_temperature / 10.0f;
209 if ((data[3] & 0x80) != 0) {
214 const uint16_t raw_humidity = uint16_t(data[0]) * 10 + data[1];
215 *humidity = raw_humidity / 10.0f;
219 *temperature = data[2];
223 uint16_t raw_humidity = (uint16_t(data[0] & 0xFF) << 8) | (data[1] & 0xFF);
224 uint16_t raw_temperature = (uint16_t(data[2] & 0xFF) << 8) | (data[3] & 0xFF);
226 if (raw_temperature & 0x8000) {
227 if (!(raw_temperature & 0x4000))
228 raw_temperature = ~(raw_temperature & 0x7FFF);
229 }
else if (raw_temperature & 0x800) {
230 raw_temperature |= 0xf000;
233 if (raw_temperature == 1 && raw_humidity == 10) {
235 ESP_LOGW(TAG,
"Invalid temperature+humidity! Sensor reported 1°C and 1%% Hum");
240 *humidity = raw_humidity * 0.1f;
243 *temperature = int16_t(raw_temperature) * 0.1f;
246 if (*temperature == 0.0f && (*humidity == 1.0f || *humidity == 2.0f)) {
248 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)