mirror of
https://github.com/esphome/esphome.git
synced 2024-12-04 12:38:17 +01:00
7df72ddb96
* dht: Fix sensor reading from DHT22 (#239) Looking at the datasheet for DHT22, it claims to need a delay of 18ms, not 800us. Change to use the same delay as DHT11. Tested working with NodeMCUv3 and DHT22 sensor with board with built-in resistor. * dht: Add model DHT22_TYPE2 with 2ms delay instead of 0.8ms The model DHT22_TYPE2 is exactly the same as DHT22, but uses a different delay. * dht: Fix bogus negative temperature reading on DHT22_TYPE2 The workaround for negative numbers associated with DHT22s can be skipped on these sensors.
62 lines
1.6 KiB
C++
62 lines
1.6 KiB
C++
#pragma once
|
|
|
|
#include "esphome/core/component.h"
|
|
#include "esphome/components/sensor/sensor.h"
|
|
|
|
namespace esphome {
|
|
namespace dht {
|
|
|
|
enum DHTModel {
|
|
DHT_MODEL_AUTO_DETECT = 0,
|
|
DHT_MODEL_DHT11,
|
|
DHT_MODEL_DHT22,
|
|
DHT_MODEL_AM2302,
|
|
DHT_MODEL_RHT03,
|
|
DHT_MODEL_SI7021,
|
|
DHT_MODEL_DHT22_TYPE2
|
|
};
|
|
|
|
/// Component for reading temperature/humidity measurements from DHT11/DHT22 sensors.
|
|
class DHT : public PollingComponent {
|
|
public:
|
|
/** Manually select the DHT model.
|
|
*
|
|
* Valid values are:
|
|
*
|
|
* - DHT_MODEL_AUTO_DETECT (default)
|
|
* - DHT_MODEL_DHT11
|
|
* - DHT_MODEL_DHT22
|
|
* - DHT_MODEL_AM2302
|
|
* - DHT_MODEL_RHT03
|
|
* - DHT_MODEL_SI7021
|
|
* - DHT_MODEL_DHT22_TYPE2
|
|
*
|
|
* @param model The DHT model.
|
|
*/
|
|
void set_dht_model(DHTModel model);
|
|
|
|
void set_pin(GPIOPin *pin) { pin_ = pin; }
|
|
void set_model(DHTModel model) { model_ = model; }
|
|
void set_temperature_sensor(sensor::Sensor *temperature_sensor) { temperature_sensor_ = temperature_sensor; }
|
|
void set_humidity_sensor(sensor::Sensor *humidity_sensor) { humidity_sensor_ = humidity_sensor; }
|
|
|
|
/// Set up the pins and check connection.
|
|
void setup() override;
|
|
void dump_config() override;
|
|
/// Update sensor values and push them to the frontend.
|
|
void update() override;
|
|
/// HARDWARE_LATE setup priority.
|
|
float get_setup_priority() const override;
|
|
|
|
protected:
|
|
bool read_sensor_(float *temperature, float *humidity, bool report_errors);
|
|
|
|
GPIOPin *pin_;
|
|
DHTModel model_{DHT_MODEL_AUTO_DETECT};
|
|
bool is_auto_detect_{false};
|
|
sensor::Sensor *temperature_sensor_{nullptr};
|
|
sensor::Sensor *humidity_sensor_{nullptr};
|
|
};
|
|
|
|
} // namespace dht
|
|
} // namespace esphome
|