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

@@ -56,6 +56,8 @@ embassy-net = { version = "0.7.1", default-features = false, features = [
"medium-ethernet", "medium-ethernet",
"tcp", "tcp",
"udp", "udp",
"proto-ipv4",
"dns"
] } ] }
embedded-io = "0.6.1" embedded-io = "0.6.1"
embedded-io-async = "0.6.1" embedded-io-async = "0.6.1"
@@ -93,6 +95,7 @@ smoltcp = { version = "0.12.0", default-features = false, features = [
"medium-ethernet", "medium-ethernet",
"multicast", "multicast",
"proto-dhcpv4", "proto-dhcpv4",
"proto-ipv6",
"proto-dns", "proto-dns",
"proto-ipv4", "proto-ipv4",
"socket-dns", "socket-dns",
@@ -118,7 +121,6 @@ ds323x = "0.6.0"
serde = { version = "1.0.219", features = ["derive", "alloc"], default-features = false } serde = { version = "1.0.219", features = ["derive", "alloc"], default-features = false }
serde_json = { version = "1.0.143", default-features = false, features = ["alloc"] } 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 = { 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"] } chrono-tz = { version = "0.10.4", default-features = false, features = ["filter-by-regex"] }
eeprom24x = "0.7.2" eeprom24x = "0.7.2"
@@ -128,8 +130,6 @@ unit-enum = "1.4.1"
pca9535 = { version = "2.0.0" } pca9535 = { version = "2.0.0" }
ina219 = { version = "0.2.0" } ina219 = { version = "0.2.0" }
embedded-storage = "=0.3.1" embedded-storage = "=0.3.1"
ekv = "1.0.0"
embedded-can = "0.4.1"
portable-atomic = "1.11.1" portable-atomic = "1.11.1"
embassy-sync = { version = "0.7.2", features = ["log"] } embassy-sync = { version = "0.7.2", features = ["log"] }
async-trait = "0.1.89" async-trait = "0.1.89"
@@ -145,6 +145,7 @@ bytemuck = { version = "1.23.2", features = ["derive", "min_const_generics", "po
deranged = "0.5.3" deranged = "0.5.3"
embassy-embedded-hal = "0.5.0" embassy-embedded-hal = "0.5.0"
bincode = { version = "2.0.1", default-features = false, features = ["derive"] } 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] [patch.crates-io]
#bq34z100 = { path = "../../bq34z100_rust" } #bq34z100 = { path = "../../bq34z100_rust" }

View File

@@ -5,19 +5,20 @@ use crate::log::{LogMessage, LOG_ACCESS};
use chrono::{DateTime, Utc}; use chrono::{DateTime, Utc};
use serde::Serialize; use serde::Serialize;
use crate::hal::little_fs2storage_adapter::LittleFs2Filesystem;
use crate::fat_error::{ContextExt, FatError, FatResult}; use crate::fat_error::{ContextExt, FatError, FatResult};
use crate::hal::little_fs2storage_adapter::LittleFs2Filesystem;
use alloc::borrow::ToOwned;
use alloc::string::ToString; use alloc::string::ToString;
use alloc::sync::Arc; use alloc::sync::Arc;
use alloc::{format, string::String, vec::Vec}; use alloc::{format, string::String, vec::Vec};
use alloc::borrow::ToOwned;
use core::marker::PhantomData; use core::marker::PhantomData;
use core::net::{IpAddr, Ipv4Addr}; use core::net::{IpAddr, Ipv4Addr, SocketAddr};
use core::str::FromStr; use core::str::FromStr;
use core::sync::atomic::Ordering; use core::sync::atomic::Ordering;
use core::sync::atomic::Ordering::Relaxed; use core::sync::atomic::Ordering::Relaxed;
use edge_dhcp::io::server::run; use edge_dhcp::io::server::run;
use embassy_executor::Spawner; use embassy_executor::Spawner;
use embassy_net::udp::UdpSocket;
use embassy_net::{DhcpConfig, Ipv4Cidr, Runner, Stack, StackResources, StaticConfigV4}; use embassy_net::{DhcpConfig, Ipv4Cidr, Runner, Stack, StackResources, StaticConfigV4};
use embassy_sync::blocking_mutex::raw::CriticalSectionRawMutex; use embassy_sync::blocking_mutex::raw::CriticalSectionRawMutex;
use embassy_sync::mutex::Mutex; use embassy_sync::mutex::Mutex;
@@ -31,11 +32,17 @@ use esp_hal::rtc_cntl::sleep::RtcSleepConfig;
use esp_hal::system::software_reset; use esp_hal::system::software_reset;
use esp_println::println; use esp_println::println;
use esp_storage::FlashStorage; 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::fs::Filesystem;
use littlefs2_core::{FileType, PathBuf, SeekFrom}; use littlefs2_core::{FileType, PathBuf, SeekFrom};
use log::info; use log::{info, warn};
use portable_atomic::AtomicBool; 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)] #[esp_hal::ram(rtc_fast, persistent)]
static mut LAST_WATERING_TIMESTAMP: [i64; PLANT_COUNT] = [0; PLANT_COUNT]; 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)] #[esp_hal::ram(rtc_fast, persistent)]
static mut RESTART_TO_CONF: i8 = 0; 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)] #[derive(Serialize, Debug)]
pub struct FileInfo { pub struct FileInfo {
@@ -72,6 +80,26 @@ pub struct MqttClient<'a> {
//mqtt_client: EspMqttClient<'a>, //mqtt_client: EspMqttClient<'a>,
base_topic: heapless::String<64>, base_topic: heapless::String<64>,
} }
#[derive(Copy, Clone, Default)]
struct Timestamp {
stamp: DateTime<Utc>,
}
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 struct Esp<'a> {
pub fs: Arc<Mutex<CriticalSectionRawMutex, Filesystem<'static, LittleFs2Filesystem>>>, pub fs: Arc<Mutex<CriticalSectionRawMutex, Filesystem<'static, LittleFs2Filesystem>>>,
pub rng: Rng, pub rng: Rng,
@@ -210,18 +238,58 @@ impl Esp<'_> {
pub(crate) fn mode_override_pressed(&mut self) -> bool { pub(crate) fn mode_override_pressed(&mut self) -> bool {
self.boot_button.is_low() self.boot_button.is_low()
} }
pub(crate) async fn sntp(&mut self, _max_wait_ms: u32) -> FatResult<DateTime<Utc>> {
//let sntp = sntp::EspSntp::new_default()?; pub(crate) async fn sntp(
//let mut counter = 0; &mut self,
//while sntp.get_sync_status() != SyncStatus::Completed { _max_wait_ms: u32,
// self.delay.delay_ms(100); stack: Stack<'_>,
// counter += 100; ) -> FatResult<DateTime<Utc>> {
// if counter > max_wait_ms { println!("start sntp");
// bail!("Reached sntp timeout, aborting") let mut rx_meta = [PacketMetadata::EMPTY; 16];
// } let mut rx_buffer = [0; 4096];
//} let mut tx_meta = [PacketMetadata::EMPTY; 16];
//self.time() let mut tx_buffer = [0; 4096];
todo!();
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<()> { pub async fn flash_ota(&mut self) -> FatResult<()> {
@@ -325,9 +393,6 @@ impl Esp<'_> {
..Default::default() ..Default::default()
}); });
println!("stop old");
self.controller.lock().await.stop()?;
self.controller self.controller
.lock() .lock()
.await .await
@@ -340,7 +405,6 @@ impl Esp<'_> {
println!("run dhcp"); println!("run dhcp");
spawner.spawn(run_dhcp(stack.clone(), gw_ip_addr_str)).ok(); spawner.spawn(run_dhcp(stack.clone(), gw_ip_addr_str)).ok();
loop { loop {
println!("waiting for wifi ap link up"); println!("waiting for wifi ap link up");
if stack.is_link_up() { if stack.is_link_up() {
@@ -352,9 +416,7 @@ impl Esp<'_> {
println!("waiting for wifi ap config up"); println!("waiting for wifi ap config up");
Timer::after(Duration::from_millis(100)).await Timer::after(Duration::from_millis(100)).await
} }
println!( println!("Connect to the AP `${ssid}` and point your browser to http://{gw_ip_addr_str}/");
"Connect to the AP `${ssid}` and point your browser to http://{gw_ip_addr_str}/"
);
stack stack
.config_v4() .config_v4()
.inspect(|c| println!("ipv4 config: {c:?}")); .inspect(|c| println!("ipv4 config: {c:?}"));
@@ -362,7 +424,10 @@ impl Esp<'_> {
Ok(stack.clone()) Ok(stack.clone())
} }
pub(crate) async fn wifi(&mut self, network_config: &NetworkConfig) -> FatResult<Stack<'static>> { pub(crate) async fn wifi(
&mut self,
network_config: &NetworkConfig,
) -> FatResult<Stack<'static>> {
esp_wifi::wifi_set_log_verbose(); esp_wifi::wifi_set_log_verbose();
let ssid = network_config.ssid.clone(); let ssid = network_config.ssid.clone();
match &ssid { match &ssid {
@@ -402,7 +467,7 @@ impl Esp<'_> {
let client_config = Configuration::Client(ClientConfiguration { let client_config = Configuration::Client(ClientConfiguration {
ssid, ssid,
bssid: None, bssid: None,
auth_method: AuthMethod::None, auth_method: AuthMethod::WPA2Personal, //FIXME read from config, fill via scan
password, password,
channel: None, channel: None,
}); });
@@ -415,9 +480,9 @@ impl Esp<'_> {
let timeout = TIME_ACCESS.get().await.current_time_us() + max_wait as u64 * 1000; let timeout = TIME_ACCESS.get().await.current_time_us() + max_wait as u64 * 1000;
loop { loop {
let state = esp_wifi::wifi::ap_state(); let state = esp_wifi::wifi::sta_state();
println!("waiting wifi sta ready {:?}", state); println!("waiting wifi sta ready {:?}", state);
match state{ match state {
WifiState::StaStarted => { WifiState::StaStarted => {
self.controller.lock().await.connect()?; self.controller.lock().await.connect()?;
break; break;
@@ -430,13 +495,29 @@ impl Esp<'_> {
WifiState::Invalid => {} WifiState::Invalid => {}
} }
if TIME_ACCESS.get().await.current_time_us() > timeout { 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() { while !stack.is_link_up() {
if TIME_ACCESS.get().await.current_time_us() > timeout { if TIME_ACCESS.get().await.current_time_us() > timeout {
bail!("Timeout waiting for wifi link up") 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)] #[embassy_executor::task(pool_size = 2)]
async fn net_task(mut runner: Runner<'static, WifiDevice<'static>>) { async fn net_task(mut runner: Runner<'static, WifiDevice<'static>>) {
runner.run().await runner.run().await

View File

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