From 869a581242516722c363d0749717b100043ce13f Mon Sep 17 00:00:00 2001 From: Empire Date: Sat, 2 Mar 2024 14:43:52 +0100 Subject: [PATCH] delay messages to reduce mqtt issues (sender to fast) improve mqtt debugging --- rust/src/main.rs | 50 +++++++++++++++++++++++++------------------ rust/src/plant_hal.rs | 9 +++++--- 2 files changed, 35 insertions(+), 24 deletions(-) diff --git a/rust/src/main.rs b/rust/src/main.rs index e6a05f4..58419dc 100644 --- a/rust/src/main.rs +++ b/rust/src/main.rs @@ -325,32 +325,44 @@ fn safe_main() -> anyhow::Result<()> { let mut water_frozen = false; + let mut temp:Option = None; for _attempt in 0..5 { let water_temperature = board.water_temperature_c(); match water_temperature { - Ok(temp) => { - if online_mode == OnlineMode::Online { - let _ = board.mqtt_publish( - &config, - "/water/temperature", - temp.to_string().as_bytes(), - ); - } - //FIXME mqtt here - println!("Water temp is {}", temp); - if temp < 4_f32 { - water_frozen = true; - } + Ok(res) => { + temp = Some(res); break; } Err(err) => { - if online_mode == OnlineMode::Online { - let _ = board.mqtt_publish(&config, "/water/temperature", "Error".as_bytes()); - } - println!("Could not get water temp {}", err) + println!("Could not get water temp {} attempt {}", err, _attempt) } } } + match temp { + Some(res) => { + println!("Water temp is {}", res); + if res < 4_f32 { + water_frozen = true; + } + if online_mode == OnlineMode::Online { + let _ = board.mqtt_publish( + &config, + "/water/temperature", + res.to_string().as_bytes(), + ); + } + }, + None => { + if online_mode == OnlineMode::Online { + let _ = board.mqtt_publish(&config, "/water/temperature", "Error".as_bytes()); + } + }, + } + + + + + let mut plantstate = [PlantState { ..Default::default() @@ -560,7 +572,6 @@ fn safe_main() -> anyhow::Result<()> { //is light out of work trigger soon? //is battery low ?? //is deep sleep - mark_app_valid(); unsafe { esp_deep_sleep(1000 * 1000 * 60 * deep_sleep_duration_minutes as u64) }; @@ -953,9 +964,6 @@ fn determine_next_plant( } println!("Plant {} state is {:?}", plant, state); } - if online_mode == OnlineMode::Online { - update_plant_state(plantstate, board, config); - } for plant in 0..PLANT_COUNT { let state = &plantstate[plant]; println!( diff --git a/rust/src/plant_hal.rs b/rust/src/plant_hal.rs index efbd087..5e51eac 100644 --- a/rust/src/plant_hal.rs +++ b/rust/src/plant_hal.rs @@ -723,11 +723,14 @@ impl PlantCtrlBoardInteraction for PlantCtrlBoard<'_> { true, message, ); - + Delay::new(10).delay_ms(10); match publish { - OkStd(_) => return Ok(()), + OkStd(message_id) => { + println!("Published mqtt topic {} with message {:#?} msgid is {:?}",full_topic, String::from_utf8_lossy(message), message_id); + return Ok(()) + }, Err(err) => { - println!("Error during mqtt send on topic {} with message {:#?} error is {:?}",full_topic, message, err); + println!("Error during mqtt send on topic {} with message {:#?} error is {:?}",full_topic, String::from_utf8_lossy(message), err); return Err(err)? }, }