Skip to content

Commit

Permalink
refactor sensor_task to ensure proper resource management and low-pow…
Browse files Browse the repository at this point in the history
…er state for DHT11 sensor
  • Loading branch information
psytraxx committed Mar 2, 2025
1 parent d698634 commit 5209f40
Showing 1 changed file with 15 additions and 9 deletions.
24 changes: 15 additions & 9 deletions src/sensors_task.rs
Original file line number Diff line number Diff line change
Expand Up @@ -78,15 +78,19 @@ pub async fn sensor_task(
for i in 0..SENSOR_SAMPLE_COUNT {
info!("Reading sensor data {}/{}", (i + 1), SENSOR_SAMPLE_COUNT);

let dht11_pin = OutputOpenDrain::new(&mut p.dht11_digital_pin, Level::High, Pull::None);
let mut dht11_sensor: Dht11<OutputOpenDrain<'_>, Delay> = Dht11::new(dht11_pin, delay);

// DHT11 needs a longer initial delay
Timer::after(Duration::from_millis(DHT11_WARMUP_DELAY_MILLISECONDS)).await;
if let Ok(result) = dht11_sensor.read() {
air_temperature_samples.push(result.temperature);
air_humidity_samples.push(result.humidity);
}
{
let dht11_pin =
OutputOpenDrain::new(&mut p.dht11_digital_pin, Level::High, Pull::None);
let mut dht11_sensor: Dht11<OutputOpenDrain<'_>, Delay> =
Dht11::new(dht11_pin, delay);

// DHT11 needs a longer initial delay
Timer::after(Duration::from_millis(DHT11_WARMUP_DELAY_MILLISECONDS)).await;
if let Ok(result) = dht11_sensor.read() {
air_temperature_samples.push(result.temperature);
air_humidity_samples.push(result.humidity);
}
} // drop dht11_pin

if let Some(result) = sample_adc(&mut adc2, &mut moisture_pin).await {
soil_moisture_samples.push(result);
Expand Down Expand Up @@ -174,6 +178,8 @@ pub async fn sensor_task(
// Power off the sensors
moisture_power_pin.set_low();
water_level_power_pin.set_low();
// Force the pin into an explicit low-power state after the sensor is dropped
Output::new(&mut p.dht11_digital_pin, Level::Low);

Timer::after(sampling_period).await;
}
Expand Down

0 comments on commit 5209f40

Please sign in to comment.