Skip to content

Commit

Permalink
Refactor sensor task to improve DHT11 and moisture pin handling
Browse files Browse the repository at this point in the history
  • Loading branch information
psytraxx committed Jan 20, 2025
1 parent d2f251e commit 3a81d1e
Showing 1 changed file with 10 additions and 9 deletions.
19 changes: 10 additions & 9 deletions src/sensors_task.rs
Original file line number Diff line number Diff line change
Expand Up @@ -38,16 +38,12 @@ pub struct SensorPeripherals {
#[embassy_executor::task]
pub async fn sensor_task(
sender: Sender<'static, NoopRawMutex, SensorData, 3>,
p: SensorPeripherals,
mut p: SensorPeripherals,
) {
info!("Create");

let dht11_pin = OutputOpenDrain::new(p.dht11_pin, Level::High, Pull::None);

let delay = Delay::new();

let mut dht11_sensor = Dht11::new(dht11_pin, delay);

let mut adc2_config = AdcConfig::new();
let mut moisture_pin = adc2_config
.enable_pin_with_cal::<_, AdcCalCurve<ADC2>>(p.moisture_analog_pin, Attenuation::_11dB);
Expand All @@ -59,8 +55,6 @@ pub async fn sensor_task(
.enable_pin_with_cal::<GpioPin<4>, AdcCalLine<ADC1>>(p.battery_pin, Attenuation::_11dB);
let mut adc1 = Adc::new(p.adc1, adc1_config);

let moiture_input_pin = Input::new(p.moisture_digital_pin, esp_hal::gpio::Pull::None);

loop {
// Collect samples for each sensor type
let mut air_humidity_samples: vec::Vec<u8> = vec![];
Expand All @@ -70,6 +64,11 @@ pub async fn sensor_task(
let mut battery_voltage_samples: vec::Vec<u16> = vec![];
let mut water_level_samples: vec::Vec<u16> = vec![];

let dht11_pin = OutputOpenDrain::new(&mut p.dht11_pin, Level::High, Pull::None);
let mut dht11_sensor = Dht11::new(dht11_pin, delay);

let moisture_input_pin = Input::new(&mut p.moisture_digital_pin, esp_hal::gpio::Pull::None);

for i in 0..SENSOR_SAMPLE_COUNT {
info!("Reading sensor data {}/{}", (i + 1), SENSOR_SAMPLE_COUNT);

Expand All @@ -78,6 +77,8 @@ pub async fn sensor_task(
if let Ok(result) = dht11_sensor.read() {
air_temperature_samples.push(result.temperature);
air_humidity_samples.push(result.humidity);
} else {
warn!("Error reading DHT11 sensor");
}

if let Some(result) = sample_adc(&mut adc2, &mut moisture_pin).await {
Expand All @@ -86,7 +87,7 @@ pub async fn sensor_task(
warn!("Error reading soil moisture sensor");
}

if moiture_input_pin.is_high() {
if moisture_input_pin.is_high() {
moisture_pin_sample_count += 1;
}

Expand Down Expand Up @@ -159,7 +160,7 @@ pub async fn sensor_task(

sender.send(sensor_data).await;

let sampling_period = Duration::from_secs(AWAKE_DURATION_SECONDS);
let sampling_period = Duration::from_secs(AWAKE_DURATION_SECONDS / 2);
Timer::after(sampling_period).await;
}
}
Expand Down

0 comments on commit 3a81d1e

Please sign in to comment.