Skip to content

add publish flag to SensorData and conditionally process MQTT messages #17

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Apr 22, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 4 additions & 4 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ version = "0.1.0"
edition = "2021"

[dependencies]
esp-backtrace = { version = "0.15.0", features = [
esp-backtrace = { version = "0.15.1", features = [
"esp32s3",
"exception-handler",
"panic-handler",
Expand Down Expand Up @@ -36,12 +36,12 @@ rust-mqtt = { version = "0.3.0", default-features = false, features = [] }
embedded-text = "0.7.2"
embedded-graphics = { version = "0.8.1", features = [] }
embedded-hal = { version = "1.0.0", features = [] }
serde = { version = "1.0.215", default-features = false, features = ["derive"] }
serde_json = { version = "1.0.133", default-features = false, features = [
serde = { version = "1.0.219", default-features = false, features = ["derive"] }
serde_json = { version = "1.0.140", default-features = false, features = [
"alloc",
] }
embassy-futures = { version = "0.1.1", features = [] }
embassy-sync = { version = "0.6.0", features = [] }
embassy-sync = { version = "0.6.2", features = [] }
static_cell = "2.1.0"
nb = { version = "1.1.0", features = [] }

Expand Down
1 change: 1 addition & 0 deletions src/domain.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ const MOISTURE_DRY_THRESHOLD: f32 = 0.15;
#[derive(Default, Debug)]
pub struct SensorData {
pub data: Vec<Sensor, 7>,
pub publish: bool,
}

impl Display for SensorData {
Expand Down
14 changes: 4 additions & 10 deletions src/sensors_task.rs
Original file line number Diff line number Diff line change
Expand Up @@ -199,18 +199,12 @@ pub async fn sensor_task(
.data
.push(Sensor::BatteryVoltage(avg_battery_voltage))
.expect("Too many samples");
} else {
println!("Error measuring battery voltage");
}

if battery_voltage_samples.is_empty() {
println!(
"No battery voltage samples collected - skipping this cycle {:?}",
&sensor_data
);
} else {
sender.send(sensor_data).await;
}
// no battery samples - no publish!
sensor_data.publish = !battery_voltage_samples.is_empty();

sender.send(sensor_data).await;

// Power off the sensors
moisture_power_pin.set_low();
Expand Down
33 changes: 24 additions & 9 deletions src/update_task.rs
Original file line number Diff line number Diff line change
Expand Up @@ -146,6 +146,20 @@ async fn handle_sensor_data(
client: &mut MqttClientImpl<'_>,
display: &mut Display<'static, Delay>,
sensor_data: SensorData,
) -> Result<(), Error> {
if sensor_data.publish {
process_mqtt(client, &sensor_data).await?;
} else {
println!("skipping publishing to MQTT");
}

process_display(display, &sensor_data).await?;
Ok(())
}

async fn process_mqtt(
client: &mut MqttClientImpl<'_>,
sensor_data: &SensorData,
) -> Result<(), Error> {
let discovery_messages_sent = unsafe { DISCOVERY_MESSAGES_SENT };
if !discovery_messages_sent {
Expand Down Expand Up @@ -186,11 +200,9 @@ async fn handle_sensor_data(
println!("Water level is full, stopping pump");
update_pump_state(false);
} else if let Sensor::PumpTrigger(enabled) = entry {
// Pump trigger is enabled, start the pump
if *enabled {
println!("Soil moisture is low, starting pump");
update_pump_state(true);
}
let enabled = *enabled;
println!("Pump trigger value: {} - updating pump state", enabled);
update_pump_state(enabled);
}
});

Expand All @@ -216,8 +228,6 @@ async fn handle_sensor_data(
.await?;
}

display.write_multiline(&format!("{}", sensor_data))?;

let pump_topic = format!("{}/pump/state", DEVICE_ID);
let message = "OFF";
println!(
Expand All @@ -234,11 +244,16 @@ async fn handle_sensor_data(
false,
)
.await?;
Ok(())
}

async fn process_display(
display: &mut Display<'static, Delay>,
sensor_data: &SensorData,
) -> Result<(), Error> {
display.write_multiline(&format!("{}", sensor_data))?;
Timer::after(Duration::from_secs(AWAKE_DURATION_SECONDS)).await;

display.enable_powersave()?;

Ok(())
}

Expand Down