From 6d5bb5b96652bf7e73919e7d40510cd597de25ed Mon Sep 17 00:00:00 2001 From: Empire Phoenix Date: Fri, 26 Sep 2025 00:44:40 +0200 Subject: [PATCH] sntp and wifi sta mode working --- rust/Cargo.toml | 7 ++- rust/src/hal/esp.rs | 150 +++++++++++++++++++++++++++++++++----------- rust/src/main.rs | 77 +++++++++++------------ 3 files changed, 155 insertions(+), 79 deletions(-) diff --git a/rust/Cargo.toml b/rust/Cargo.toml index 6879782..12ee340 100644 --- a/rust/Cargo.toml +++ b/rust/Cargo.toml @@ -56,6 +56,8 @@ embassy-net = { version = "0.7.1", default-features = false, features = [ "medium-ethernet", "tcp", "udp", + "proto-ipv4", + "dns" ] } embedded-io = "0.6.1" embedded-io-async = "0.6.1" @@ -93,6 +95,7 @@ smoltcp = { version = "0.12.0", default-features = false, features = [ "medium-ethernet", "multicast", "proto-dhcpv4", + "proto-ipv6", "proto-dns", "proto-ipv4", "socket-dns", @@ -118,7 +121,6 @@ ds323x = "0.6.0" serde = { version = "1.0.219", features = ["derive", "alloc"], default-features = false } serde_json = { version = "1.0.143", default-features = false, features = ["alloc"] } -#timezone chrono = { version = "0.4.42", default-features = false, features = ["iana-time-zone", "alloc", "serde"] } chrono-tz = { version = "0.10.4", default-features = false, features = ["filter-by-regex"] } eeprom24x = "0.7.2" @@ -128,8 +130,6 @@ unit-enum = "1.4.1" pca9535 = { version = "2.0.0" } ina219 = { version = "0.2.0" } embedded-storage = "=0.3.1" -ekv = "1.0.0" -embedded-can = "0.4.1" portable-atomic = "1.11.1" embassy-sync = { version = "0.7.2", features = ["log"] } async-trait = "0.1.89" @@ -145,6 +145,7 @@ bytemuck = { version = "1.23.2", features = ["derive", "min_const_generics", "po deranged = "0.5.3" embassy-embedded-hal = "0.5.0" bincode = { version = "2.0.1", default-features = false, features = ["derive"] } +sntpc = { version = "0.6.0", default-features = false, features = ["log", "embassy-socket", "embassy-socket-ipv6"] } [patch.crates-io] #bq34z100 = { path = "../../bq34z100_rust" } diff --git a/rust/src/hal/esp.rs b/rust/src/hal/esp.rs index ffe01a0..8d86679 100644 --- a/rust/src/hal/esp.rs +++ b/rust/src/hal/esp.rs @@ -5,19 +5,20 @@ use crate::log::{LogMessage, LOG_ACCESS}; use chrono::{DateTime, Utc}; use serde::Serialize; -use crate::hal::little_fs2storage_adapter::LittleFs2Filesystem; use crate::fat_error::{ContextExt, FatError, FatResult}; +use crate::hal::little_fs2storage_adapter::LittleFs2Filesystem; +use alloc::borrow::ToOwned; use alloc::string::ToString; use alloc::sync::Arc; use alloc::{format, string::String, vec::Vec}; -use alloc::borrow::ToOwned; use core::marker::PhantomData; -use core::net::{IpAddr, Ipv4Addr}; +use core::net::{IpAddr, Ipv4Addr, SocketAddr}; use core::str::FromStr; use core::sync::atomic::Ordering; use core::sync::atomic::Ordering::Relaxed; use edge_dhcp::io::server::run; use embassy_executor::Spawner; +use embassy_net::udp::UdpSocket; use embassy_net::{DhcpConfig, Ipv4Cidr, Runner, Stack, StackResources, StaticConfigV4}; use embassy_sync::blocking_mutex::raw::CriticalSectionRawMutex; use embassy_sync::mutex::Mutex; @@ -31,11 +32,17 @@ use esp_hal::rtc_cntl::sleep::RtcSleepConfig; use esp_hal::system::software_reset; use esp_println::println; use esp_storage::FlashStorage; -use esp_wifi::wifi::{AccessPointConfiguration, AccessPointInfo, AuthMethod, ClientConfiguration, Configuration, Interfaces, ScanConfig, ScanTypeConfig, WifiController, WifiDevice, WifiState}; +use esp_wifi::wifi::{ + AccessPointConfiguration, AccessPointInfo, AuthMethod, ClientConfiguration, Configuration, + Interfaces, ScanConfig, ScanTypeConfig, WifiController, WifiDevice, WifiState, +}; use littlefs2::fs::Filesystem; use littlefs2_core::{FileType, PathBuf, SeekFrom}; -use log::info; +use log::{info, warn}; use portable_atomic::AtomicBool; +use smoltcp::socket::udp::PacketMetadata; +use smoltcp::wire::DnsQueryType; +use sntpc::{get_time, NtpContext, NtpTimestampGenerator}; #[esp_hal::ram(rtc_fast, persistent)] static mut LAST_WATERING_TIMESTAMP: [i64; PLANT_COUNT] = [0; PLANT_COUNT]; @@ -46,7 +53,8 @@ static mut LOW_VOLTAGE_DETECTED: i8 = 0; #[esp_hal::ram(rtc_fast, persistent)] static mut RESTART_TO_CONF: i8 = 0; -static CONFIG_FILE: &str = "config.json"; +const CONFIG_FILE: &str = "config.json"; +const NTP_SERVER: &str = "pool.ntp.org"; #[derive(Serialize, Debug)] pub struct FileInfo { @@ -72,6 +80,26 @@ pub struct MqttClient<'a> { //mqtt_client: EspMqttClient<'a>, base_topic: heapless::String<64>, } + +#[derive(Copy, Clone, Default)] +struct Timestamp { + stamp: DateTime, +} + +impl NtpTimestampGenerator for Timestamp { + fn init(&mut self) { + self.stamp = DateTime::default(); + } + + fn timestamp_sec(&self) -> u64 { + self.stamp.timestamp() as u64 + } + + fn timestamp_subsec_micros(&self) -> u32 { + self.stamp.timestamp_subsec_micros() + } +} + pub struct Esp<'a> { pub fs: Arc>>, pub rng: Rng, @@ -210,18 +238,58 @@ impl Esp<'_> { pub(crate) fn mode_override_pressed(&mut self) -> bool { self.boot_button.is_low() } - pub(crate) async fn sntp(&mut self, _max_wait_ms: u32) -> FatResult> { - //let sntp = sntp::EspSntp::new_default()?; - //let mut counter = 0; - //while sntp.get_sync_status() != SyncStatus::Completed { - // self.delay.delay_ms(100); - // counter += 100; - // if counter > max_wait_ms { - // bail!("Reached sntp timeout, aborting") - // } - //} - //self.time() - todo!(); + + pub(crate) async fn sntp( + &mut self, + _max_wait_ms: u32, + stack: Stack<'_>, + ) -> FatResult> { + println!("start sntp"); + let mut rx_meta = [PacketMetadata::EMPTY; 16]; + let mut rx_buffer = [0; 4096]; + let mut tx_meta = [PacketMetadata::EMPTY; 16]; + let mut tx_buffer = [0; 4096]; + + let mut socket = UdpSocket::new( + stack, + &mut rx_meta, + &mut rx_buffer, + &mut tx_meta, + &mut tx_buffer, + ); + socket.bind(123).unwrap(); + + let context = NtpContext::new(Timestamp::default()); + + let ntp_addrs = stack + .dns_query(NTP_SERVER, DnsQueryType::A) + .await + .expect("Failed to resolve DNS"); + if ntp_addrs.is_empty() { + bail!("Failed to resolve DNS"); + } + + let mut counter = 0; + loop { + let addr: IpAddr = ntp_addrs[0].into(); + let result = get_time(SocketAddr::from((addr, 123)), &socket, context).await; + + match result { + Ok(time) => { + info!("Time: {:?}", time); + return DateTime::from_timestamp(time.seconds as i64, 0) + .context("Could not convert Sntp result"); + } + Err(e) => { + warn!("Error: {:?}", e); + counter += 1; + if counter > 10 { + bail!("Failed to get time from NTP server"); + } + Timer::after(Duration::from_millis(100)).await; + } + } + } } pub async fn flash_ota(&mut self) -> FatResult<()> { @@ -325,9 +393,6 @@ impl Esp<'_> { ..Default::default() }); - println!("stop old"); - self.controller.lock().await.stop()?; - self.controller .lock() .await @@ -340,7 +405,6 @@ impl Esp<'_> { println!("run dhcp"); spawner.spawn(run_dhcp(stack.clone(), gw_ip_addr_str)).ok(); - loop { println!("waiting for wifi ap link up"); if stack.is_link_up() { @@ -352,9 +416,7 @@ impl Esp<'_> { println!("waiting for wifi ap config up"); Timer::after(Duration::from_millis(100)).await } - println!( - "Connect to the AP `${ssid}` and point your browser to http://{gw_ip_addr_str}/" - ); + println!("Connect to the AP `${ssid}` and point your browser to http://{gw_ip_addr_str}/"); stack .config_v4() .inspect(|c| println!("ipv4 config: {c:?}")); @@ -362,7 +424,10 @@ impl Esp<'_> { Ok(stack.clone()) } - pub(crate) async fn wifi(&mut self, network_config: &NetworkConfig) -> FatResult> { + pub(crate) async fn wifi( + &mut self, + network_config: &NetworkConfig, + ) -> FatResult> { esp_wifi::wifi_set_log_verbose(); let ssid = network_config.ssid.clone(); match &ssid { @@ -402,7 +467,7 @@ impl Esp<'_> { let client_config = Configuration::Client(ClientConfiguration { ssid, bssid: None, - auth_method: AuthMethod::None, + auth_method: AuthMethod::WPA2Personal, //FIXME read from config, fill via scan password, channel: None, }); @@ -415,9 +480,9 @@ impl Esp<'_> { let timeout = TIME_ACCESS.get().await.current_time_us() + max_wait as u64 * 1000; loop { - let state = esp_wifi::wifi::ap_state(); + let state = esp_wifi::wifi::sta_state(); println!("waiting wifi sta ready {:?}", state); - match state{ + match state { WifiState::StaStarted => { self.controller.lock().await.connect()?; break; @@ -430,13 +495,29 @@ impl Esp<'_> { WifiState::Invalid => {} } if TIME_ACCESS.get().await.current_time_us() > timeout { - bail!("Timeout waiting for wifi link up") + bail!("Timeout waiting for wifi sta ready") } + Timer::after(Duration::from_millis(500)).await; + } + loop { + let state = esp_wifi::wifi::sta_state(); + println!("waiting wifi sta connected {:?}", state); + match state { + WifiState::StaStarted => {} + WifiState::StaConnected => { + break; + } + WifiState::StaDisconnected => {} + WifiState::StaStopped => {} + WifiState::ApStarted => {} + WifiState::ApStopped => {} + WifiState::Invalid => {} + } + if TIME_ACCESS.get().await.current_time_us() > timeout { + bail!("Timeout waiting for wifi sta connected") + } + Timer::after(Duration::from_millis(500)).await; } - - - - while !stack.is_link_up() { if TIME_ACCESS.get().await.current_time_us() > timeout { bail!("Timeout waiting for wifi link up") @@ -838,7 +919,6 @@ async fn run_dhcp(stack: Stack<'static>, gw_ip_addr: &'static str) { } } - #[embassy_executor::task(pool_size = 2)] async fn net_task(mut runner: Runner<'static, WifiDevice<'static>>) { runner.run().await diff --git a/rust/src/main.rs b/rust/src/main.rs index b03c79a..31a2013 100644 --- a/rust/src/main.rs +++ b/rust/src/main.rs @@ -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::().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::().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>) -> NetworkMode { +async fn try_connect_wifi_sntp_mqtt( + board: &mut MutexGuard<'static, CriticalSectionRawMutex, HAL<'static>>, + mut stack_store: Option>, +) -> 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 {