diff --git a/growpi.toml b/growpi.toml index ddc8e4f..676be69 100644 --- a/growpi.toml +++ b/growpi.toml @@ -5,7 +5,12 @@ logic_level = 3.299999952316284 light_pin = 0 fan_pin = 1 water_pump_pin = 2 -relay_gpio_pins = [17, 27, 22, -1] +relay_gpio_pins = [ + 17, + 27, + 22, + -1, +] [soil_moisture_settings] pin = 1 @@ -19,6 +24,7 @@ voltage_divider_resistance = 9700.0 nominal_resistance = 10000.0 nominal_temperature = 298.1499938964844 thermal_constant = 3950.0 +resistor = "R2" [water_pump_settings] -milliseconds_to_grams = 0.06914 +grams_per_millisecond = 0.05280999839305878 diff --git a/src/cli_mode.rs b/src/cli_mode.rs index 82d1207..08c179d 100644 --- a/src/cli_mode.rs +++ b/src/cli_mode.rs @@ -67,7 +67,7 @@ fn command_temp(args: &[&str], config: &Configuration) -> GenericResult<()> { loop { let temperature = sensors::get_temperature(config)?; println!("Temperature: {}C", temperature); - if show_loop { + if !show_loop { break; } thread::sleep(Duration::from_secs(1)); @@ -84,7 +84,7 @@ fn command_soil(args: &[&str], config: &Configuration) -> GenericResult<()> { loop { let humidity = sensors::get_soil_moisture(config)?; println!("Soil humidity: {}", humidity); - if show_loop { + if !show_loop { break; } thread::sleep(Duration::from_secs(1)); @@ -143,7 +143,7 @@ fn command_ana(args: &[&str]) -> GenericResult<()> { loop { let voltage = get_input_voltage(pin)?; println!("Voltage read: {}", voltage); - if show_loop { + if !show_loop { break; } thread::sleep(Duration::from_secs(1)); @@ -188,8 +188,11 @@ fn init_state(config: &Configuration) -> GenericResult { pub fn run_cli() { let mut rl = init_readline().unwrap(); - let config = - Configuration::from_file(std::path::Path::new("./growpi.toml")).unwrap_or_default(); + let config = Configuration::from_file(std::path::Path::new("./growpi.toml")); + if let Err(config) = &config { + println!("Could not load config: {}", config); + } + let config = config.unwrap_or_default(); let mut program_state = init_state(&config).unwrap(); diff --git a/src/config.rs b/src/config.rs index 4ba95fb..ad2a9ea 100644 --- a/src/config.rs +++ b/src/config.rs @@ -17,6 +17,13 @@ pub struct ThermistorSettings { pub nominal_resistance: f32, pub nominal_temperature: f32, pub thermal_constant: f32, + pub resistor: VoltageDividerResistor, +} + +#[derive(Serialize, Deserialize)] +pub enum VoltageDividerResistor { + R1, + R2, } #[derive(Serialize, Deserialize)] @@ -81,6 +88,7 @@ impl Default for Configuration { nominal_resistance: 10_000., nominal_temperature: 298.15, thermal_constant: 3950., + resistor: VoltageDividerResistor::R2, }, water_pump_settings: WaterPumpSettings { grams_per_millisecond: 0.05281, diff --git a/src/control.rs b/src/control.rs new file mode 100644 index 0000000..e69de29 diff --git a/src/sensors.rs b/src/sensors.rs index f8df28d..be1c6dc 100644 --- a/src/sensors.rs +++ b/src/sensors.rs @@ -2,8 +2,14 @@ use crate::{config::*, error::GenericResult, io::get_input_voltage}; pub fn get_temperature(config: &Configuration) -> GenericResult { let voltage = get_input_voltage(config.thermistor_settings.pin)?; - let resistance = (config.board_settings.logic_level / voltage - 1.) - * config.thermistor_settings.voltage_divider_resistance; + + let k = config.board_settings.logic_level / voltage - 1.; + let k = match config.thermistor_settings.resistor { + VoltageDividerResistor::R1 => k, + VoltageDividerResistor::R2 => 1. / k, + }; + let resistance = k * config.thermistor_settings.voltage_divider_resistance; + let temperature = 1. / ((1. / config.thermistor_settings.nominal_temperature) + (1. / config.thermistor_settings.thermal_constant