sntp and wifi sta mode working

This commit is contained in:
2025-09-26 00:44:40 +02:00
parent 336961f0a0
commit 6d5bb5b966
3 changed files with 155 additions and 79 deletions

View File

@@ -13,11 +13,11 @@ esp_bootloader_esp_idf::esp_app_desc!();
use esp_backtrace as _;
use crate::config::PlantConfig;
use crate::fat_error::FatResult;
use crate::hal::{esp_time, TIME_ACCESS};
use crate::log::LOG_ACCESS;
use crate::tank::{determine_tank_state, TankError, WATER_FROZEN_THRESH};
use crate::webserver::httpd;
use crate::fat_error::FatResult;
use crate::{
config::BoardVersion::INITIAL,
hal::{PlantHal, HAL, PLANT_COUNT},
@@ -43,6 +43,7 @@ use hal::battery::BatteryState;
use log::LogMessage;
use plant_state::PlantState;
use serde::{Deserialize, Serialize};
use smoltcp::socket::udp::PacketMetadata;
#[no_mangle]
extern "C" fn custom_halt() -> ! {
@@ -58,8 +59,8 @@ extern "C" fn custom_halt() -> ! {
}
//use tank::*;
mod fat_error;
mod config;
mod fat_error;
mod hal;
mod log;
mod plant_state;
@@ -227,8 +228,6 @@ async fn safe_main(spawner: Spawner) -> FatResult<()> {
info!("no mode override");
}
if (board.board_hal.get_config().hardware.board == INITIAL
&& board.board_hal.get_config().network.ssid.is_none())
{
@@ -253,14 +252,12 @@ async fn safe_main(spawner: Spawner) -> FatResult<()> {
NetworkMode::OFFLINE
};
if matches!(network_mode, NetworkMode::OFFLINE) && to_config {
info!("Could not connect to station and config mode forced, switching to ap mode!");
let res = {
let esp = board.board_hal.get_esp();
esp.wifi_ap(true).await
};
match res {
Ok(ap_stack) => {
@@ -271,20 +268,22 @@ async fn safe_main(spawner: Spawner) -> FatResult<()> {
}
}
let tz = & board.board_hal.get_config().timezone;
let timezone = match tz {
Some(tz_str) => tz_str.parse::<Tz>().unwrap_or_else(|_| {
info!("Invalid timezone '{}', falling back to UTC", tz_str);
UTC
}),
None => UTC, // Fallback to UTC if no timezone is set
};
let tz = &board.board_hal.get_config().timezone;
let timezone = match tz {
Some(tz_str) => tz_str.parse::<Tz>().unwrap_or_else(|_| {
info!("Invalid timezone '{}', falling back to UTC", tz_str);
UTC
}),
None => UTC, // Fallback to UTC if no timezone is set
};
let _timezone = Tz::UTC;
let timezone_time = cur.with_timezone(&timezone);
info!(
"Running logic at utc {} and {} {}",
cur, timezone.name(), timezone_time
cur,
timezone.name(),
timezone_time
);
if let NetworkMode::WIFI { ref ip_address, .. } = network_mode {
@@ -630,7 +629,6 @@ async fn safe_main(spawner: Spawner) -> FatResult<()> {
}
}
pub async fn do_secure_pump(
board: &mut MutexGuard<'_, CriticalSectionRawMutex, HAL<'_>>,
plant_id: usize,
@@ -643,21 +641,12 @@ pub async fn do_secure_pump(
let mut first_error = true;
let mut pump_time_s = 0;
if !dry_run {
board
.board_hal
.get_tank_sensor()?
.reset_flow_meter();
board
.board_hal
.get_tank_sensor()?
.start_flow_meter();
board.board_hal.get_tank_sensor()?.reset_flow_meter();
board.board_hal.get_tank_sensor()?.start_flow_meter();
board.board_hal.pump(plant_id, true).await?;
Timer::after_millis(10).await;
for step in 0..plant_config.pump_time_s as usize {
let flow_value = board
.board_hal
.get_tank_sensor()?
.get_flow_meter_value();
let flow_value = board.board_hal.get_tank_sensor()?.get_flow_meter_value();
let flow_value = 1;
flow_collector[step] = flow_value;
let flow_value_ml = flow_value as f32 * board.board_hal.get_config().tank.ml_per_pulse;
@@ -748,11 +737,8 @@ pub async fn do_secure_pump(
pump_time_s += 1;
}
}
board.board_hal.get_tank_sensor().unwrap().stop_flow_meter();
let final_flow_value = board
.board_hal
.get_tank_sensor()?
.get_flow_meter_value();
board.board_hal.get_tank_sensor().unwrap().stop_flow_meter();
let final_flow_value = board.board_hal.get_tank_sensor()?.get_flow_meter_value();
let final_flow_value = 12;
let flow_value_ml = final_flow_value as f32 * board.board_hal.get_config().tank.ml_per_pulse;
info!(
@@ -896,19 +882,23 @@ async fn publish_firmware_info(
let _ = esp.mqtt_publish("/state", "online".as_bytes()).await;
}
async fn try_connect_wifi_sntp_mqtt(board: &mut MutexGuard<'static, CriticalSectionRawMutex, HAL<'static>>, mut stack_store:Option<Stack<'_>>) -> NetworkMode {
async fn try_connect_wifi_sntp_mqtt(
board: &mut MutexGuard<'static, CriticalSectionRawMutex, HAL<'static>>,
mut stack_store: Option<Stack<'_>>,
) -> NetworkMode {
let nw_conf = &board.board_hal.get_config().network.clone();
match board.board_hal.get_esp().wifi(nw_conf).await {
Ok(stack) => {
stack_store = Some(stack);
stack_store = Some(stack.clone());
loop {
println!("wifi stuff");
Timer::after_millis(1000).await;
}
let sntp_mode: SntpMode = match board.board_hal.get_esp().sntp(1000 * 10).await {
let sntp_mode: SntpMode = match board
.board_hal
.get_esp()
.sntp(1000 * 10, stack.clone())
.await
{
Ok(new_time) => {
info!("Using time from sntp");
info!("Using time from sntp {}", new_time.to_rfc3339());
let _ = board.board_hal.get_rtc_module().set_rtc_time(&new_time);
SntpMode::SYNC { current: new_time }
}
@@ -918,6 +908,11 @@ async fn try_connect_wifi_sntp_mqtt(board: &mut MutexGuard<'static, CriticalSect
SntpMode::OFFLINE
}
};
loop {
println!("wifi stuff");
Timer::after_millis(1000).await;
}
let mqtt_connected = if board.board_hal.get_config().network.mqtt_url.is_some() {
let nw_config = &board.board_hal.get_config().network.clone();
match board.board_hal.get_esp().mqtt(nw_config).await {