diff --git a/rust/.cargo/config.toml b/rust/.cargo/config.toml index 8b89996..e5fe9ad 100644 --- a/rust/.cargo/config.toml +++ b/rust/.cargo/config.toml @@ -4,8 +4,9 @@ target = "xtensa-esp32-espidf" [target.xtensa-esp32-espidf] linker = "ldproxy" #runner = "espflash flash --monitor --partition-table partitions.csv" # Select this runner for espflash v2.x.x -#runner = "espflash flash --monitor --baud 921600 --partition-table partitions.csv" # Select this runner for espflash v2.x.x -runner = "cargo runner" +# runner = espflash erase-parts otadata +runner = "espflash flash --monitor --baud 921600 --partition-table partitions.csv" # Select this runner for espflash v2.x.x +#runner = "cargo runner" rustflags = [ "--cfg", "espidf_time64"] # Extending time_t for ESP IDF 5: https://github.com/esp-rs/rust/issues/110 [unstable] @@ -16,4 +17,4 @@ MCU="esp32" # Note: this variable is not used by the pio builder (`cargo build --features pio`) ESP_IDF_VERSION = "v5.1.1" CHRONO_TZ_TIMEZONE_FILTER="UTC|Europe/Berlin" -CARGO_WORKSPACE_DIR = { value = "", relative = true } \ No newline at end of file +CARGO_WORKSPACE_DIR = { value = "", relative = true } 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 239d722..40022e7 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()); @@ -690,7 +700,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 @@ -702,12 +712,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)