Nareshkumar Rao
7 months ago
7 changed files with 309 additions and 67 deletions
@ -1,13 +1,85 @@ |
|||||
pub const LOGIC_LEVEL: f32 = 3.3; |
|
||||
pub const THERMISTOR_ANALOG_PIN: u8 = 0; |
|
||||
pub const THERMISTOR_VOLTAGE_DIVIDER_RESISTANCE: f32 = 9_700.; |
|
||||
|
|
||||
pub const LIGHT_RELAY_PIN: u8 = 0; |
|
||||
pub const FAN_RELAY_PIN: u8 = 1; |
|
||||
pub const WATER_PUMP_RELAY_PIN: u8 = 2; |
|
||||
pub const RELAY_GPIO_PINS: [Option<u8>; 4] = [Some(17), Some(27), Some(22), None]; |
|
||||
|
|
||||
pub const SOIL_MOISTURE_PIN: u8 = 1; |
|
||||
pub const SOIL_MOISTURE_100_VOLTAGE: f32 = 1.417; |
|
||||
pub const SOIL_NOMINAL_MOISTURE: f32 = 0.41; |
|
||||
pub const SOIL_NOMINAL_MOISTURE_VOLTAGE: f32 = 2.823; |
|
||||
|
use serde::{Deserialize, Serialize}; |
||||
|
|
||||
|
use crate::error::GenericResult; |
||||
|
|
||||
|
#[derive(Serialize, Deserialize)] |
||||
|
pub struct RelaySettings { |
||||
|
pub light_pin: u8, |
||||
|
pub fan_pin: u8, |
||||
|
pub water_pump_pin: u8, |
||||
|
pub relay_gpio_pins: Vec<Option<u8>>, |
||||
|
} |
||||
|
|
||||
|
#[derive(Serialize, Deserialize)] |
||||
|
pub struct ThermistorSettings { |
||||
|
pub pin: u8, |
||||
|
pub voltage_divider_resistance: f32, |
||||
|
pub nominal_resistance: f32, |
||||
|
pub nominal_temperature: f32, |
||||
|
pub thermal_constant: f32, |
||||
|
} |
||||
|
|
||||
|
#[derive(Serialize, Deserialize)] |
||||
|
pub struct SoilMoistureSettings { |
||||
|
pub pin: u8, |
||||
|
pub voltage_100: f32, |
||||
|
pub voltage_nominal: f32, |
||||
|
pub moisture_nominal: f32, |
||||
|
} |
||||
|
|
||||
|
#[derive(Serialize, Deserialize)] |
||||
|
pub struct BoardSettings { |
||||
|
pub logic_level: f32, |
||||
|
} |
||||
|
|
||||
|
#[derive(Serialize, Deserialize)] |
||||
|
pub struct Configuration { |
||||
|
pub board_settings: BoardSettings, |
||||
|
pub relay_settings: RelaySettings, |
||||
|
pub soil_moisture_settings: SoilMoistureSettings, |
||||
|
pub thermistor_settings: ThermistorSettings, |
||||
|
} |
||||
|
|
||||
|
impl Configuration { |
||||
|
fn from_file(path: &std::path::Path) -> GenericResult<Configuration> { |
||||
|
let text = std::fs::read_to_string(path)?; |
||||
|
let config: Configuration = toml::from_str(text.as_str())?; |
||||
|
Ok(config) |
||||
|
} |
||||
|
fn save_to_file(path: &std::path::Path, config: &Configuration) -> GenericResult<()> { |
||||
|
let text = toml::to_string_pretty(config)?; |
||||
|
std::fs::write(path, text)?; |
||||
|
Ok(()) |
||||
|
} |
||||
|
} |
||||
|
|
||||
|
const THERMISTOR_NOMINAL_RESISTANCE: f32 = 10_000.; |
||||
|
const THERMISTOR_NOMINAL_TEMPERATURE: f32 = 298.15; |
||||
|
const THERMISTOR_CONSTANT: f32 = 3950.; |
||||
|
|
||||
|
impl Default for Configuration { |
||||
|
fn default() -> Self { |
||||
|
Self { |
||||
|
board_settings: BoardSettings { logic_level: 3.3 }, |
||||
|
relay_settings: RelaySettings { |
||||
|
light_pin: 0, |
||||
|
fan_pin: 1, |
||||
|
water_pump_pin: 2, |
||||
|
relay_gpio_pins: [Some(17), Some(27), Some(22), None].to_vec(), |
||||
|
}, |
||||
|
soil_moisture_settings: SoilMoistureSettings { |
||||
|
pin: 1, |
||||
|
voltage_100: 1.417, |
||||
|
voltage_nominal: 2.823, |
||||
|
moisture_nominal: 0.41, |
||||
|
}, |
||||
|
thermistor_settings: ThermistorSettings { |
||||
|
pin: 0, |
||||
|
voltage_divider_resistance: 9_700., |
||||
|
nominal_resistance: 10_000., |
||||
|
nominal_temperature: 298.15, |
||||
|
thermal_constant: 3950., |
||||
|
}, |
||||
|
} |
||||
|
} |
||||
|
} |
||||
|
@ -1,27 +1,26 @@ |
|||||
use crate::{config::*, error::GenericResult, io::get_input_voltage}; |
use crate::{config::*, error::GenericResult, io::get_input_voltage}; |
||||
|
|
||||
pub fn get_temperature() -> GenericResult<f32> { |
|
||||
const THERMISTOR_NOMINAL_RESISTANCE: f32 = 10_000.; |
|
||||
const THERMISTOR_NOMINAL_TEMPERATURE: f32 = 298.15; |
|
||||
const THERMISTOR_CONSTANT: f32 = 3950.; |
|
||||
|
|
||||
let voltage = get_input_voltage(THERMISTOR_ANALOG_PIN)?; |
|
||||
let resistance = (LOGIC_LEVEL / voltage - 1.) * THERMISTOR_VOLTAGE_DIVIDER_RESISTANCE; |
|
||||
|
pub fn get_temperature(config: &Configuration) -> GenericResult<f32> { |
||||
|
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 temperature = 1. |
let temperature = 1. |
||||
/ ((1. / THERMISTOR_NOMINAL_TEMPERATURE) |
|
||||
+ (1. / THERMISTOR_CONSTANT * f32::ln(resistance / THERMISTOR_NOMINAL_RESISTANCE))) |
|
||||
|
/ ((1. / config.thermistor_settings.nominal_temperature) |
||||
|
+ (1. / config.thermistor_settings.thermal_constant |
||||
|
* f32::ln(resistance / config.thermistor_settings.nominal_resistance))) |
||||
- 273.15; |
- 273.15; |
||||
Ok(temperature) |
Ok(temperature) |
||||
} |
} |
||||
|
|
||||
pub fn get_soil_moisture() -> GenericResult<f32> { |
|
||||
let voltage = get_input_voltage(SOIL_MOISTURE_PIN)?; |
|
||||
|
pub fn get_soil_moisture(config: &Configuration) -> GenericResult<f32> { |
||||
|
let voltage = get_input_voltage(config.soil_moisture_settings.pin)?; |
||||
|
|
||||
const VOLTAGE_ZERO_HUMIDITY: f32 = (SOIL_NOMINAL_MOISTURE_VOLTAGE |
|
||||
- SOIL_MOISTURE_100_VOLTAGE * SOIL_NOMINAL_MOISTURE) |
|
||||
/ (1. - SOIL_NOMINAL_MOISTURE); |
|
||||
|
let voltage_zero_humidity: f32 = (config.soil_moisture_settings.voltage_nominal |
||||
|
- config.soil_moisture_settings.voltage_100 |
||||
|
* config.soil_moisture_settings.moisture_nominal) |
||||
|
/ (1. - config.soil_moisture_settings.moisture_nominal); |
||||
|
|
||||
let humidity = |
|
||||
(voltage - VOLTAGE_ZERO_HUMIDITY) / (SOIL_MOISTURE_100_VOLTAGE - VOLTAGE_ZERO_HUMIDITY); |
|
||||
|
let humidity = (voltage - voltage_zero_humidity) |
||||
|
/ (config.soil_moisture_settings.voltage_100 - voltage_zero_humidity); |
||||
Ok(humidity) |
Ok(humidity) |
||||
} |
} |
||||
|
Loading…
Reference in new issue