Skip to content

Commit b87df0c

Browse files
authored
add publish flag to SensorData and conditionally process MQTT messages (#17)
* add publish flag to SensorData and update MQTT publishing logic * simplify sensor task logic by always sending sensor data and removing empty sample check * update serde and esp-backtrace dependencies; refactor pump trigger logic in update_task
1 parent 9ed1dee commit b87df0c

File tree

5 files changed

+37
-27
lines changed

5 files changed

+37
-27
lines changed

Cargo.lock

+4-4
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

+4-4
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ version = "0.1.0"
44
edition = "2021"
55

66
[dependencies]
7-
esp-backtrace = { version = "0.15.0", features = [
7+
esp-backtrace = { version = "0.15.1", features = [
88
"esp32s3",
99
"exception-handler",
1010
"panic-handler",
@@ -36,12 +36,12 @@ rust-mqtt = { version = "0.3.0", default-features = false, features = [] }
3636
embedded-text = "0.7.2"
3737
embedded-graphics = { version = "0.8.1", features = [] }
3838
embedded-hal = { version = "1.0.0", features = [] }
39-
serde = { version = "1.0.215", default-features = false, features = ["derive"] }
40-
serde_json = { version = "1.0.133", default-features = false, features = [
39+
serde = { version = "1.0.219", default-features = false, features = ["derive"] }
40+
serde_json = { version = "1.0.140", default-features = false, features = [
4141
"alloc",
4242
] }
4343
embassy-futures = { version = "0.1.1", features = [] }
44-
embassy-sync = { version = "0.6.0", features = [] }
44+
embassy-sync = { version = "0.6.2", features = [] }
4545
static_cell = "2.1.0"
4646
nb = { version = "1.1.0", features = [] }
4747

src/domain.rs

+1
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ const MOISTURE_DRY_THRESHOLD: f32 = 0.15;
1717
#[derive(Default, Debug)]
1818
pub struct SensorData {
1919
pub data: Vec<Sensor, 7>,
20+
pub publish: bool,
2021
}
2122

2223
impl Display for SensorData {

src/sensors_task.rs

+4-10
Original file line numberDiff line numberDiff line change
@@ -199,18 +199,12 @@ pub async fn sensor_task(
199199
.data
200200
.push(Sensor::BatteryVoltage(avg_battery_voltage))
201201
.expect("Too many samples");
202-
} else {
203-
println!("Error measuring battery voltage");
204202
}
205203

206-
if battery_voltage_samples.is_empty() {
207-
println!(
208-
"No battery voltage samples collected - skipping this cycle {:?}",
209-
&sensor_data
210-
);
211-
} else {
212-
sender.send(sensor_data).await;
213-
}
204+
// no battery samples - no publish!
205+
sensor_data.publish = !battery_voltage_samples.is_empty();
206+
207+
sender.send(sensor_data).await;
214208

215209
// Power off the sensors
216210
moisture_power_pin.set_low();

src/update_task.rs

+24-9
Original file line numberDiff line numberDiff line change
@@ -146,6 +146,20 @@ async fn handle_sensor_data(
146146
client: &mut MqttClientImpl<'_>,
147147
display: &mut Display<'static, Delay>,
148148
sensor_data: SensorData,
149+
) -> Result<(), Error> {
150+
if sensor_data.publish {
151+
process_mqtt(client, &sensor_data).await?;
152+
} else {
153+
println!("skipping publishing to MQTT");
154+
}
155+
156+
process_display(display, &sensor_data).await?;
157+
Ok(())
158+
}
159+
160+
async fn process_mqtt(
161+
client: &mut MqttClientImpl<'_>,
162+
sensor_data: &SensorData,
149163
) -> Result<(), Error> {
150164
let discovery_messages_sent = unsafe { DISCOVERY_MESSAGES_SENT };
151165
if !discovery_messages_sent {
@@ -186,11 +200,9 @@ async fn handle_sensor_data(
186200
println!("Water level is full, stopping pump");
187201
update_pump_state(false);
188202
} else if let Sensor::PumpTrigger(enabled) = entry {
189-
// Pump trigger is enabled, start the pump
190-
if *enabled {
191-
println!("Soil moisture is low, starting pump");
192-
update_pump_state(true);
193-
}
203+
let enabled = *enabled;
204+
println!("Pump trigger value: {} - updating pump state", enabled);
205+
update_pump_state(enabled);
194206
}
195207
});
196208

@@ -216,8 +228,6 @@ async fn handle_sensor_data(
216228
.await?;
217229
}
218230

219-
display.write_multiline(&format!("{}", sensor_data))?;
220-
221231
let pump_topic = format!("{}/pump/state", DEVICE_ID);
222232
let message = "OFF";
223233
println!(
@@ -234,11 +244,16 @@ async fn handle_sensor_data(
234244
false,
235245
)
236246
.await?;
247+
Ok(())
248+
}
237249

250+
async fn process_display(
251+
display: &mut Display<'static, Delay>,
252+
sensor_data: &SensorData,
253+
) -> Result<(), Error> {
254+
display.write_multiline(&format!("{}", sensor_data))?;
238255
Timer::after(Duration::from_secs(AWAKE_DURATION_SECONDS)).await;
239-
240256
display.enable_powersave()?;
241-
242257
Ok(())
243258
}
244259

0 commit comments

Comments
 (0)