initial webserver stub running

This commit is contained in:
2025-09-14 05:29:23 +02:00
parent be3c4a5095
commit b3cc313139
9 changed files with 959 additions and 726 deletions

View File

@@ -1,5 +1,5 @@
pub(crate) mod battery;
mod esp;
pub mod esp;
mod initial_hal;
mod rtc;
//mod water;
@@ -19,21 +19,34 @@ use alloc::boxed::Box;
use alloc::format;
use anyhow::{Ok, Result};
use async_trait::async_trait;
use core::cell::OnceCell;
use core::marker::PhantomData;
use embassy_executor::Spawner;
use core::net::Ipv4Addr;
use core::str::FromStr;
use embassy_executor::{SendSpawner, Spawner};
use embassy_net::tcp::{Error, TcpSocket};
use embassy_net::{IpListenEndpoint, Ipv4Cidr, Runner, Stack, StackResources, StaticConfigV4};
//use battery::BQ34Z100G1;
//use bq34z100::Bq34z100g1Driver;
use embassy_sync::blocking_mutex::raw::CriticalSectionRawMutex;
use embassy_sync::mutex::Mutex;
use embassy_sync::once_lock::OnceLock;
use embassy_time::{Duration, Timer};
use esp_bootloader_esp_idf::partitions::DataPartitionSubType;
use esp_hal::clock::CpuClock;
use esp_hal::gpio::{Input, InputConfig, Pull};
use esp_hal::timer::systimer::SystemTimer;
use esp_println::println;
use esp_println::{print, println};
use measurements::{Current, Voltage};
use embassy_sync::mutex::{Mutex, MutexGuard};
use esp_alloc as _;
use esp_backtrace as _;
use esp_hal::rng::Rng;
use esp_hal::timer::timg::TimerGroup;
use esp_wifi::wifi::{
AccessPointConfiguration, Configuration, WifiController, WifiDevice, WifiEvent, WifiState,
};
use esp_wifi::{init, EspWifiController};
//Only support for 8 right now!
pub const PLANT_COUNT: usize = 8;
@@ -42,16 +55,6 @@ const TANK_MULTI_SAMPLE: usize = 11;
//pub static I2C_DRIVER: LazyLock<Mutex<CriticalSectionRawMutex,I2cDriver<'static>>> = LazyLock::new(PlantHal::create_i2c);
// When you are okay with using a nightly compiler it's better to use https://docs.rs/static_cell/2.1.0/static_cell/macro.make_static.html
macro_rules! mk_static {
($t:ty,$val:expr) => {{
static STATIC_CELL: static_cell::StaticCell<$t> = static_cell::StaticCell::new();
#[deny(unused_attributes)]
let x = STATIC_CELL.uninit().write(($val));
x
}};
}
fn deep_sleep(_duration_in_ms: u64) -> ! {
//unsafe {
// //if we don't do this here, we might just revert newly flashed firmware
@@ -159,6 +162,17 @@ pub struct FreePeripherals {
// pub can: CAN,
}
macro_rules! mk_static {
($t:ty,$val:expr) => {{
static STATIC_CELL: static_cell::StaticCell<$t> = static_cell::StaticCell::new();
#[deny(unused_attributes)]
let x = STATIC_CELL.uninit().write(($val));
x
}};
}
const GW_IP_ADDR_ENV: Option<&'static str> = option_env!("GATEWAY_IP");
impl PlantHal {
// fn create_i2c() -> Mutex<CriticalSectionRawMutex, I2cDriver<'static>> {
// let peripherals = unsafe { Peripherals::new() };
@@ -176,19 +190,31 @@ impl PlantHal {
// Mutex::new(I2cDriver::new(i2c, sda, scl, &config).unwrap())
// }
pub fn create(spawner: Spawner) -> Result<Mutex<CriticalSectionRawMutex, HAL<'static>>> {
pub async fn create(spawner: Spawner) -> Result<Mutex<CriticalSectionRawMutex, HAL<'static>>> {
let config = esp_hal::Config::default().with_cpu_clock(CpuClock::max());
let peripherals = esp_hal::init(config);
esp_alloc::heap_allocator!(size: 64 * 1024);
let systimer = SystemTimer::new(peripherals.SYSTIMER);
esp_hal_embassy::init(systimer.alarm0);
let boot_button = Input::new(
peripherals.GPIO9,
InputConfig::default().with_pull(Pull::None),
);
let mut rng = Rng::new(peripherals.RNG);
let timg0 = TimerGroup::new(peripherals.TIMG0);
let esp_wifi_ctrl = &*mk_static!(
EspWifiController<'static>,
init(timg0.timer0, rng.clone()).unwrap()
);
let (mut controller, interfaces) =
esp_wifi::wifi::new(&esp_wifi_ctrl, peripherals.WIFI).unwrap();
use esp_hal::timer::systimer::SystemTimer;
esp_hal_embassy::init(systimer.alarm0);
//
// let free_pins = FreePeripherals {
// can: peripherals.can,
@@ -255,15 +281,16 @@ impl PlantHal {
let ota_state = ota.current_ota_state()?;
let mut esp = Esp {
rng,
controller: Some(controller),
interfaces: Some(interfaces),
boot_button,
mqtt_client: None,
storage,
slot: current.number(),
next_slot: current.next().number(),
ota_state,
dummy: PhantomData::default(),
wall_clock_offset: 0, // wifi_driver,
// boot_button
wall_clock_offset: 0,
};
//init,reset rtc memory depending on cause