startup and ota state detection working, now wifi_ap next

This commit is contained in:
2025-09-13 20:56:11 +02:00
parent 4160202cdc
commit be3c4a5095
9 changed files with 294 additions and 225 deletions

View File

@@ -6,14 +6,14 @@ use anyhow::{anyhow, bail, Context};
use chrono::{DateTime, Utc};
use serde::Serialize;
use alloc::{
string::{String, ToString},
vec::Vec,
};
use alloc::{string::String, vec::Vec};
use core::marker::PhantomData;
use core::net::IpAddr;
use core::str::FromStr;
use embassy_time::{Instant, Timer};
use embassy_time::Instant;
use esp_bootloader_esp_idf::ota::OtaImageState;
use esp_hal::gpio::Input;
use esp_storage::FlashStorage;
#[link_section = ".rtc.data"]
static mut LAST_WATERING_TIMESTAMP: [i64; PLANT_COUNT] = [0; PLANT_COUNT];
@@ -51,11 +51,16 @@ pub struct MqttClient<'a> {
base_topic: heapless::String<64>,
}
pub struct Esp<'a> {
pub boot_button: Input<'a>,
pub(crate) mqtt_client: Option<MqttClient<'a>>,
pub(crate) dummy: PhantomData<&'a ()>,
pub(crate) wall_clock_offset: u64
pub(crate) wall_clock_offset: u64,
//pub(crate) wifi_driver: EspWifi<'a>,
//pub(crate) boot_button: PinDriver<'a, esp_idf_hal::gpio::AnyIOPin, esp_idf_hal::gpio::Input>,
pub storage: FlashStorage,
pub slot: usize,
pub next_slot: usize,
pub ota_state: OtaImageState,
}
pub struct IpInfo {
@@ -72,10 +77,9 @@ impl Esp<'_> {
const BASE_PATH: &'static str = "/spiffs";
pub(crate) fn mode_override_pressed(&mut self) -> bool {
todo!();
//self.boot_button.get_level() == Level::Low
self.boot_button.is_low()
}
pub(crate) async fn sntp(&mut self, max_wait_ms: u32) -> anyhow::Result<DateTime<Utc>> {
pub(crate) async fn sntp(&mut self, _max_wait_ms: u32) -> anyhow::Result<DateTime<Utc>> {
//let sntp = sntp::EspSntp::new_default()?;
//let mut counter = 0;
//while sntp.get_sync_status() != SyncStatus::Completed {
@@ -147,10 +151,11 @@ impl Esp<'_> {
}
pub(crate) async fn wifi_ap(&mut self) -> anyhow::Result<()> {
let ssid = match self.load_config() {
let _ssid = match self.load_config() {
Ok(config) => config.network.ap_ssid.clone(),
Err(_) => heapless::String::from_str("PlantCtrl Emergency Mode").unwrap(),
};
todo!("todo");
//
// let apconfig = AccessPointConfiguration {
@@ -165,15 +170,13 @@ impl Esp<'_> {
// anyhow::Ok(())
}
pub(crate) async fn wifi(&mut self, network_config: &NetworkConfig) -> anyhow::Result<IpInfo> {
let ssid = network_config
let _ssid = network_config
.ssid
.clone()
.ok_or(anyhow!("No ssid configured"))?;
let password = network_config.password.clone();
let max_wait = network_config.max_wait;
let _password = network_config.password.clone();
let _max_wait = network_config.max_wait;
bail!("todo")
// match password {
// Some(pw) => {
@@ -237,7 +240,7 @@ impl Esp<'_> {
}
pub(crate) async fn save_config(
&mut self,
config: &PlantControllerConfig,
_config: &PlantControllerConfig,
) -> anyhow::Result<()> {
bail!("todo");
// let mut cfg = File::create(Self::CONFIG_FILE)?;
@@ -247,9 +250,9 @@ impl Esp<'_> {
}
pub(crate) fn mount_file_system(&mut self) -> anyhow::Result<()> {
bail!("fail");
log(LogMessage::MountingFilesystem, 0, 0, "", "");
let base_path = String::try_from("/spiffs")?;
let storage = String::try_from(Self::SPIFFS_PARTITION_NAME)?;
// log(LogMessage::MountingFilesystem, 0, 0, "", "");
// let base_path = String::try_from("/spiffs")?;
// let storage = String::try_from(Self::SPIFFS_PARTITION_NAME)?;
//let conf = todo!();
//let conf = esp_idf_sys::esp_vfs_spiffs_conf_t {
@@ -272,7 +275,7 @@ impl Esp<'_> {
// &free_space.used_size.to_string(),
// "",
// );
anyhow::Ok(())
// anyhow::Ok(())
}
async fn file_system_size(&mut self) -> anyhow::Result<FileSystemSizeInfo> {
bail!("fail");
@@ -348,7 +351,7 @@ impl Esp<'_> {
// iter_error,
// }
}
pub(crate) async fn delete_file(&self, filename: &str) -> anyhow::Result<()> {
pub(crate) async fn delete_file(&self, _filename: &str) -> anyhow::Result<()> {
bail!("todo");
// let filepath = Path::new(Self::BASE_PATH).join(Path::new(filename));
// match fs::remove_file(filepath) {
@@ -425,7 +428,7 @@ impl Esp<'_> {
if base_topic.is_empty() {
bail!("Mqtt base_topic was empty")
}
let base_topic_copy = base_topic.clone();
let _base_topic_copy = base_topic.clone();
let mqtt_url = network_config
.mqtt_url
.as_ref()
@@ -583,8 +586,8 @@ impl Esp<'_> {
}
pub(crate) async fn mqtt_publish(
&mut self,
subtopic: &str,
message: &[u8],
_subtopic: &str,
_message: &[u8],
) -> anyhow::Result<()> {
bail!("todo");
//