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

@@ -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<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 fs: Arc<Mutex<CriticalSectionRawMutex, Filesystem<'static, LittleFs2Filesystem>>>,
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<DateTime<Utc>> {
//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<DateTime<Utc>> {
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<Stack<'static>> {
pub(crate) async fn wifi(
&mut self,
network_config: &NetworkConfig,
) -> FatResult<Stack<'static>> {
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