| Crates.io | dht22-driver |
| lib.rs | dht22-driver |
| version | 0.1.1 |
| created_at | 2024-01-02 15:47:00.776818+00 |
| updated_at | 2024-01-02 15:54:30.309114+00 |
| description | No-std, no-dependency, platform-agnostic driver for the dht22 sensor |
| homepage | |
| repository | https://github.com/Tastaturtaste/dht22-driver |
| max_upload_size | |
| id | 1086361 |
| size | 24,139 |
No-std, no-dependency, platform-agnostic driver for the dht22 sensor
std provides an implementation of std::error::Error for the DhtError, allowing more ergonomic error handling. When error_in_core is stabelized this feature may be deprecated and possibly removed.A specific example for the esp32c3mini1 board is available in the examples folder. This example can also be run using wokwi (see the accompanying justfile for further details how to run it).
An example usage without any platform specific implementation is shown below.
use dht22_driver::{IOPin, MicroTimer, Microseconds, Dht22};
// Newtype over device specific GPIO pin type
struct Pin;
#[derive(Debug)]
struct SomeMCUSpecificError;
impl IOPin for Pin
{
type DeviceError = SomeMCUSpecificError;
fn set_low(&mut self) -> Result<(), Self::DeviceError> {
todo!()
}
fn set_high(&mut self) -> Result<(), Self::DeviceError> {
todo!()
}
fn is_low(&self) -> bool {
todo!()
}
fn is_high(&self) -> bool {
todo!()
}
}
// Newtype over device specific clock/timer
struct DeviceTimer;
impl MicroTimer for DeviceTimer {
fn now(&self) -> Microseconds {
Microseconds(
todo!()
)
}
}
fn main() -> Result<(), SomeMCUSpecificError> {
let mut pin = Pin;
pin.set_high()?;
let timer = DeviceTimer;
let mut sensor = Dht22::new(pin, timer);
loop {
std::thread::sleep(std::time::Duration::from_secs(2));
if let Ok(reading) = sensor.read() {
println!("Humidity: {:?}, Temperature: {:?}", reading.humidity, reading.temperature);
}
}
}