idfxx 1.0.0
Modern C++23 components for ESP-IDF
Loading...
Searching...
No Matches
idfxx::dht::sensor Class Reference

DHT11/DHT22 sensor on a single GPIO data line. More...

Classes

struct  config
 Configuration for a DHT sensor. More...
 

Public Member Functions

 sensor (config config)
 Constructs a sensor driver.
 
 ~sensor ()
 
 sensor (const sensor &)=delete
 
sensoroperator= (const sensor &)=delete
 
 sensor (sensor &&other) noexcept
 
sensoroperator= (sensor &&other) noexcept
 
idfxx::gpio pin () const noexcept
 Returns the configured data-line GPIO.
 
enum model model () const noexcept
 Returns the configured sensor model.
 
std::chrono::milliseconds min_read_interval () const noexcept
 Returns the minimum interval between reads for the model.
 
reading read ()
 Performs one measurement.
 
result< readingtry_read ()
 Performs one measurement.
 

Static Public Member Functions

static result< sensormake (config config)
 Creates a sensor driver.
 

Detailed Description

DHT11/DHT22 sensor on a single GPIO data line.

Move-only. The destructor releases the RMT receive channel.

auto r = sensor.read();
// r.temperature, r.humidity_pct
DHT11/DHT22 sensor on a single GPIO data line.
Definition dht.hpp:74
reading read()
Performs one measurement.
Definition dht.hpp:151
@ dht22
DHT22 / AM2301 / AM2302: 0.1 °C / 0.1 RH resolution, 2 s period.
constexpr gpio gpio_1
Definition gpio.hpp:883

Definition at line 74 of file dht.hpp.

Constructor & Destructor Documentation

◆ sensor() [1/3]

idfxx::dht::sensor::sensor ( config  config)
explicit

Constructs a sensor driver.

Claims an RMT receive channel for the data line and configures the pin as an open-drain input/output.

Parameters
configSensor configuration.
Note
Only available when CONFIG_COMPILER_CXX_EXCEPTIONS is enabled in menuconfig.
Exceptions
std::system_erroron failure (e.g. no free RMT channel).

◆ ~sensor()

idfxx::dht::sensor::~sensor ( )

◆ sensor() [2/3]

idfxx::dht::sensor::sensor ( const sensor )
delete

◆ sensor() [3/3]

idfxx::dht::sensor::sensor ( sensor &&  other)
noexcept

Member Function Documentation

◆ make()

static result< sensor > idfxx::dht::sensor::make ( config  config)
static

Creates a sensor driver.

Claims an RMT receive channel for the data line and configures the pin as an open-drain input/output.

Parameters
configSensor configuration.
Returns
The driver, or an error.
Return values
idfxx::errc::invalid_argIf the pin is not connected.

◆ min_read_interval()

std::chrono::milliseconds idfxx::dht::sensor::min_read_interval ( ) const
noexcept

Returns the minimum interval between reads for the model.

1 s for the DHT11, 2 s for the DHT22. try_read fails with idfxx::errc::invalid_state when called again sooner.

Returns
The model's minimum sampling period.

◆ model()

enum model idfxx::dht::sensor::model ( ) const
noexcept

Returns the configured sensor model.

◆ operator=() [1/2]

sensor & idfxx::dht::sensor::operator= ( const sensor )
delete

◆ operator=() [2/2]

sensor & idfxx::dht::sensor::operator= ( sensor &&  other)
noexcept

◆ pin()

idfxx::gpio idfxx::dht::sensor::pin ( ) const
noexcept

Returns the configured data-line GPIO.

◆ read()

reading idfxx::dht::sensor::read ( )
inline

Performs one measurement.

Issues the start pulse, captures the sensor's 40-bit reply, verifies its checksum, and converts it. Blocks for the start pulse plus the reply: roughly 25 ms for the DHT11 (its ~20 ms start pulse dominates) and under 10 ms for the DHT22.

Returns
The measurement.
Note
Only available when CONFIG_COMPILER_CXX_EXCEPTIONS is enabled in menuconfig.
Exceptions
std::system_erroron failure; see try_read for the error conditions.

Definition at line 151 of file dht.hpp.

References try_read(), and idfxx::unwrap().

◆ try_read()

result< reading > idfxx::dht::sensor::try_read ( )

Performs one measurement.

Issues the start pulse, captures the sensor's 40-bit reply, verifies its checksum, and converts it. Blocks for the start pulse plus the reply: roughly 25 ms for the DHT11 (its ~20 ms start pulse dominates) and under 10 ms for the DHT22.

Returns
The measurement, or an error.
Return values
idfxx::errc::invalid_stateCalled again within min_read_interval of the previous read (the sensor needs the gap to refresh its measurement).
idfxx::errc::timeoutThe sensor did not answer the start pulse: no response preamble was captured. Typically no sensor on the line, or a wiring, power, or pull-up fault.
idfxx::errc::invalid_responseA reply began but the 40-bit frame was incomplete or malformed — line noise, a marginal pull-up, or the wrong model selected.
idfxx::errc::invalid_crcThe reply failed its checksum.

Referenced by read().


The documentation for this class was generated from the following file: