From 27bde9cc87099915627be47cb8fe0f5c2b9d0421 Mon Sep 17 00:00:00 2001 From: Empire Date: Wed, 21 Feb 2024 22:30:03 +0100 Subject: [PATCH] fix tank calculation, update bq34 driver --- rust/Cargo.toml | 2 +- rust/build.rs | 2 +- rust/src/main.rs | 20 +++++++++++++++----- rust/src/plant_hal.rs | 8 +++++--- rust/src/webserver/config.html | 2 +- 5 files changed, 23 insertions(+), 11 deletions(-) diff --git a/rust/Cargo.toml b/rust/Cargo.toml index fe1fa84..8024b0c 100644 --- a/rust/Cargo.toml +++ b/rust/Cargo.toml @@ -76,7 +76,7 @@ serde_json = "1.0.108" strum = { version = "0.26.1", features = ["derive"] } once_cell = "1.19.0" measurements = "0.11.0" -bq34z100 = "0.1.0" +bq34z100 = "0.2.1" [build-dependencies] embuild = "0.31.3" diff --git a/rust/build.rs b/rust/build.rs index ea2d340..b0169e5 100644 --- a/rust/build.rs +++ b/rust/build.rs @@ -25,7 +25,7 @@ fn main() { } Err(_) => { println!("Assuming build on linux"); - let output = Command::new("bash") + let output = Command::new("npx") .arg("webpack") .current_dir("./src_webpack") .output() diff --git a/rust/src/main.rs b/rust/src/main.rs index d9a52cb..a1f11a8 100644 --- a/rust/src/main.rs +++ b/rust/src/main.rs @@ -236,8 +236,10 @@ fn safe_main() -> anyhow::Result<()> { }; println!("attempting to connect wifi"); + let mut ip_address: Option = None; match board.wifi(wifi.ssid, wifi.password, 10000) { - Ok(_) => { + Ok(ip_info) => { + ip_address = Some(ip_info.ip.to_string()); online_mode = OnlineMode::Wifi; } Err(_) => { @@ -292,6 +294,14 @@ fn safe_main() -> anyhow::Result<()> { } if online_mode == OnlineMode::Online { + match ip_address { + Some(add_some) => { + let _ = board.mqtt_publish(&config, "/firmware/address", add_some.as_bytes()); + }, + None => { + let _ = board.mqtt_publish(&config, "/firmware/address", "N/A?".as_bytes()); + }, + } let _ = board.mqtt_publish(&config, "/firmware/githash", git_hash.as_bytes()); let _ = board.mqtt_publish(&config, "/firmware/buildtime", build_timestamp.as_bytes()); let _ = board.mqtt_publish(&config, "/firmware/last_online", europe_time.to_rfc3339().as_bytes()); @@ -679,7 +689,7 @@ fn determine_tank_state( ); }) .and_then(|percent| { - rv.left_ml = (percent * config.tank_useable_ml as f32) as u32; + rv.left_ml = ((percent * config.tank_useable_ml as f32) / 100_f32) as u32; println!( "Tank sensor returned mv {} as {}% leaving {} ml useable", rv.raw, percent as u8, rv.left_ml @@ -691,12 +701,12 @@ fn determine_tank_state( percent as u8, config.tank_warn_percent ); } - if config.tank_empty_percent > percent as u8 { + if config.tank_empty_percent < percent as u8 { println!( - "Empty water, current percent is {}, minimum empty level is {}", + "Enough water, current percent is {}, minimum empty level is {}", percent as u8, config.tank_empty_percent ); - rv.enough_water = false; + rv.enough_water = true; } return Ok(()); }); diff --git a/rust/src/plant_hal.rs b/rust/src/plant_hal.rs index 2f34ec6..b404bf4 100644 --- a/rust/src/plant_hal.rs +++ b/rust/src/plant_hal.rs @@ -7,6 +7,7 @@ use embedded_svc::wifi::{ use esp_idf_hal::i2c::{I2cConfig, I2cDriver, I2cError}; use esp_idf_hal::units::FromValueType; use esp_idf_svc::eventloop::EspSystemEventLoop; +use esp_idf_svc::ipv4::IpInfo; use esp_idf_svc::mqtt::client::QoS::AtLeastOnce; use esp_idf_svc::mqtt::client::QoS::ExactlyOnce; use esp_idf_svc::mqtt::client::{EspMqttClient, LwtConfiguration, MqttClientConfiguration}; @@ -94,7 +95,7 @@ pub trait PlantCtrlBoardInteraction { ssid: heapless::String<32>, password: Option>, max_wait: u32, - ) -> Result<()>; + ) -> Result; fn sntp(&mut self, max_wait: u32) -> Result>; fn mount_file_system(&mut self) -> Result<()>; fn file_system_size(&mut self) -> Result; @@ -393,7 +394,7 @@ impl PlantCtrlBoardInteraction for PlantCtrlBoard<'_> { ssid: heapless::String<32>, password: Option>, max_wait: u32, - ) -> Result<()> { + ) -> Result { match password { Some(pw) => { //TODO expect error due to invalid pw or similar! //call this during configuration and check if works, revert to config mode if not @@ -448,7 +449,7 @@ impl PlantCtrlBoardInteraction for PlantCtrlBoard<'_> { //update freertos registers ;) let address = self.wifi_driver.sta_netif().get_ip_info().unwrap(); println!("IP info: {:?}", address); - Ok(()) + Ok(address) } fn mount_file_system(&mut self) -> Result<()> { @@ -620,6 +621,7 @@ impl PlantCtrlBoardInteraction for PlantCtrlBoard<'_> { qos: AtLeastOnce, retain: true, }), + keep_alive_interval : Some(Duration::from_secs(60*60*2)), //room for improvement ..Default::default() }; diff --git a/rust/src/webserver/config.html b/rust/src/webserver/config.html index 0156e59..55bf906 100644 --- a/rust/src/webserver/config.html +++ b/rust/src/webserver/config.html @@ -40,7 +40,7 @@
- Tank Warn below mL + Tank Warn Percent (mapped in relation to empty and full)