initial config changes

This commit is contained in:
Empire 2023-12-22 01:35:08 +01:00
parent 066b3ec24f
commit 58801f870e
11 changed files with 653 additions and 253 deletions

View File

@ -3,8 +3,7 @@ target = "xtensa-esp32-espidf"
[target.xtensa-esp32-espidf] [target.xtensa-esp32-espidf]
linker = "ldproxy" linker = "ldproxy"
# runner = "espflash --monitor --baud 921600" # Select this runner for espflash v1.x.x runner = "espflash flash --monitor --baud 921600 --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 = "cargo runner"
rustflags = [ "--cfg", "espidf_time64"] # Extending time_t for ESP IDF 5: https://github.com/esp-rs/rust/issues/110 rustflags = [ "--cfg", "espidf_time64"] # Extending time_t for ESP IDF 5: https://github.com/esp-rs/rust/issues/110

View File

@ -58,7 +58,7 @@ log = { version = "0.4", default-features = false }
esp-idf-svc = { version = "0.47.3", default-features = false } esp-idf-svc = { version = "0.47.3", default-features = false }
serde = { version = "1.0.192", features = ["derive"] } serde = { version = "1.0.192", features = ["derive"] }
average = { version = "0.14.1" , features = ["std"] } average = { version = "0.14.1" , features = ["std"] }
esp32 = "0.27.0" #esp32 = "0.28.0"
bit_field = "0.10.2" bit_field = "0.10.2"
ds18b20 = "0.1.1" ds18b20 = "0.1.1"
embedded-svc = { version = "0.26.4", features = ["experimental"] } embedded-svc = { version = "0.26.4", features = ["experimental"] }
@ -69,7 +69,7 @@ esp_idf_build = "0.1.3"
chrono = { version = "0.4.23", default-features = false , features = ["iana-time-zone"] } chrono = { version = "0.4.23", default-features = false , features = ["iana-time-zone"] }
chrono-tz = {version="0.8.0", default-features = false , features = [ "filter-by-regex" ]} chrono-tz = {version="0.8.0", default-features = false , features = [ "filter-by-regex" ]}
embedded-hal = "0.2.7" embedded-hal = "0.2.7"
shift-register-driver = "0.1.1" #shift-register-driver = "0.1.1"
one-wire-bus = "0.1.1" one-wire-bus = "0.1.1"
anyhow = { version = "1.0.75", features = ["std", "backtrace"] } anyhow = { version = "1.0.75", features = ["std", "backtrace"] }
schemars = "0.8.16" schemars = "0.8.16"

4
rust/src/lib.rs Normal file
View File

@ -0,0 +1,4 @@
#![allow(dead_code)]
extern crate embedded_hal as hal;
pub mod sipo;

View File

@ -1,23 +1,15 @@
use std::{ use std::{sync::{Arc, Mutex}, env};
ffi::CString,
fs::File,
io::{Read, Write},
mem,
str::from_utf8,
};
use chrono::{Datelike, NaiveDateTime, Timelike}; use chrono::{Datelike, NaiveDateTime, Timelike};
use anyhow::{Context, Result}; use anyhow::Result;
use chrono_tz::Europe::Berlin; use chrono_tz::Europe::Berlin;
use esp_idf_hal::delay::Delay; use esp_idf_hal::delay::Delay;
use esp_idf_svc::http::server::EspHttpServer; use esp_idf_sys::{esp_restart, vTaskDelay};
use esp_idf_sys::{esp_restart, vTaskDelay, TickType_t};
use heapless::String;
use plant_hal::{CreatePlantHal, PlantCtrlBoard, PlantCtrlBoardInteraction, PlantHal, PLANT_COUNT}; use plant_hal::{CreatePlantHal, PlantCtrlBoard, PlantCtrlBoardInteraction, PlantHal, PLANT_COUNT};
use webserver::webserver::httpd;
use crate::config::{Config, WifiConfig};
use crate::{config::{Config, WifiConfig}, webserver::webserver::httpd_initial};
mod config; mod config;
pub mod plant_hal; pub mod plant_hal;
mod webserver { mod webserver {
@ -33,14 +25,30 @@ enum OnlineMode {
MqttRoundtrip MqttRoundtrip
} }
fn wait_infinity(board: &mut PlantCtrlBoard<'_>) { enum WaitType{
InitialConfig,
FlashError
}
fn wait_infinity(board_access: Arc<Mutex<PlantCtrlBoard<'_>>>, wait_type:WaitType) -> !{
let delay = match wait_type {
WaitType::InitialConfig => 250_u32,
WaitType::FlashError => 100_u32,
};
board_access.lock().unwrap().light(true);
loop { loop {
unsafe { unsafe {
//do not trigger watchdog //do not trigger watchdog
board.general_fault(true); for i in 0..7 {
vTaskDelay(500_u32); board_access.lock().unwrap().fault(i, true);
board.general_fault(false); }
vTaskDelay(500_u32); board_access.lock().unwrap().general_fault(true);
vTaskDelay(delay);
board_access.lock().unwrap().general_fault(false);
for i in 0..7 {
board_access.lock().unwrap().fault(i, false);
}
vTaskDelay(delay);
} }
} }
} }
@ -59,7 +67,8 @@ fn main() -> Result<()> {
println!("Version useing git has {}", git_hash); println!("Version useing git has {}", git_hash);
println!("Board hal init"); println!("Board hal init");
let mut board = PlantHal::create()?; let board_access = PlantHal::create()?;
let mut board = board_access.lock().unwrap();
println!("Mounting filesystem"); println!("Mounting filesystem");
board.mountFileSystem()?; board.mountFileSystem()?;
let free_space = board.fileSystemSize()?; let free_space = board.fileSystemSize()?;
@ -103,7 +112,7 @@ fn main() -> Result<()> {
Err(err) => { Err(err) => {
println!("Could not remove config files, system borked {}", err); println!("Could not remove config files, system borked {}", err);
//terminate main app and freeze //terminate main app and freeze
wait_infinity(&mut board); wait_infinity( board_access.clone(), WaitType::FlashError);
} }
} }
} }
@ -118,12 +127,11 @@ fn main() -> Result<()> {
}, },
Err(err) => { Err(err) => {
println!("Missing wifi config, entering initial config mode {}", err); println!("Missing wifi config, entering initial config mode {}", err);
board.wifi_ap().unwrap();
//config upload will trigger reboot! //config upload will trigger reboot!
let _webserver = httpd(true); drop(board);
wait_infinity(&mut board); let _webserver = httpd_initial(board_access.clone());
//how to do this better? wait_infinity(board_access.clone(), WaitType::InitialConfig);
let ssid: String<32> = String::try_from("").unwrap();
wifi = WifiConfig { ssid : ssid, password : None};
}, },
}; };
@ -195,10 +203,10 @@ fn main() -> Result<()> {
println!("Running logic at europe/berlin {}", europe_time); println!("Running logic at europe/berlin {}", europe_time);
} }
if(online_mode == OnlineMode::SnTp){ if online_mode == OnlineMode::SnTp {
//mqtt here //mqtt here
} }
if(online_mode == OnlineMode::Mqtt){ if online_mode == OnlineMode::Mqtt {
//mqtt roundtrip here //mqtt roundtrip here
} }
//TODO configmode webserver logic here //TODO configmode webserver logic here

View File

@ -1,66 +1,71 @@
//mod config; //mod config;
use embedded_svc::wifi::{Configuration, ClientConfiguration, AuthMethod, Wifi}; use embedded_hal::blocking::delay::DelayMs;
use embedded_svc::wifi::{
AccessPointConfiguration, AuthMethod, ClientConfiguration, Configuration, Wifi, AccessPointInfo,
};
use esp_idf_svc::eventloop::EspSystemEventLoop; use esp_idf_svc::eventloop::EspSystemEventLoop;
use esp_idf_svc::nvs::EspDefaultNvsPartition; use esp_idf_svc::nvs::EspDefaultNvsPartition;
use esp_idf_svc::wifi::EspWifi; use esp_idf_svc::wifi::config::{ScanConfig, ScanType};
use esp_idf_svc::wifi::{EspWifi, NonBlocking};
use plant_ctrl2::sipo::ShiftRegister40;
use anyhow::anyhow;
use anyhow::{bail, Context, Ok, Result};
use std::ffi::CString; use std::ffi::CString;
use std::fs::File; use std::fs::File;
use std::io::{Read, BufReader}; use std::io::{BufReader, Read};
use std::path::Path; use std::path::Path;
use std::str::from_utf8; use std::str::from_utf8;
use std::sync::Mutex; use std::sync::{Mutex, Arc};
use anyhow::{Context, Result, bail, Ok}; use std::time::Duration;
use anyhow::anyhow;
use chrono::{Utc, NaiveDateTime, DateTime}; use chrono::{DateTime, NaiveDateTime, Utc};
use ds18b20::Ds18b20; use ds18b20::Ds18b20;
use embedded_hal::digital::v1_compat::OldOutputPin; use embedded_hal::digital::v1_compat::OldOutputPin;
use embedded_hal::digital::v2::OutputPin; use embedded_hal::digital::v2::OutputPin;
use esp_idf_hal::adc::config::Config; use esp_idf_hal::adc::config::Config;
use esp_idf_hal::adc::{AdcDriver, AdcChannelDriver, attenuation}; use esp_idf_hal::adc::{attenuation, AdcChannelDriver, AdcDriver};
use esp_idf_hal::delay::{Delay, FreeRtos}; use esp_idf_hal::delay::{Delay, FreeRtos};
use esp_idf_hal::pcnt::{PcntDriver, PcntChannel, PinIndex, PcntChannelConfig, PcntControlMode, PcntCountMode}; use esp_idf_hal::gpio::{AnyInputPin, Gpio39, Gpio4, Level, PinDriver};
use esp_idf_hal::pcnt::{
PcntChannel, PcntChannelConfig, PcntControlMode, PcntCountMode, PcntDriver, PinIndex,
};
use esp_idf_hal::prelude::Peripherals;
use esp_idf_hal::reset::ResetReason; use esp_idf_hal::reset::ResetReason;
use esp_idf_svc::sntp::{self, SyncStatus}; use esp_idf_svc::sntp::{self, SyncStatus};
use esp_idf_svc::systime::EspSystemTime; use esp_idf_svc::systime::EspSystemTime;
use esp_idf_sys::EspError; use esp_idf_sys::{vTaskDelay, EspError};
use one_wire_bus::OneWire; use one_wire_bus::OneWire;
use shift_register_driver::sipo::ShiftRegister40;
use esp_idf_hal::gpio::{PinDriver, Gpio39, Gpio4, AnyInputPin, Level};
use esp_idf_hal::prelude::Peripherals;
use serde::{Deserialize, Serialize}; use serde::{Deserialize, Serialize};
use crate::config::{self, WifiConfig}; use crate::config::{self, WifiConfig};
pub const PLANT_COUNT:usize = 8; pub const PLANT_COUNT: usize = 8;
const PINS_PER_PLANT:usize = 5; const PINS_PER_PLANT: usize = 5;
const PLANT_PUMP_OFFSET:usize = 0; const PLANT_PUMP_OFFSET: usize = 0;
const PLANT_FAULT_OFFSET:usize = 1; const PLANT_FAULT_OFFSET: usize = 1;
const PLANT_MOIST_PUMP_OFFSET:usize = 2; const PLANT_MOIST_PUMP_OFFSET: usize = 2;
const PLANT_MOIST_B_OFFSET:usize = 3; const PLANT_MOIST_B_OFFSET: usize = 3;
const PLANT_MOIST_A_OFFSET:usize = 4; const PLANT_MOIST_A_OFFSET: usize = 4;
const SPIFFS_PARTITION_NAME: &str = "storage"; const SPIFFS_PARTITION_NAME: &str = "storage";
const WIFI_CONFIG_FILE: &str = "/spiffs/wifi.cfg"; const WIFI_CONFIG_FILE: &str = "/spiffs/wifi.cfg";
const CONFIG_FILE: &str = "/spiffs/config.cfg"; const CONFIG_FILE: &str = "/spiffs/config.cfg";
#[link_section = ".rtc.data"] #[link_section = ".rtc.data"]
static mut LAST_WATERING_TIMESTAMP: [i64; PLANT_COUNT] = [0; PLANT_COUNT]; static mut LAST_WATERING_TIMESTAMP: [i64; PLANT_COUNT] = [0; PLANT_COUNT];
#[link_section = ".rtc.data"] #[link_section = ".rtc.data"]
static mut CONSECUTIVE_WATERING_PLANT: [u32; PLANT_COUNT] = [0; PLANT_COUNT]; static mut CONSECUTIVE_WATERING_PLANT: [u32; PLANT_COUNT] = [0; PLANT_COUNT];
#[link_section = ".rtc.data"] #[link_section = ".rtc.data"]
static mut LOW_VOLTAGE_DETECTED:bool = false; static mut LOW_VOLTAGE_DETECTED: bool = false;
pub struct BatteryState { pub struct BatteryState {
state_charge_percent: u8, state_charge_percent: u8,
max_error_percent: u8, max_error_percent: u8,
remaining_milli_ampere_hour: u32, remaining_milli_ampere_hour: u32,
max_milli_ampere_hour: u32, max_milli_ampere_hour: u32,
design_milli_ampere_hour:u32, design_milli_ampere_hour: u32,
voltage_milli_volt: u16, voltage_milli_volt: u16,
average_current_milli_ampere: u16, average_current_milli_ampere: u16,
temperature_tenth_kelvin: u32, temperature_tenth_kelvin: u32,
@ -68,25 +73,25 @@ pub struct BatteryState {
average_time_to_full_minute: u16, average_time_to_full_minute: u16,
average_discharge_power_cycle_milli_watt: u16, average_discharge_power_cycle_milli_watt: u16,
cycle_count: u16, cycle_count: u16,
state_health_percent: u8 state_health_percent: u8,
} }
pub struct FileSystemSizeInfo{ pub struct FileSystemSizeInfo {
pub total_size: usize, pub total_size: usize,
pub used_size: usize, pub used_size: usize,
pub free_size: usize pub free_size: usize,
} }
#[derive(Debug)] #[derive(Debug)]
pub enum Sensor{ pub enum Sensor {
A, A,
B, B,
PUMP PUMP,
} }
pub trait PlantCtrlBoardInteraction{ pub trait PlantCtrlBoardInteraction {
fn time(&mut self) -> Result<chrono::DateTime<Utc>>; fn time(&mut self) -> Result<chrono::DateTime<Utc>>;
fn wifi(&mut self, ssid:&str, password:Option<&str>, max_wait:u32) -> Result<()>; fn wifi(&mut self, ssid: &str, password: Option<&str>, max_wait: u32) -> Result<()>;
fn sntp(&mut self, max_wait:u32) -> Result<chrono::DateTime<Utc>>; fn sntp(&mut self, max_wait: u32) -> Result<chrono::DateTime<Utc>>;
fn mountFileSystem(&mut self) -> Result<()>; fn mountFileSystem(&mut self) -> Result<()>;
fn fileSystemSize(&mut self) -> Result<FileSystemSizeInfo>; fn fileSystemSize(&mut self) -> Result<FileSystemSizeInfo>;
@ -94,27 +99,27 @@ pub trait PlantCtrlBoardInteraction{
fn general_fault(&mut self, enable: bool); fn general_fault(&mut self, enable: bool);
fn is_day(&self,) -> bool; fn is_day(&self) -> bool;
fn water_temperature_c(&mut self,) -> Result<f32>; fn water_temperature_c(&mut self) -> Result<f32>;
fn tank_sensor_mv(&mut self,) -> Result<u16>; fn tank_sensor_mv(&mut self) -> Result<u16>;
fn set_low_voltage_in_cycle(&mut self,); fn set_low_voltage_in_cycle(&mut self);
fn clear_low_voltage_in_cycle(&mut self,); fn clear_low_voltage_in_cycle(&mut self);
fn low_voltage_in_cycle(&mut self) -> bool; fn low_voltage_in_cycle(&mut self) -> bool;
fn any_pump(&mut self, enabled:bool) -> Result<()>; fn any_pump(&mut self, enabled: bool) -> Result<()>;
//keep state during deepsleep //keep state during deepsleep
fn light(&mut self,enable:bool) -> Result<()>; fn light(&mut self, enable: bool) -> Result<()>;
fn measure_moisture_hz(&self, plant:usize, sensor:Sensor) -> Result<i32>; fn measure_moisture_hz(&self, plant: usize, sensor: Sensor) -> Result<i32>;
fn pump(&self,plant:usize, enable:bool) -> Result<()>; fn pump(&self, plant: usize, enable: bool) -> Result<()>;
fn last_pump_time(&self,plant:usize) -> Result<chrono::DateTime<Utc>>; fn last_pump_time(&self, plant: usize) -> Result<chrono::DateTime<Utc>>;
fn store_last_pump_time(&mut self,plant:usize, time: chrono::DateTime<Utc>); fn store_last_pump_time(&mut self, plant: usize, time: chrono::DateTime<Utc>);
fn store_consecutive_pump_count(&mut self,plant:usize, count:u32); fn store_consecutive_pump_count(&mut self, plant: usize, count: u32);
fn consecutive_pump_count(&mut self,plant:usize) -> u32; fn consecutive_pump_count(&mut self, plant: usize) -> u32;
//keep state during deepsleep //keep state during deepsleep
fn fault(&self,plant:usize, enable:bool); fn fault(&self, plant: usize, enable: bool);
//config //config
fn is_config_reset(&mut self) -> bool; fn is_config_reset(&mut self) -> bool;
@ -122,25 +127,54 @@ pub trait PlantCtrlBoardInteraction{
fn get_config(&mut self) -> Result<config::Config>; fn get_config(&mut self) -> Result<config::Config>;
fn get_wifi(&mut self) -> Result<config::WifiConfig>; fn get_wifi(&mut self) -> Result<config::WifiConfig>;
fn set_wifi(&mut self, wifi: &WifiConfig) -> Result<()>; fn set_wifi(&mut self, wifi: &WifiConfig) -> Result<()>;
fn wifi_ap(&mut self) -> Result<()>;
fn wifi_scan(&mut self) -> Result<Vec<AccessPointInfo>>;
} }
pub trait CreatePlantHal<'a> { pub trait CreatePlantHal<'a> {
fn create()-> Result<PlantCtrlBoard<'static>>; fn create() -> Result<Arc<Mutex<PlantCtrlBoard<'static>>>>;
} }
pub struct PlantHal { pub struct PlantHal {}
} impl CreatePlantHal<'_> for PlantHal {
fn create() -> Result<Arc<Mutex<PlantCtrlBoard<'static>>>> {
impl CreatePlantHal<'_> for PlantHal{
fn create() -> Result<PlantCtrlBoard<'static>> {
let peripherals = Peripherals::take()?; let peripherals = Peripherals::take()?;
let clock = OldOutputPin::from(PinDriver::output(peripherals.pins.gpio21)?); let mut clock = PinDriver::output(peripherals.pins.gpio21)?;
let latch = OldOutputPin::from(PinDriver::output(peripherals.pins.gpio22)?); let mut latch = PinDriver::output(peripherals.pins.gpio22)?;
let data = OldOutputPin::from(PinDriver::output(peripherals.pins.gpio19)?); let mut data = PinDriver::output(peripherals.pins.gpio19)?;
// loop {
// unsafe {
// let delay = Delay::new_default();
// latch.set_low().unwrap();
// delay.delay_ms(1);
// for i in 1..2 {
// data.set_high().unwrap();
// delay.delay_ms(1);
// clock.set_high().unwrap();
// delay.delay_ms(1);
// clock.set_low().unwrap();
// delay.delay_ms(1);
// }
// latch.set_high().unwrap();
// latch.set_low().unwrap();
// delay.delay_ms(1);
// for i in 1..2 {
// data.set_low().unwrap();
// delay.delay_ms(1);
// clock.set_high().unwrap();
// delay.delay_ms(1);
// clock.set_low().unwrap();
// delay.delay_ms(1);
// }
// latch.set_high().unwrap();
// vTaskDelay(5);
// }
// }
let one_wire_pin = PinDriver::input_output_od(peripherals.pins.gpio4)?; let one_wire_pin = PinDriver::input_output_od(peripherals.pins.gpio4)?;
//TODO make to none if not possible to init //TODO make to none if not possible to init
@ -201,61 +235,59 @@ impl CreatePlantHal<'_> for PlantHal{
counter_unit1.set_filter_value(1023)?; counter_unit1.set_filter_value(1023)?;
counter_unit1.filter_enable()?; counter_unit1.filter_enable()?;
println!("Wifi start"); println!("Wifi start");
let sys_loop = EspSystemEventLoop::take()?; let sys_loop = EspSystemEventLoop::take()?;
let nvs = EspDefaultNvsPartition::take()?; let nvs = EspDefaultNvsPartition::take()?;
let wifi_driver = EspWifi::new( let wifi_driver = EspWifi::new(peripherals.modem, sys_loop, Some(nvs))?;
peripherals.modem,
sys_loop,
Some(nvs)
)?;
let shift_register = ShiftRegister40::new(clock, latch, data); let shift_register = ShiftRegister40::new(clock, latch, data);
let last_watering_timestamp = Mutex::new(unsafe { LAST_WATERING_TIMESTAMP }); let last_watering_timestamp = Mutex::new(unsafe { LAST_WATERING_TIMESTAMP });
let consecutive_watering_plant = Mutex::new(unsafe { CONSECUTIVE_WATERING_PLANT }); let consecutive_watering_plant = Mutex::new(unsafe { CONSECUTIVE_WATERING_PLANT });
let low_voltage_detected = Mutex::new(unsafe { LOW_VOLTAGE_DETECTED }); let low_voltage_detected = Mutex::new(unsafe { LOW_VOLTAGE_DETECTED });
let tank_driver = AdcDriver::new(peripherals.adc1, &Config::new())?; let tank_driver = AdcDriver::new(peripherals.adc1, &Config::new())?;
let tank_channel: AdcChannelDriver<'_, {attenuation::DB_11}, Gpio39> = AdcChannelDriver::new(peripherals.pins.gpio39)?; let tank_channel: AdcChannelDriver<'_, { attenuation::DB_11 }, Gpio39> =
AdcChannelDriver::new(peripherals.pins.gpio39)?;
let solar_is_day = PinDriver::input(peripherals.pins.gpio25)?; let solar_is_day = PinDriver::input(peripherals.pins.gpio25)?;
let boot_button = PinDriver::input(peripherals.pins.gpio0)?; let boot_button = PinDriver::input(peripherals.pins.gpio0)?;
let light = PinDriver::output(peripherals.pins.gpio26)?; let light = PinDriver::output(peripherals.pins.gpio26)?;
let main_pump = PinDriver::output(peripherals.pins.gpio23)?; let main_pump = PinDriver::output(peripherals.pins.gpio23)?;
let tank_power = PinDriver::output(peripherals.pins.gpio27)?; let tank_power = PinDriver::output(peripherals.pins.gpio27)?;
let general_fault = PinDriver::output(peripherals.pins.gpio13)?; let general_fault = PinDriver::output(peripherals.pins.gpio13)?;
let one_wire_bus = OneWire::new(one_wire_pin).map_err(|err| -> anyhow::Error {anyhow!("Missing attribute: {:?}", err)})?; let one_wire_bus = OneWire::new(one_wire_pin)
.map_err(|err| -> anyhow::Error { anyhow!("Missing attribute: {:?}", err) })?;
println!("After stuff"); println!("After stuff");
return Ok(PlantCtrlBoard {
shift_register : shift_register, let rv = Arc::new(Mutex::new(PlantCtrlBoard {
last_watering_timestamp : last_watering_timestamp, //shift_register : shift_register,
consecutive_watering_plant : consecutive_watering_plant, last_watering_timestamp: last_watering_timestamp,
low_voltage_detected : low_voltage_detected, consecutive_watering_plant: consecutive_watering_plant,
tank_driver : tank_driver, low_voltage_detected: low_voltage_detected,
tank_driver: tank_driver,
tank_channel: tank_channel, tank_channel: tank_channel,
solar_is_day : solar_is_day, solar_is_day: solar_is_day,
boot_button : boot_button, boot_button: boot_button,
light: light, light: light,
main_pump: main_pump, main_pump: main_pump,
tank_power: tank_power, tank_power: tank_power,
general_fault: general_fault, general_fault: general_fault,
one_wire_bus: one_wire_bus, one_wire_bus: one_wire_bus,
signal_counter : counter_unit1, signal_counter: counter_unit1,
wifi_driver : wifi_driver wifi_driver: wifi_driver,
}); }));
return Ok(rv);
} }
} }
pub struct PlantCtrlBoard<'a> {
pub struct PlantCtrlBoard<'a>{ //shift_register: ShiftRegister40<OldOutputPin<PinDriver<'a, esp_idf_hal::gpio::Gpio21, esp_idf_hal::gpio::Output>>, OldOutputPin<PinDriver<'a, esp_idf_hal::gpio::Gpio22, esp_idf_hal::gpio::Output>>, OldOutputPin<PinDriver<'a, esp_idf_hal::gpio::Gpio19, esp_idf_hal::gpio::Output>>>,
shift_register: ShiftRegister40<OldOutputPin<PinDriver<'a, esp_idf_hal::gpio::Gpio21, esp_idf_hal::gpio::Output>>, OldOutputPin<PinDriver<'a, esp_idf_hal::gpio::Gpio22, esp_idf_hal::gpio::Output>>, OldOutputPin<PinDriver<'a, esp_idf_hal::gpio::Gpio19, esp_idf_hal::gpio::Output>>>,
consecutive_watering_plant: Mutex<[u32; PLANT_COUNT]>, consecutive_watering_plant: Mutex<[u32; PLANT_COUNT]>,
last_watering_timestamp: Mutex<[i64; PLANT_COUNT]>, last_watering_timestamp: Mutex<[i64; PLANT_COUNT]>,
low_voltage_detected: Mutex<bool>, low_voltage_detected: Mutex<bool>,
tank_driver: AdcDriver<'a, esp_idf_hal::adc::ADC1>, tank_driver: AdcDriver<'a, esp_idf_hal::adc::ADC1>,
tank_channel: esp_idf_hal::adc::AdcChannelDriver<'a, { attenuation::DB_11 }, Gpio39 >, tank_channel: esp_idf_hal::adc::AdcChannelDriver<'a, { attenuation::DB_11 }, Gpio39>,
solar_is_day: PinDriver<'a, esp_idf_hal::gpio::Gpio25, esp_idf_hal::gpio::Input>, solar_is_day: PinDriver<'a, esp_idf_hal::gpio::Gpio25, esp_idf_hal::gpio::Input>,
boot_button: PinDriver<'a, esp_idf_hal::gpio::Gpio0, esp_idf_hal::gpio::Input>, boot_button: PinDriver<'a, esp_idf_hal::gpio::Gpio0, esp_idf_hal::gpio::Input>,
signal_counter: PcntDriver<'a>, signal_counter: PcntDriver<'a>,
@ -268,36 +300,45 @@ pub struct PlantCtrlBoard<'a>{
} }
impl PlantCtrlBoardInteraction for PlantCtrlBoard<'_> { impl PlantCtrlBoardInteraction for PlantCtrlBoard<'_> {
fn battery_state(&mut self,) -> Result<BatteryState> { fn battery_state(&mut self) -> Result<BatteryState> {
todo!() todo!()
} }
fn is_day(&self,) -> bool { fn is_day(&self) -> bool {
return self.solar_is_day.get_level().into(); return self.solar_is_day.get_level().into();
} }
fn water_temperature_c(&mut self,) -> Result<f32> { fn water_temperature_c(&mut self) -> Result<f32> {
let mut delay = Delay::new_default(); let mut delay = Delay::new_default();
self.one_wire_bus.reset(&mut delay).map_err(|err| -> anyhow::Error {anyhow!("Missing attribute: {:?}", err)})?; self.one_wire_bus
.reset(&mut delay)
.map_err(|err| -> anyhow::Error { anyhow!("Missing attribute: {:?}", err) })?;
let first = self.one_wire_bus.devices(false, &mut delay).next(); let first = self.one_wire_bus.devices(false, &mut delay).next();
if first.is_none() { if first.is_none() {
bail!("Not found any one wire Ds18b20"); bail!("Not found any one wire Ds18b20");
} }
let device_address = first.unwrap().map_err(|err| -> anyhow::Error {anyhow!("Missing attribute: {:?}", err)})?; let device_address = first
.unwrap()
.map_err(|err| -> anyhow::Error { anyhow!("Missing attribute: {:?}", err) })?;
let water_temp_sensor = Ds18b20::new::<EspError>(device_address).map_err(|err| -> anyhow::Error {anyhow!("Missing attribute: {:?}", err)})?; let water_temp_sensor = Ds18b20::new::<EspError>(device_address)
.map_err(|err| -> anyhow::Error { anyhow!("Missing attribute: {:?}", err) })?;
water_temp_sensor.start_temp_measurement(&mut self.one_wire_bus, &mut delay).map_err(|err| -> anyhow::Error {anyhow!("Missing attribute: {:?}", err)})?; water_temp_sensor
.start_temp_measurement(&mut self.one_wire_bus, &mut delay)
.map_err(|err| -> anyhow::Error { anyhow!("Missing attribute: {:?}", err) })?;
ds18b20::Resolution::Bits12.delay_for_measurement_time(&mut delay); ds18b20::Resolution::Bits12.delay_for_measurement_time(&mut delay);
let sensor_data = water_temp_sensor.read_data(&mut self.one_wire_bus, &mut delay).map_err(|err| -> anyhow::Error {anyhow!("Missing attribute: {:?}", err)})?; let sensor_data = water_temp_sensor
.read_data(&mut self.one_wire_bus, &mut delay)
.map_err(|err| -> anyhow::Error { anyhow!("Missing attribute: {:?}", err) })?;
if sensor_data.temperature == 85_f32 { if sensor_data.temperature == 85_f32 {
bail!("Ds18b20 dummy temperature returned"); bail!("Ds18b20 dummy temperature returned");
} }
return Ok(sensor_data.temperature); return Ok(sensor_data.temperature);
} }
fn tank_sensor_mv(&mut self,) -> Result<u16> { fn tank_sensor_mv(&mut self) -> Result<u16> {
let delay = Delay::new_default(); let delay = Delay::new_default();
self.tank_power.set_high()?; self.tank_power.set_high()?;
//let stabilize //let stabilize
@ -307,68 +348,70 @@ impl PlantCtrlBoardInteraction for PlantCtrlBoard<'_> {
return Ok(value); return Ok(value);
} }
fn set_low_voltage_in_cycle(&mut self,) { fn set_low_voltage_in_cycle(&mut self) {
*self.low_voltage_detected.get_mut().unwrap() = true; *self.low_voltage_detected.get_mut().unwrap() = true;
} }
fn clear_low_voltage_in_cycle(&mut self,) { fn clear_low_voltage_in_cycle(&mut self) {
*self.low_voltage_detected.get_mut().unwrap() = false; *self.low_voltage_detected.get_mut().unwrap() = false;
} }
fn light(&mut self,enable:bool) -> Result<()>{ fn light(&mut self, enable: bool) -> Result<()> {
self.light.set_state(enable.into())?; self.light.set_state(enable.into())?;
Ok(()) Ok(())
} }
fn pump(&self,plant:usize, enable:bool) -> Result<()> { fn pump(&self, plant: usize, enable: bool) -> Result<()> {
let index = plant*PINS_PER_PLANT+PLANT_PUMP_OFFSET; let index = plant * PINS_PER_PLANT + PLANT_PUMP_OFFSET;
//currently infailable error, keep for future as result anyway //currently infailable error, keep for future as result anyway
self.shift_register.decompose()[index].set_state(enable.into()).unwrap(); //self.shift_register.decompose()[index].set_state(enable.into()).unwrap();
Ok(()) Ok(())
} }
fn last_pump_time(&self,plant:usize) -> Result<chrono::DateTime<Utc>> { fn last_pump_time(&self, plant: usize) -> Result<chrono::DateTime<Utc>> {
let ts = unsafe { LAST_WATERING_TIMESTAMP }[plant]; let ts = unsafe { LAST_WATERING_TIMESTAMP }[plant];
let timestamp = NaiveDateTime::from_timestamp_millis(ts).ok_or(anyhow!("could not convert timestamp"))?; let timestamp = NaiveDateTime::from_timestamp_millis(ts)
.ok_or(anyhow!("could not convert timestamp"))?;
return Ok(DateTime::<Utc>::from_naive_utc_and_offset(timestamp, Utc)); return Ok(DateTime::<Utc>::from_naive_utc_and_offset(timestamp, Utc));
} }
fn store_last_pump_time(&mut self,plant:usize, time: chrono::DateTime<Utc>) { fn store_last_pump_time(&mut self, plant: usize, time: chrono::DateTime<Utc>) {
self.last_watering_timestamp.get_mut().unwrap()[plant] = time.timestamp_millis(); self.last_watering_timestamp.get_mut().unwrap()[plant] = time.timestamp_millis();
} }
fn store_consecutive_pump_count(&mut self,plant:usize, count:u32) { fn store_consecutive_pump_count(&mut self, plant: usize, count: u32) {
self.consecutive_watering_plant.get_mut().unwrap()[plant] = count; self.consecutive_watering_plant.get_mut().unwrap()[plant] = count;
} }
fn consecutive_pump_count(&mut self,plant:usize) -> u32 { fn consecutive_pump_count(&mut self, plant: usize) -> u32 {
return self.consecutive_watering_plant.get_mut().unwrap()[plant] return self.consecutive_watering_plant.get_mut().unwrap()[plant];
} }
fn fault(&self,plant:usize, enable:bool) { fn fault(&self, plant: usize, enable: bool) {
let index = plant*PINS_PER_PLANT+PLANT_FAULT_OFFSET; let index = plant * PINS_PER_PLANT + PLANT_FAULT_OFFSET;
self.shift_register.decompose()[index].set_state(enable.into()).unwrap() //self.shift_register.decompose()[index].set_state(enable.into()).unwrap()
} }
fn low_voltage_in_cycle(&mut self) -> bool { fn low_voltage_in_cycle(&mut self) -> bool {
return *self.low_voltage_detected.get_mut().unwrap() return *self.low_voltage_detected.get_mut().unwrap();
} }
fn any_pump(&mut self, enable:bool) -> Result<()> { fn any_pump(&mut self, enable: bool) -> Result<()> {
return Ok(self.main_pump.set_state(enable.into()).unwrap()); return Ok(self.main_pump.set_state(enable.into()).unwrap());
} }
fn time(&mut self) -> Result<chrono::DateTime<Utc>> { fn time(&mut self) -> Result<chrono::DateTime<Utc>> {
let time = EspSystemTime{}.now().as_millis(); let time = EspSystemTime {}.now().as_millis();
let smaller_time = time as i64; let smaller_time = time as i64;
let local_time = NaiveDateTime::from_timestamp_millis(smaller_time).ok_or(anyhow!("could not convert timestamp"))?; let local_time = NaiveDateTime::from_timestamp_millis(smaller_time)
.ok_or(anyhow!("could not convert timestamp"))?;
return Ok(local_time.and_utc()); return Ok(local_time.and_utc());
} }
fn sntp(&mut self, max_wait_ms:u32) -> Result<chrono::DateTime<Utc>> { fn sntp(&mut self, max_wait_ms: u32) -> Result<chrono::DateTime<Utc>> {
let sntp = sntp::EspSntp::new_default()?; let sntp = sntp::EspSntp::new_default()?;
let mut counter = 0; let mut counter = 0;
while sntp.get_sync_status() != SyncStatus::Completed{ while sntp.get_sync_status() != SyncStatus::Completed {
let delay = Delay::new_default(); let delay = Delay::new_default();
delay.delay_ms(100); delay.delay_ms(100);
counter += 100; counter += 100;
@ -380,7 +423,7 @@ impl PlantCtrlBoardInteraction for PlantCtrlBoard<'_> {
return self.time(); return self.time();
} }
fn measure_moisture_hz(&self, plant:usize, sensor:Sensor) -> Result<i32> { fn measure_moisture_hz(&self, plant: usize, sensor: Sensor) -> Result<i32> {
self.signal_counter.counter_pause()?; self.signal_counter.counter_pause()?;
self.signal_counter.counter_clear()?; self.signal_counter.counter_clear()?;
// //
@ -389,55 +432,72 @@ impl PlantCtrlBoardInteraction for PlantCtrlBoard<'_> {
Sensor::B => PLANT_MOIST_B_OFFSET, Sensor::B => PLANT_MOIST_B_OFFSET,
Sensor::PUMP => PLANT_MOIST_PUMP_OFFSET, Sensor::PUMP => PLANT_MOIST_PUMP_OFFSET,
}; };
let index = plant*PINS_PER_PLANT+offset; let index = plant * PINS_PER_PLANT + offset;
let delay = Delay::new_default(); let delay = Delay::new_default();
let measurement = 100; let measurement = 100;
let factor = 1000/100; let factor = 1000 / 100;
self.shift_register.decompose()[index].set_high().unwrap(); //self.shift_register.decompose()[index].set_high().unwrap();
//give some time to stabilize //give some time to stabilize
delay.delay_ms(10); delay.delay_ms(10);
self.signal_counter.counter_resume()?; self.signal_counter.counter_resume()?;
delay.delay_ms(measurement); delay.delay_ms(measurement);
self.signal_counter.counter_pause()?; self.signal_counter.counter_pause()?;
self.shift_register.decompose()[index].set_low().unwrap(); //self.shift_register.decompose()[index].set_low().unwrap();
let unscaled = self.signal_counter.get_counter_value()? as i32; let unscaled = self.signal_counter.get_counter_value()? as i32;
let hz = unscaled*factor; let hz = unscaled * factor;
println!("Measuring {:?} @ {} with {}", sensor, plant, hz); println!("Measuring {:?} @ {} with {}", sensor, plant, hz);
return Ok(hz); return Ok(hz);
} }
fn general_fault(&mut self, enable:bool) { fn general_fault(&mut self, enable: bool) {
self.general_fault.set_state(enable.into()).unwrap(); self.general_fault.set_state(enable.into()).unwrap();
} }
fn wifi(&mut self, ssid:&str, password:Option<&str>,max_wait:u32) -> Result<()> { fn wifi_ap(&mut self) -> Result<()> {
match password{
let apconfig = AccessPointConfiguration {
ssid: "PlantCtrl".into(),
auth_method: AuthMethod::None,
ssid_hidden: false,
..Default::default()
};
let clientconfig = ClientConfiguration::default();
self.wifi_driver.set_configuration(&Configuration::Mixed(clientconfig, apconfig))?;
self.wifi_driver.start()?;
Ok(())
}
fn wifi(&mut self, ssid: &str, password: Option<&str>, max_wait: u32) -> Result<()> {
match password {
Some(pw) => { 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 //TODO expect error due to invalid pw or similar! //call this during configuration and check if works, revert to config mode if not
self.wifi_driver.set_configuration(&Configuration::Client(ClientConfiguration{ self.wifi_driver.set_configuration(&Configuration::Client(
ClientConfiguration {
ssid: ssid.into(), ssid: ssid.into(),
password: pw.into(), password: pw.into(),
..Default::default() ..Default::default()
}))?;
}, },
))?;
}
None => { None => {
self.wifi_driver.set_configuration(&Configuration::Client(ClientConfiguration { self.wifi_driver
.set_configuration(&Configuration::Client(ClientConfiguration {
ssid: ssid.into(), ssid: ssid.into(),
auth_method: AuthMethod::None, auth_method: AuthMethod::None,
..Default::default() ..Default::default()
})).unwrap(); }))
}, .unwrap();
}
} }
self.wifi_driver.start().unwrap(); self.wifi_driver.start()?;
self.wifi_driver.connect().unwrap(); self.wifi_driver.connect()?;
let delay = Delay::new_default(); let delay = Delay::new_default();
let mut counter = 0_u32; let mut counter = 0_u32;
while !self.wifi_driver.is_connected().unwrap(){ while !self.wifi_driver.is_connected()? {
let config = self.wifi_driver.get_configuration().unwrap();
println!("Waiting for station connection"); println!("Waiting for station connection");
//TODO blink status? //TODO blink status?
delay.delay_ms(250); delay.delay_ms(250);
@ -489,9 +549,17 @@ impl PlantCtrlBoardInteraction for PlantCtrlBoard<'_> {
let mut total_size = 0; let mut total_size = 0;
let mut used_size = 0; let mut used_size = 0;
unsafe { unsafe {
esp_idf_sys::esp!(esp_idf_sys::esp_spiffs_info(storage.as_ptr(),&mut total_size,&mut used_size))?; esp_idf_sys::esp!(esp_idf_sys::esp_spiffs_info(
storage.as_ptr(),
&mut total_size,
&mut used_size
))?;
} }
return Ok(FileSystemSizeInfo{total_size, used_size, free_size : total_size - used_size}); return Ok(FileSystemSizeInfo {
total_size,
used_size,
free_size: total_size - used_size,
});
} }
fn is_config_reset(&mut self) -> bool { fn is_config_reset(&mut self) -> bool {
@ -519,7 +587,7 @@ impl PlantCtrlBoardInteraction for PlantCtrlBoard<'_> {
return Ok(config); return Ok(config);
} }
fn set_wifi(&mut self, wifi: &WifiConfig ) -> Result<()> { fn set_wifi(&mut self, wifi: &WifiConfig) -> Result<()> {
let mut cfg = File::create(WIFI_CONFIG_FILE)?; let mut cfg = File::create(WIFI_CONFIG_FILE)?;
serde_json::to_writer(&mut cfg, &wifi)?; serde_json::to_writer(&mut cfg, &wifi)?;
println!("Wrote wifi config {}", wifi); println!("Wrote wifi config {}", wifi);
@ -532,8 +600,24 @@ impl PlantCtrlBoardInteraction for PlantCtrlBoard<'_> {
let read = cfg.read(&mut data)?; let read = cfg.read(&mut data)?;
println!("Read file {}", from_utf8(&data[0..read])?); println!("Read file {}", from_utf8(&data[0..read])?);
bail!("todo") bail!("todo")
} }
fn wifi_scan(&mut self) -> Result<Vec<AccessPointInfo>> {
for i in 1..11 {
println!("Scanning channel {}", i);
self.wifi_driver.start_scan(&ScanConfig{
scan_type : ScanType::Passive(Duration::from_secs(1)),
show_hidden: false,
channel: Some(i),
..Default::default()
}, true)?;
let sr = self.wifi_driver.get_scan_result()?;
for r in sr.iter(){
println!("Found wifi {}", r.ssid);
}
}
return bail!("dummy");
}
} }

146
rust/src/sipo.rs Normal file
View File

@ -0,0 +1,146 @@
//! Serial-in parallel-out shift register
use core::cell::RefCell;
use core::mem::{self, MaybeUninit};
use crate::hal::digital::v2::OutputPin;
trait ShiftRegisterInternal {
fn update(&self, index: usize, command: bool) -> Result<(), ()>;
}
/// Output pin of the shift register
pub struct ShiftRegisterPin<'a>
{
shift_register: &'a dyn ShiftRegisterInternal,
index: usize,
}
impl<'a> ShiftRegisterPin<'a>
{
fn new(shift_register: &'a dyn ShiftRegisterInternal, index: usize) -> Self {
ShiftRegisterPin { shift_register, index }
}
}
impl OutputPin for ShiftRegisterPin<'_>
{
type Error = ();
fn set_low(&mut self) -> Result<(), Self::Error> {
self.shift_register.update(self.index, false)?;
Ok(())
}
fn set_high(&mut self) -> Result<(), Self::Error> {
self.shift_register.update(self.index, true)?;
Ok(())
}
}
macro_rules! ShiftRegisterBuilder {
($name: ident, $size: expr) => {
/// Serial-in parallel-out shift register
pub struct $name<Pin1, Pin2, Pin3>
where Pin1: OutputPin,
Pin2: OutputPin,
Pin3: OutputPin
{
clock: RefCell<Pin1>,
latch: RefCell<Pin2>,
data: RefCell<Pin3>,
output_state: RefCell<[bool; $size]>,
}
impl<Pin1, Pin2, Pin3> ShiftRegisterInternal for $name<Pin1, Pin2, Pin3>
where Pin1: OutputPin,
Pin2: OutputPin,
Pin3: OutputPin
{
/// Sets the value of the shift register output at `index` to value `command`
fn update(&self, index: usize, command: bool) -> Result<(), ()>{
self.output_state.borrow_mut()[index] = command;
let output_state = self.output_state.borrow();
self.latch.borrow_mut().set_low().map_err(|_e| ())?;
for i in 1..=output_state.len() {
if output_state[output_state.len() - i] {
self.data.borrow_mut().set_high().map_err(|_e| ())?;
} else {
self.data.borrow_mut().set_low().map_err(|_e| ())?;
}
self.clock.borrow_mut().set_high().map_err(|_e| ())?;
self.clock.borrow_mut().set_low().map_err(|_e| ())?;
}
self.latch.borrow_mut().set_high().map_err(|_e| ())?;
Ok(())
}
}
impl<Pin1, Pin2, Pin3> $name<Pin1, Pin2, Pin3>
where Pin1: OutputPin,
Pin2: OutputPin,
Pin3: OutputPin
{
/// Creates a new SIPO shift register from clock, latch, and data output pins
pub fn new(clock: Pin1, latch: Pin2, data: Pin3) -> Self {
$name {
clock: RefCell::new(clock),
latch: RefCell::new(latch),
data: RefCell::new(data),
output_state: RefCell::new([false; $size]),
}
}
/// Get embedded-hal output pins to control the shift register outputs
pub fn decompose(&self) -> [ShiftRegisterPin; $size] {
// Create an uninitialized array of `MaybeUninit`. The `assume_init` is
// safe because the type we are claiming to have initialized here is a
// bunch of `MaybeUninit`s, which do not require initialization.
let mut pins: [MaybeUninit<ShiftRegisterPin>; $size] = unsafe {
MaybeUninit::uninit().assume_init()
};
// Dropping a `MaybeUninit` does nothing, so if there is a panic during this loop,
// we have a memory leak, but there is no memory safety issue.
for (index, elem) in pins.iter_mut().enumerate() {
elem.write(ShiftRegisterPin::new(self, index));
}
// Everything is initialized. Transmute the array to the
// initialized type.
unsafe { mem::transmute::<_, [ShiftRegisterPin; $size]>(pins) }
}
/// Consume the shift register and return the original clock, latch, and data output pins
pub fn release(self) -> (Pin1, Pin2, Pin3) {
let Self{clock, latch, data, output_state: _} = self;
(clock.into_inner(), latch.into_inner(), data.into_inner())
}
}
}
}
ShiftRegisterBuilder!(ShiftRegister8, 8);
ShiftRegisterBuilder!(ShiftRegister16, 16);
ShiftRegisterBuilder!(ShiftRegister24, 24);
ShiftRegisterBuilder!(ShiftRegister32, 32);
ShiftRegisterBuilder!(ShiftRegister40, 40);
ShiftRegisterBuilder!(ShiftRegister48, 48);
ShiftRegisterBuilder!(ShiftRegister56, 56);
ShiftRegisterBuilder!(ShiftRegister64, 64);
ShiftRegisterBuilder!(ShiftRegister72, 72);
ShiftRegisterBuilder!(ShiftRegister80, 80);
ShiftRegisterBuilder!(ShiftRegister88, 88);
ShiftRegisterBuilder!(ShiftRegister96, 96);
ShiftRegisterBuilder!(ShiftRegister104, 104);
ShiftRegisterBuilder!(ShiftRegister112, 112);
ShiftRegisterBuilder!(ShiftRegister120, 120);
ShiftRegisterBuilder!(ShiftRegister128, 128);
/// 8 output serial-in parallel-out shift register
pub type ShiftRegister<Pin1, Pin2, Pin3> = ShiftRegister8<Pin1, Pin2, Pin3>;

View File

@ -1,7 +1,7 @@
<html> <html>
<meta> <meta>
<script src="ota.js"></script> <script src="ota.js"></script>
<script src="jsoneditor.js"></script> <script src="form.js"></script>
</meta> </meta>
<body> <body>
@ -15,58 +15,43 @@
</form> </form>
<h2>config</h2> <h2>config</h2>
<div id="editor_holder" data-theme="html" class="je-ready">
<button id="submit" onclick="createForm()">Create</button>
<div id="configform">
<h3>Tank:</h3>
<div>
<input type="checkbox" id="tank_sensor_enabled" onchange="submitForm()">
Enable Tank Sensor
</div> </div>
<button id="submit">Submit (console.log)</button>
<script>
// Initialize the editor with a JSON schema
var editor = new JSONEditor(document.getElementById('editor_holder'),{
schema: {
type: "object",
title: "Car",
properties: {
make: {
type: "string",
enum: [
"Toyota",
"BMW",
"Honda",
"Ford",
"Chevy",
"VW"
]
},
model: {
type: "string"
},
year: {
type: "integer",
enum: [
1995,1996,1997,1998,1999,
2000,2001,2002,2003,2004,
2005,2006,2007,2008,2009,
2010,2011,2012,2013,2014
],
default: 2008
},
safety: {
type: "integer",
format: "rating",
maximum: "5",
exclusiveMaximum: false,
readonly: false
}
}
}
});
// Hook up the submit button to log to the console <div>
document.getElementById('submit').addEventListener('click',function() { <input type="number" min="2" max="500000" id="tank_full_ml" onchange="submitForm()">
// Get the value from the editor Tank Size mL
console.log(editor.getValue()); </div>
}); <div>
</script> <input type="number" min="1" max="500000" id="tank_warn_percent" onchange="submitForm()">
Tank Warn below mL
</div>
<h3>Light:</h3>
<div>
Start
<input type="time" id="night_lamp_time_start" onchange="submitForm()">
Stop
<input type="time" id="night_lamp_time_end" onchange="submitForm()">
</div>
<div>
<input type="checkbox" id="night_lamp_only_when_dark" onchange="submitForm()">
Light only when dark
</div>
<h3>Plants:</h3>
<div id="plants"></div>
</div>
<button id="submit" onclick="submitForm()">Submit</button>
<br>
<textarea id="json" cols=50 rows=10></textarea>
</body> </body>
</html> </html>

109
rust/src/webserver/form.js Normal file
View File

@ -0,0 +1,109 @@
var plantcount = 1;
function createForm(){
var current = {}
current.tank_sensor_enabled = true;
current.tank_full_ml = 400;
current.tank_warn_percent = 200;
current.night_lamp_time_start = "18:00";
current.night_lamp_time_end = "02:00";
current.night_lamp_only_when_dark = true;
current.plants = [
{
target_moisture: 40,
pump_time_s:60
}
]
current.plantcount = 1;
plantcount = current.plantcount;
for(i=0;i<plantcount;i++){
var plant = document.createElement("div");
plants.appendChild(plant);
var header = document.createElement("h4");
header.textContent = "Plant " + (i+1);
plant.appendChild(header);
{
var holder = document.createElement("div");
plant.appendChild(holder);
var inputf = document.createElement("input");
inputf.id = "plant_"+i+"_target_moisture";
inputf.onchange = function() {submitForm()};
inputf.type = "number";
inputf.min = 0;
inputf.max = 100;
holder.appendChild(inputf)
var text = document.createElement("span");
holder.appendChild(text)
text.innerHTML += "Target Moisture"
}
{
var holder = document.createElement("div");
plant.appendChild(holder);
var input = document.createElement("input");
input.id = "plant_"+i+"_pump_time_s";
input.onchange = function() {submitForm()};
input.type = "number";
input.min = 0;
input.max = 600;
holder.appendChild(input)
var text = document.createElement("span");
holder.appendChild(text)
text.innerHTML += "Pump Time (s)"
}
}
sync(current);
}
function sync(current){
document.getElementById("tank_full_ml").disabled = !current.tank_sensor_enabled;
document.getElementById("tank_warn_percent").disabled = !current.tank_sensor_enabled;
document.getElementById("tank_sensor_enabled").checked = current.tank_sensor_enabled;
document.getElementById("tank_full_ml").value = current.tank_full_ml;
document.getElementById("tank_warn_percent").value = current.tank_warn_percent;
document.getElementById("night_lamp_time_start").value = current.night_lamp_time_start;
document.getElementById("night_lamp_time_end").value = current.night_lamp_time_end;
document.getElementById("tank_warn_percent").value = current.tank_warn_percent;
for(i=0;i<plantcount;i++){
document.getElementById("plant_"+i+"_target_moisture").value = current.plants[i].target_moisture;
document.getElementById("plant_"+i+"_pump_time_s").value = current.plants[i].pump_time_s;
}
}
function submitForm() {
var current = {}
current.plantcount = plantcount;
current.tank_sensor_enabled = document.getElementById("tank_sensor_enabled").checked;
current.tank_full_ml = document.getElementById("tank_full_ml").value;
current.tank_warn_percent = document.getElementById("tank_warn_percent").value;
current.night_lamp_time_start = document.getElementById("night_lamp_time_start").value;
current.night_lamp_time_end = document.getElementById("night_lamp_time_end").value;
current.night_lamp_only_when_dark = document.getElementById("night_lamp_only_when_dark").checked;
current.plants = []
for(i=0;i<plantcount;i++){
console.log("Adding plant " + i)
current.plants[i] = {}
current.plants[i].target_moisture = document.getElementById("plant_"+i+"_target_moisture").value;
current.plants[i].pump_time_s = document.getElementById("plant_"+i+"_pump_time_s").value;
}
sync(current);
console.log(current);
var pretty = JSON.stringify(current, undefined, 4);
document.getElementById('json').value = pretty;
}

View File

@ -1,6 +1,7 @@
<html> <html>
<meta> <meta>
<script src="ota.js"></script> <script src="ota.js"></script>
<script src="wifi.js"></script>
</meta> </meta>
<body> <body>
<div> <div>
@ -13,5 +14,17 @@
<p id="loaded_n_total"></p> <p id="loaded_n_total"></p>
</form> </form>
</div> </div>
<div>
<h2>WIFI</h2>
<input type="button" id="scan" onchange="scanWifi()" value="Scan">
<br>
<label for="ssid">SSID:</label>
<input type="text" id="ssid" list="cityname">
<datalist id="cityname">
<option value="Boston">
<option value="Cambridge">
</datalist>
</div>
</body> </body>
</html> </html>

View File

@ -1,23 +1,64 @@
//offer ota and config mode //offer ota and config mode
use std::{vec, sync::{Mutex, Arc}};
use embedded_svc::http::Method; use embedded_svc::http::Method;
use esp_idf_svc::http::server::EspHttpServer; use esp_idf_svc::http::server::EspHttpServer;
use esp_ota::OtaUpdate; use esp_ota::OtaUpdate;
#[allow(unused_variables)] use crate::plant_hal::{PlantCtrlBoard, PlantCtrlBoardInteraction};
pub fn httpd(initial_config:bool) -> EspHttpServer<'static> {
let mut server = EspHttpServer::new(&Default::default()).unwrap(); pub fn httpd_initial(board_access:Arc<Mutex<PlantCtrlBoard<'static>>>) -> Box<EspHttpServer<'static>> {
let mut server = shared();
server.fn_handler("/",Method::Get, move |request| {
let mut response = request.into_ok_response()?;
response.write(include_bytes!("initial_config.html"))?;
return Ok(())
}).unwrap();
server.fn_handler("/wifiscan",Method::Get, move |request| {
let mut response = request.into_ok_response()?;
let mut board = board_access.lock().unwrap();
match board.wifi_scan() {
Err(error) => {
response.write(format!("Error scanning wifi: {}", error).as_bytes())?;
},
Ok(scan_result) => {
println!("Scan result is {:?}", scan_result);
response.write("{ ssids:[".as_bytes())?;
let mut first = true;
for ap in scan_result.iter(){
if !first {
response.write(",".as_bytes())?;
}
response.write(ap.ssid.as_bytes())?;
first = false;
}
response.write("]".as_bytes())?;
},
}
return Ok(())
}).unwrap();
return server
}
pub fn httpd(board:&mut Box<PlantCtrlBoard<'static>>) -> Box<EspHttpServer<'static>> {
let mut server = shared();
server server
.fn_handler("/",Method::Get, move |request| { .fn_handler("/",Method::Get, move |request| {
let mut response = request.into_ok_response()?; let mut response = request.into_ok_response()?;
match initial_config { response.write(include_bytes!("config.html"))?;
true => response.write(include_bytes!("initial_config.html"))?,
false => response.write(include_bytes!("config.html"))?
};
return Ok(()) return Ok(())
}).unwrap(); }).unwrap();
return server;
}
pub fn shared() -> Box<EspHttpServer<'static>> {
let mut server = Box::new(EspHttpServer::new(&Default::default()).unwrap());
server server
.fn_handler("/version",Method::Get, |request| { .fn_handler("/version",Method::Get, |request| {
let mut response = request.into_ok_response()?; let mut response = request.into_ok_response()?;
@ -78,9 +119,6 @@ pub fn httpd(initial_config:bool) -> EspHttpServer<'static> {
println!("changing boot partition"); println!("changing boot partition");
finalizer.set_as_boot_partition().unwrap(); finalizer.set_as_boot_partition().unwrap();
finalizer.restart(); finalizer.restart();
//return Ok(())
}).unwrap(); }).unwrap();
return server; return server;
} }

View File

@ -0,0 +1,14 @@
function scanWifi(){
const req = new XMLHttpRequest();
req.addEventListener("progress", updateProgress);
req.addEventListener("load", wifiTransferComplete);
req.addEventListener("error", transferFailed);
req.addEventListener("abort", transferCanceled);
req.open("GET", "/wifiscan");
req.send();
}
function wifiTransferComplete(evt) {
console.log("The transfer is complete.");
}