Dht11Sensor

Struct Dht11Sensor 

Source
pub struct Dht11Sensor { /* private fields */ }
Expand description

DHT11 temperature and humidity sensor implementation

Implementations§

Source§

impl Dht11Sensor

Source

pub fn new(pin: u8) -> Self

Create a new DHT11 sensor instance

§Arguments
  • pin - GPIO pin number connected to the DHT11 sensor
§Example
use env_monitor::sensors::dht11::Dht11Sensor;

let sensor = Dht11Sensor::new(17);
Examples found in repository?
src/examples/env_monitor_example.rs (line 13)
8async fn main() -> Result<(), Box<dyn Error + Send + Sync>> {
9    println!("环境监控系统启动");
10    println!("- DHT11温湿度传感器: GPIO17");
11    println!("- 火焰传感器: GPIO27, 蜂鸣器: GPIO22");
12
13    let dht_sensor = Dht11Sensor::new(17);
14    let fire_sensor = FireSensor::new(27, 22, true);
15
16    fire_sensor.start_monitoring(100).await?;
17
18    loop {
19        match dht_sensor.read_async().await {
20            Ok(data) => {
21                println!(
22                    "温度: {:.1}°C, 湿度: {:.1}%",
23                    data.temperature, data.humidity
24                );
25
26                if data.temperature > 40.0 {
27                    println!("警告: 温度过高! ({:.1}°C)", data.temperature);
28                }
29            }
30            Err(e) => {
31                println!("DHT11读取失败: {}", e);
32            }
33        }
34
35        time::sleep(Duration::from_secs(5)).await;
36    }
37}

Trait Implementations§

Source§

impl TemperatureSensor for Dht11Sensor

Source§

fn read(&self) -> Result<Dht11Data, SensorError>

Synchronously read temperature and humidity data

§Returns

Temperature and humidity data or error

§Example
use env_monitor::sensors::TemperatureSensor;
use env_monitor::sensors::dht11::Dht11Sensor;

let sensor = Dht11Sensor::new(17);
match sensor.read() {
    Ok(data) => println!("Temperature: {}°C, Humidity: {}%", data.temperature, data.humidity),
    Err(e) => println!("Read failed: {}", e),
}
Source§

fn read_async<'life0, 'async_trait>( &'life0 self, ) -> Pin<Box<dyn Future<Output = Result<Dht11Data, SensorError>> + Send + 'async_trait>>
where Self: 'async_trait, 'life0: 'async_trait,

Asynchronously read temperature and humidity data

§Returns

Temperature and humidity data or error

§Example
use env_monitor::sensors::TemperatureSensor;
use env_monitor::sensors::dht11::Dht11Sensor;

#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
    let sensor = Dht11Sensor::new(17);
    match sensor.read_async().await {
        Ok(data) => println!("Temperature: {}°C, Humidity: {}%", data.temperature, data.humidity),
        Err(e) => println!("Read failed: {}", e),
    }
    Ok(())
}

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.