Skip to content

Commit 513cce0

Browse files
committed
add publish flag to SensorData and conditionally process MQTT messages
1 parent 6e33e34 commit 513cce0

File tree

3 files changed

+32
-14
lines changed

3 files changed

+32
-14
lines changed

src/domain.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ const MOISTURE_DRY_THRESHOLD: f32 = 0.15;
2121
#[derive(Default, Debug)]
2222
pub struct SensorData {
2323
pub data: Vec<Sensor>,
24+
pub publish: bool,
2425
}
2526

2627
impl Display for SensorData {

src/sensors_task.rs

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -171,13 +171,16 @@ pub async fn sensor_task(
171171
warn!("Unable to generate average value of soil moisture");
172172
}
173173

174-
if let Some(avg) = calculate_average(&mut battery_voltage_samples) {
175-
info!("Battery voltage: {}mV", avg);
176-
sensor_data.data.push(Sensor::BatteryVoltage(avg));
177-
} else {
178-
warn!("Error measuring battery voltage");
174+
if let Some(avg_battery_voltage) = calculate_average(&mut battery_voltage_samples) {
175+
info!("Battery voltage: {}mV", avg_battery_voltage);
176+
sensor_data
177+
.data
178+
.push(Sensor::BatteryVoltage(avg_battery_voltage));
179179
}
180180

181+
// no battery samples - no publish!
182+
sensor_data.publish = !battery_voltage_samples.is_empty();
183+
181184
sender.send(sensor_data).await;
182185

183186
// Power off the sensors

src/update_task.rs

Lines changed: 23 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ use alloc::{
33
string::{String, ToString},
44
};
55
use core::{num::ParseIntError, str};
6-
use defmt::{error, info};
6+
use defmt::{error, info, warn};
77
use embassy_futures::select::{select, Either};
88
use embassy_net::{
99
dns::{DnsQueryType, Error as DnsError},
@@ -146,6 +146,20 @@ async fn handle_sensor_data(
146146
client: &mut MqttClientImpl<'_>,
147147
display: &mut Display<'static>,
148148
sensor_data: SensorData,
149+
) -> Result<(), Error> {
150+
if sensor_data.publish {
151+
process_mqtt(client, &sensor_data).await?;
152+
} else {
153+
warn!("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 {
@@ -179,14 +193,12 @@ async fn handle_sensor_data(
179193
info!("Discovery messages already sent");
180194
}
181195

182-
// act on sensor data - turn pump on/off
196+
// Act on sensor data - turn pump on/off
183197
sensor_data.data.iter().for_each(|entry| {
184198
if let Sensor::WaterLevel(WaterLevel::Full) = entry {
185-
// Water level is full, stop the pump in any case if it's running
186199
info!("Water level is full, stopping pump");
187200
ENABLE_PUMP.signal(false);
188201
} else if let Sensor::PumpTrigger(enabled) = entry {
189-
// Pump trigger is enabled, start the pump
190202
if *enabled {
191203
info!("Soil moisture is low, starting pump");
192204
ENABLE_PUMP.signal(true);
@@ -199,7 +211,6 @@ async fn handle_sensor_data(
199211
let value = s.value();
200212
let message = json!({ "value": value }).to_string();
201213
let topic_name = format!("{}/{}", DEVICE_ID, key);
202-
203214
info!(
204215
"Publishing to topic {}, message: {}",
205216
topic_name.as_str(),
@@ -216,8 +227,6 @@ async fn handle_sensor_data(
216227
.await?;
217228
}
218229

219-
display.write_multiline(&format!("{}", sensor_data))?;
220-
221230
let pump_topic = format!("{}/pump/state", DEVICE_ID);
222231
let message = "OFF";
223232
info!(
@@ -234,11 +243,16 @@ async fn handle_sensor_data(
234243
false,
235244
)
236245
.await?;
246+
Ok(())
247+
}
237248

249+
async fn process_display(
250+
display: &mut Display<'static>,
251+
sensor_data: &SensorData,
252+
) -> Result<(), Error> {
253+
display.write_multiline(&format!("{}", sensor_data))?;
238254
Timer::after(Duration::from_secs(AWAKE_DURATION_SECONDS)).await;
239-
240255
display.enable_powersave()?;
241-
242256
Ok(())
243257
}
244258

0 commit comments

Comments
 (0)