pub struct Dht11Sensor { /* private fields */ }Expand description
DHT11 temperature and humidity sensor implementation
Implementations§
Source§impl Dht11Sensor
impl Dht11Sensor
Sourcepub fn new(pin: u8) -> Self
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
impl TemperatureSensor for Dht11Sensor
Source§fn read(&self) -> Result<Dht11Data, SensorError>
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,
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§
impl Freeze for Dht11Sensor
impl RefUnwindSafe for Dht11Sensor
impl Send for Dht11Sensor
impl Sync for Dht11Sensor
impl Unpin for Dht11Sensor
impl UnwindSafe for Dht11Sensor
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more