dht22-driver 0.1.1

No-std, no-dependency, platform-agnostic driver for the dht22 sensor
Documentation
  • Coverage
  • 56.52%
    13 out of 23 items documented1 out of 14 items with examples
  • Size
  • Source code size: 24.03 kB This is the summed size of all the files inside the crates.io package for this release.
  • Documentation size: 1.78 MB This is the summed size of all files generated by rustdoc for all configured targets
  • Ø build duration
  • this release: 10s Average build duration of successful builds.
  • all releases: 10s Average build duration of successful builds in releases after 2024-10-23.
  • Links
  • Tastaturtaste/dht22-driver
    0 0 1
  • crates.io
  • Dependencies
  • Versions
  • Owners
  • Tastaturtaste

DHT22

No-std, no-dependency, platform-agnostic driver for the dht22 sensor

Features

  • critical-section is used by default to prevent interrupts from messing with the timing-based data transfer protocol of the sensor. While it is recommended to use if possible, the driver may still work reliably if interrupts are disabled another way. Otherwise the chance for additional spurious invalid reads due to timing mismatches may be acceptable, as spurious failurs should be handled anyway.
  • 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.

Example usage

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);    
        }
    }
}