3 Commits

3 changed files with 41 additions and 32 deletions

View File

@@ -47,6 +47,7 @@ use portable_atomic::AtomicBool;
use sntpc::{NtpContext, NtpTimestampGenerator, NtpUdpSocket, get_time}; use sntpc::{NtpContext, NtpTimestampGenerator, NtpUdpSocket, get_time};
use super::shared_flash::MutexFlashStorage; use super::shared_flash::MutexFlashStorage;
use crate::network::net_task;
#[esp_hal::ram(unstable(rtc_fast), unstable(persistent))] #[esp_hal::ram(unstable(rtc_fast), unstable(persistent))]
static mut LAST_WATERING_TIMESTAMP: [i64; PLANT_COUNT] = [0; PLANT_COUNT]; static mut LAST_WATERING_TIMESTAMP: [i64; PLANT_COUNT] = [0; PLANT_COUNT];
@@ -732,11 +733,6 @@ impl Esp<'_> {
} }
#[embassy_executor::task(pool_size = 2)]
async fn net_task(mut runner: Runner<'static, Interface<'static>>) {
runner.run().await;
}
#[embassy_executor::task] #[embassy_executor::task]
async fn run_dhcp(stack: Stack<'static>, ip: Ipv4Addr) { async fn run_dhcp(stack: Stack<'static>, ip: Ipv4Addr) {
use core::net::SocketAddrV4; use core::net::SocketAddrV4;

View File

@@ -68,6 +68,7 @@ mod fat_error;
mod hal; mod hal;
mod log; mod log;
mod mqtt; mod mqtt;
mod network;
mod plant_state; mod plant_state;
mod tank; mod tank;
mod webserver; mod webserver;
@@ -120,21 +121,7 @@ pub struct PumpResult {
pump_time_s: u16, pump_time_s: u16,
} }
#[derive(Serialize, Debug, PartialEq)]
enum SntpMode {
OFFLINE,
SYNC { current: DateTime<Utc> },
}
#[derive(Serialize, Debug, PartialEq)]
enum NetworkMode {
WIFI {
sntp: SntpMode,
mqtt: bool,
ip_address: String,
},
OFFLINE,
}
async fn safe_main(spawner: Spawner) -> FatResult<()> { async fn safe_main(spawner: Spawner) -> FatResult<()> {
info!("Startup Rust"); info!("Startup Rust");
@@ -224,10 +211,10 @@ async fn safe_main(spawner: Spawner) -> FatResult<()> {
info!("No wifi configured"); info!("No wifi configured");
//the current sensors require this amount to stabilize, in the case of Wi-Fi this is already handled due to connect timings; //the current sensors require this amount to stabilize, in the case of Wi-Fi this is already handled due to connect timings;
Timer::after_millis(100).await; Timer::after_millis(100).await;
NetworkMode::OFFLINE network::NetworkMode::OFFLINE
}; };
if matches!(network_mode, NetworkMode::OFFLINE) && to_config { if matches!(network_mode, network::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 = {
@@ -261,7 +248,7 @@ async fn safe_main(spawner: Spawner) -> FatResult<()> {
timezone_time timezone_time
); );
if let NetworkMode::WIFI { ref ip_address, .. } = network_mode { if let network::NetworkMode::WIFI { ref ip_address, .. } = network_mode {
publish_firmware_info(&mut board, version, ip_address, &timezone_time.to_rfc3339()).await; publish_firmware_info(&mut board, version, ip_address, &timezone_time.to_rfc3339()).await;
publish_battery_state(&mut board).await; publish_battery_state(&mut board).await;
let _ = publish_mppt_state(&mut board).await; let _ = publish_mppt_state(&mut board).await;
@@ -269,15 +256,15 @@ async fn safe_main(spawner: Spawner) -> FatResult<()> {
log( log(
LogMessage::StartupInfo, LogMessage::StartupInfo,
matches!(network_mode, NetworkMode::WIFI { .. }) as u32, matches!(network_mode, network::NetworkMode::WIFI { .. }) as u32,
matches!( matches!(
network_mode, network_mode,
NetworkMode::WIFI { network::NetworkMode::WIFI {
sntp: SntpMode::SYNC { .. }, sntp: network::SntpMode::SYNC { .. },
.. ..
} }
) as u32, ) as u32,
matches!(network_mode, NetworkMode::WIFI { mqtt: true, .. }) matches!(network_mode, network::NetworkMode::WIFI { mqtt: true, .. })
.to_string() .to_string()
.as_str(), .as_str(),
"", "",
@@ -750,13 +737,13 @@ async fn try_connect_wifi_sntp_mqtt(
board: &mut MutexGuard<'static, CriticalSectionRawMutex, HAL<'static>>, board: &mut MutexGuard<'static, CriticalSectionRawMutex, HAL<'static>>,
stack_store: &mut OptionLock<Stack<'static>>, stack_store: &mut OptionLock<Stack<'static>>,
spawner: Spawner, spawner: Spawner,
) -> NetworkMode { ) -> network::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, spawner).await { match board.board_hal.get_esp().wifi(nw_conf, spawner).await {
Ok(stack) => { Ok(stack) => {
stack_store.replace(stack); stack_store.replace(stack);
let sntp_mode: SntpMode = match board.board_hal.get_esp().sntp(1000 * 10, stack).await { let sntp_mode: network::SntpMode = match board.board_hal.get_esp().sntp(1000 * 10, stack).await {
Ok(new_time) => { Ok(new_time) => {
info!("Using time from sntp {}", new_time.to_rfc3339()); info!("Using time from sntp {}", new_time.to_rfc3339());
let _ = board let _ = board
@@ -764,12 +751,12 @@ async fn try_connect_wifi_sntp_mqtt(
.get_rtc_module() .get_rtc_module()
.set_rtc_time(&new_time) .set_rtc_time(&new_time)
.await; .await;
SntpMode::SYNC { current: new_time } network::SntpMode::SYNC { current: new_time }
} }
Err(err) => { Err(err) => {
warn!("sntp error: {err}"); warn!("sntp error: {err}");
board.board_hal.general_fault(true).await; board.board_hal.general_fault(true).await;
SntpMode::OFFLINE network::SntpMode::OFFLINE
} }
}; };
@@ -797,7 +784,7 @@ async fn try_connect_wifi_sntp_mqtt(
None => String::from("No IP"), None => String::from("No IP"),
}, },
}; };
NetworkMode::WIFI { network::NetworkMode::WIFI {
sntp: sntp_mode, sntp: sntp_mode,
mqtt: mqtt_connected, mqtt: mqtt_connected,
ip_address: ip, ip_address: ip,
@@ -806,7 +793,7 @@ async fn try_connect_wifi_sntp_mqtt(
Err(err) => { Err(err) => {
info!("Offline mode due to {err}"); info!("Offline mode due to {err}");
board.board_hal.general_fault(true).await; board.board_hal.general_fault(true).await;
NetworkMode::OFFLINE network::NetworkMode::OFFLINE
} }
} }
} }

26
rust/src/network.rs Normal file
View File

@@ -0,0 +1,26 @@
use alloc::string::String;
use chrono::{DateTime, Utc};
use embassy_net::Runner;
use esp_radio::wifi::Interface;
use serde::Serialize;
#[derive(Serialize, Debug, PartialEq)]
pub enum SntpMode {
OFFLINE,
SYNC { current: DateTime<Utc> },
}
#[derive(Serialize, Debug, PartialEq)]
pub enum NetworkMode {
WIFI {
sntp: SntpMode,
mqtt: bool,
ip_address: String,
},
OFFLINE,
}
#[embassy_executor::task(pool_size = 2)]
pub(crate) async fn net_task(mut runner: Runner<'static, Interface<'static>>) {
runner.run().await;
}