improved wifi setup

This commit is contained in:
2025-10-27 15:23:06 +01:00
parent d63e9e964d
commit 9852534dc6
2 changed files with 35 additions and 18 deletions

View File

@@ -8,8 +8,7 @@ use esp_radio::wifi::{
};
use log::debug;
use static_cell::StaticCell;
static ESP_WIFI_CTRL: StaticCell<Controller<'static>> = StaticCell::new();
use thiserror::Error;
pub async fn set_antenna_mode(gpio3: GPIO3<'static>, gpio14: GPIO14<'static>) {
let mut rf_switch = Output::new(gpio3, esp_hal::gpio::Level::Low, OutputConfig::default());
@@ -23,29 +22,45 @@ pub async fn set_antenna_mode(gpio3: GPIO3<'static>, gpio14: GPIO14<'static>) {
antenna_mode.set_low();
}
pub fn setup_wifi<'d: 'static>(wifi: WIFI<'static>, spawner: Spawner) -> Interfaces<'d> {
let esp_wifi_ctrl = ESP_WIFI_CTRL.init(esp_radio::init().unwrap());
#[derive(Error, Debug)]
pub enum WifiError {
#[error("Failed to init radio")]
Init(#[from] esp_radio::InitializationError),
#[error("Failed to init wifi")]
Wifi(#[from] esp_radio::wifi::WifiError),
#[error("Failed to spawn wifi task")]
Spawn(#[from] embassy_executor::SpawnError),
}
pub fn setup_wifi<'d: 'static>(
wifi: WIFI<'static>,
spawner: Spawner,
) -> Result<Interfaces<'d>, WifiError> {
static ESP_WIFI_CTRL: StaticCell<Controller<'static>> = StaticCell::new();
let esp_wifi_ctrl = ESP_WIFI_CTRL.init(esp_radio::init()?);
let config = esp_radio::wifi::Config::default();
let (controller, interfaces) = esp_radio::wifi::new(esp_wifi_ctrl, wifi, config).unwrap();
let (controller, interfaces) = esp_radio::wifi::new(esp_wifi_ctrl, wifi, config)?;
spawner.must_spawn(connection(controller));
spawner.spawn(connection(controller))?;
interfaces
Ok(interfaces)
}
#[embassy_executor::task]
async fn connection(mut controller: WifiController<'static>) {
debug!("start connection task");
debug!("Device capabilities: {:?}", controller.capabilities());
loop {
match esp_radio::wifi::ap_state() {
WifiApState::Started => {
// wait until we're no longer connected
controller.wait_for_event(WifiEvent::ApStop).await;
Timer::after(Duration::from_millis(5000)).await
}
_ => {}
if esp_radio::wifi::ap_state() == WifiApState::Started {
// wait until we're no longer connected
controller.wait_for_event(WifiEvent::ApStop).await;
Timer::after(Duration::from_millis(5000)).await
}
if !matches!(controller.is_started(), Ok(true)) {
let client_config = ModeConfig::AccessPoint(
AccessPointConfig::default()