Skip to content

Commit

Permalink
Add Wifi reconnect task
Browse files Browse the repository at this point in the history
  • Loading branch information
zargony committed Sep 9, 2024
1 parent 234e0b0 commit 941c20e
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 13 deletions.
3 changes: 2 additions & 1 deletion firmware/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ unsafe fn halt() -> ! {
}

#[main]
async fn main(_spawner: Spawner) {
async fn main(spawner: Spawner) {
let peripherals = Peripherals::take();
let system = SystemControl::new(peripherals.SYSTEM);
let clocks = ClockControl::boot_defaults(system.clock_control).freeze();
Expand Down Expand Up @@ -160,6 +160,7 @@ async fn main(_spawner: Spawner) {
peripherals.RADIO_CLK,
&clocks,
peripherals.WIFI,
spawner,
)
.await
{
Expand Down
60 changes: 48 additions & 12 deletions firmware/src/wifi.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,14 @@
use embassy_executor::{task, Spawner};
use embassy_time::{Duration, Timer};
use esp_hal::clock::Clocks;
use esp_hal::peripherals;
use esp_hal::rng::Rng;
use esp_wifi::wifi::{self, WifiController, WifiDevice, WifiStaDevice};
use esp_wifi::wifi::{self, WifiController, WifiDevice, WifiEvent, WifiStaDevice, WifiState};
use esp_wifi::{EspWifiInitFor, EspWifiInitialization, EspWifiTimerSource};
use log::{debug, info};
use log::{debug, info, warn};

/// Delay after disconnect or connection failure before trying to reconnect
const CONNECT_RETRY_DELAY: Duration = Duration::from_millis(5000);

/// Wifi initialization error
pub use esp_wifi::InitializationError;
Expand All @@ -15,7 +20,6 @@ pub use esp_wifi::InitializationError;
pub struct Wifi<'d> {
_init: EspWifiInitialization,
_device: WifiDevice<'d, WifiStaDevice>,
_controller: WifiController<'d>,
}

impl<'d> Wifi<'d> {
Expand All @@ -26,27 +30,59 @@ impl<'d> Wifi<'d> {
radio_clocks: peripherals::RADIO_CLK,
clocks: &Clocks<'d>,
wifi: peripherals::WIFI,
spawner: Spawner,
) -> Result<Self, InitializationError> {
debug!("Wifi: Initializing controller...");

debug!("Wifi: Static configuration: {:?}", esp_wifi::CONFIG);
let init = esp_wifi::initialize(EspWifiInitFor::Wifi, timer, rng, radio_clocks, clocks)?;

let (device, mut controller) = wifi::new_with_mode(&init, wifi, WifiStaDevice)?;
debug!("Wifi static configuration: {:?}", esp_wifi::CONFIG);
debug!("Wifi configuration: {:?}", controller.get_configuration());
debug!("Wifi capabilities: {:?}", controller.get_capabilities());
debug!("Wifi state: {:?}", wifi::get_wifi_state());
let client_config = Default::default();
let (device, mut controller) = wifi::new_with_config(&init, wifi, client_config)?;

debug!("Wifi: Starting controller...");
controller.start().await?;

info!("Wifi: Controller initialized");
debug!("Wifi state: {:?}", wifi::get_wifi_state());
debug!("Wifi: Starting connection task...");
match spawner.spawn(connection(controller)) {
Ok(()) => (),
// Panic on failure since failing to spawn a task indicates a serious error
Err(err) => panic!("Failed to spawn task: {:?}", err),
}

info!("Wifi: Controller initialized");
Ok(Self {
_init: init,
_device: device,
_controller: controller,
})
}
}

#[task]
async fn connection(mut controller: WifiController<'static>) -> ! {
debug!("Wifi: Start connection task");

loop {
match wifi::get_wifi_state() {
// If connected, wait for disconnect
WifiState::StaConnected => {
controller.wait_for_event(WifiEvent::StaDisconnected).await;
warn!("Wifi: Disconnected");
Timer::after(CONNECT_RETRY_DELAY).await;
}
// If disconnected, try to connect
WifiState::StaStarted | WifiState::StaDisconnected => {
info!("Wifi: Connecting...");
debug!("Wifi: Configuration: {:?}", controller.get_configuration());
match controller.connect().await {
Ok(()) => info!("Wifi: Connected"),
Err(err) => {
warn!("Wifi: Failed to connect: {:?}", err);
Timer::after(CONNECT_RETRY_DELAY).await;
}
}
}
// Any other state is unexpected and triggers a panic
state => panic!("Unexpected Wifi state {:?}", state),
}
}
}

0 comments on commit 941c20e

Please sign in to comment.