Nareshkumar Rao 7 months ago
parent
commit
76d30c3f78
  1. 10
      growpi.toml
  2. 13
      src/cli_mode.rs
  3. 8
      src/config.rs
  4. 0
      src/control.rs
  5. 10
      src/sensors.rs

10
growpi.toml

@ -5,7 +5,12 @@ logic_level = 3.299999952316284
light_pin = 0 light_pin = 0
fan_pin = 1 fan_pin = 1
water_pump_pin = 2 water_pump_pin = 2
relay_gpio_pins = [17, 27, 22, -1]
relay_gpio_pins = [
17,
27,
22,
-1,
]
[soil_moisture_settings] [soil_moisture_settings]
pin = 1 pin = 1
@ -19,6 +24,7 @@ voltage_divider_resistance = 9700.0
nominal_resistance = 10000.0 nominal_resistance = 10000.0
nominal_temperature = 298.1499938964844 nominal_temperature = 298.1499938964844
thermal_constant = 3950.0 thermal_constant = 3950.0
resistor = "R2"
[water_pump_settings] [water_pump_settings]
milliseconds_to_grams = 0.06914
grams_per_millisecond = 0.05280999839305878

13
src/cli_mode.rs

@ -67,7 +67,7 @@ fn command_temp(args: &[&str], config: &Configuration) -> GenericResult<()> {
loop { loop {
let temperature = sensors::get_temperature(config)?; let temperature = sensors::get_temperature(config)?;
println!("Temperature: {}C", temperature); println!("Temperature: {}C", temperature);
if show_loop {
if !show_loop {
break; break;
} }
thread::sleep(Duration::from_secs(1)); thread::sleep(Duration::from_secs(1));
@ -84,7 +84,7 @@ fn command_soil(args: &[&str], config: &Configuration) -> GenericResult<()> {
loop { loop {
let humidity = sensors::get_soil_moisture(config)?; let humidity = sensors::get_soil_moisture(config)?;
println!("Soil humidity: {}", humidity); println!("Soil humidity: {}", humidity);
if show_loop {
if !show_loop {
break; break;
} }
thread::sleep(Duration::from_secs(1)); thread::sleep(Duration::from_secs(1));
@ -143,7 +143,7 @@ fn command_ana(args: &[&str]) -> GenericResult<()> {
loop { loop {
let voltage = get_input_voltage(pin)?; let voltage = get_input_voltage(pin)?;
println!("Voltage read: {}", voltage); println!("Voltage read: {}", voltage);
if show_loop {
if !show_loop {
break; break;
} }
thread::sleep(Duration::from_secs(1)); thread::sleep(Duration::from_secs(1));
@ -188,8 +188,11 @@ fn init_state(config: &Configuration) -> GenericResult<ProgramState> {
pub fn run_cli() { pub fn run_cli() {
let mut rl = init_readline().unwrap(); 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(); let mut program_state = init_state(&config).unwrap();

8
src/config.rs

@ -17,6 +17,13 @@ pub struct ThermistorSettings {
pub nominal_resistance: f32, pub nominal_resistance: f32,
pub nominal_temperature: f32, pub nominal_temperature: f32,
pub thermal_constant: f32, pub thermal_constant: f32,
pub resistor: VoltageDividerResistor,
}
#[derive(Serialize, Deserialize)]
pub enum VoltageDividerResistor {
R1,
R2,
} }
#[derive(Serialize, Deserialize)] #[derive(Serialize, Deserialize)]
@ -81,6 +88,7 @@ impl Default for Configuration {
nominal_resistance: 10_000., nominal_resistance: 10_000.,
nominal_temperature: 298.15, nominal_temperature: 298.15,
thermal_constant: 3950., thermal_constant: 3950.,
resistor: VoltageDividerResistor::R2,
}, },
water_pump_settings: WaterPumpSettings { water_pump_settings: WaterPumpSettings {
grams_per_millisecond: 0.05281, grams_per_millisecond: 0.05281,

0
src/control.rs

10
src/sensors.rs

@ -2,8 +2,14 @@ use crate::{config::*, error::GenericResult, io::get_input_voltage};
pub fn get_temperature(config: &Configuration) -> GenericResult<f32> { pub fn get_temperature(config: &Configuration) -> GenericResult<f32> {
let voltage = get_input_voltage(config.thermistor_settings.pin)?; 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. let temperature = 1.
/ ((1. / config.thermistor_settings.nominal_temperature) / ((1. / config.thermistor_settings.nominal_temperature)
+ (1. / config.thermistor_settings.thermal_constant + (1. / config.thermistor_settings.thermal_constant

Loading…
Cancel
Save