idfxx 1.0.0
Modern C++23 components for ESP-IDF
Loading...
Searching...
No Matches
dht.hpp
Go to the documentation of this file.
1// SPDX-License-Identifier: Apache-2.0
2// Copyright 2026 Chris Leishman
3
4#pragma once
5
25#include <idfxx/error>
26#include <idfxx/gpio>
27
28#include <chrono>
29#include <cstdint>
30#include <memory>
31#include <string>
32#include <thermo/thermo>
33#include <utility>
34
39namespace idfxx::dht {
40
48enum class model : uint8_t {
49 dht11,
50 dht22,
51};
52
57struct reading {
58 thermo::millicelsius temperature{0};
59 float humidity_pct = 0.0f;
60};
61
74class sensor {
75public:
87
88#ifdef CONFIG_COMPILER_CXX_EXCEPTIONS
99 [[nodiscard]] explicit sensor(config config);
100#endif
101
112 [[nodiscard]] static result<sensor> make(config config);
113
115
116 sensor(const sensor&) = delete;
117 sensor& operator=(const sensor&) = delete;
118 sensor(sensor&& other) noexcept;
119 sensor& operator=(sensor&& other) noexcept;
120
122 [[nodiscard]] idfxx::gpio pin() const noexcept;
123
125 [[nodiscard]] enum model model() const noexcept;
126
135 [[nodiscard]] std::chrono::milliseconds min_read_interval() const noexcept;
136
137#ifdef CONFIG_COMPILER_CXX_EXCEPTIONS
151 [[nodiscard]] reading read() { return unwrap(try_read()); }
152#endif
153
174 [[nodiscard]] result<reading> try_read();
175
176private:
178 struct state;
179 explicit sensor(std::unique_ptr<state> s) noexcept;
181
182 std::unique_ptr<state> _state;
183};
184
// end of idfxx_dht
186
187} // namespace idfxx::dht
188
189namespace idfxx {
190
198[[nodiscard]] inline std::string to_string(dht::model m) {
199 switch (m) {
201 return "DHT11";
203 return "DHT22";
204 default:
205 return "unknown(" + std::to_string(std::to_underlying(m)) + ')';
206 }
207}
208
209} // namespace idfxx
210
211#include "sdkconfig.h"
212#ifdef CONFIG_IDFXX_STD_FORMAT
214#include <algorithm>
215#include <format>
216namespace std {
217template<>
218struct formatter<idfxx::dht::model> {
219 constexpr auto parse(format_parse_context& ctx) { return ctx.begin(); }
220
221 template<typename FormatContext>
222 auto format(idfxx::dht::model m, FormatContext& ctx) const {
223 auto s = idfxx::to_string(m);
224 return std::copy(s.begin(), s.end(), ctx.out());
225 }
226};
227} // namespace std
229#endif // CONFIG_IDFXX_STD_FORMAT
DHT11/DHT22 sensor on a single GPIO data line.
Definition dht.hpp:74
std::chrono::milliseconds min_read_interval() const noexcept
Returns the minimum interval between reads for the model.
idfxx::gpio pin() const noexcept
Returns the configured data-line GPIO.
sensor(config config)
Constructs a sensor driver.
result< reading > try_read()
Performs one measurement.
sensor & operator=(sensor &&other) noexcept
static result< sensor > make(config config)
Creates a sensor driver.
sensor(const sensor &)=delete
reading read()
Performs one measurement.
Definition dht.hpp:151
sensor(sensor &&other) noexcept
sensor & operator=(const sensor &)=delete
A GPIO pin.
Definition gpio.hpp:62
static constexpr gpio nc()
Returns a GPIO representing "not connected".
Definition gpio.hpp:255
std::string to_string(core_id c)
Returns a string representation of a CPU core identifier.
Definition cpu.hpp:52
model
Supported sensor models.
Definition dht.hpp:48
@ dht11
DHT11: 1 °C / 1 RH resolution, 1 s minimum sampling period.
@ dht22
DHT22 / AM2301 / AM2302: 0.1 °C / 0.1 RH resolution, 2 s period.
DHT sensor classes and utilities.
Definition dht.hpp:39
Definition adc.hpp:48
T unwrap(result< T > result)
Throws a std::system_error if the result is an error.
Definition error.hpp:307
std::expected< T, std::error_code > result
result type wrapping a value or error code.
Definition error.hpp:120
One temperature and humidity measurement.
Definition dht.hpp:57
float humidity_pct
Relative humidity in percent (0–100).
Definition dht.hpp:59
thermo::millicelsius temperature
Measured temperature.
Definition dht.hpp:58
Configuration for a DHT sensor.
Definition dht.hpp:80
idfxx::gpio pin
Data line GPIO (required).
Definition dht.hpp:81
bool internal_pullup
Enable the internal pull-up on the data line.
Definition dht.hpp:85