//! Example of using async wifi. Almost identical to `wifi_async` but also proves that `AsyncWifi` can be sent between tasks and can be `'static`. //! //! Add your own ssid and password use core::convert::TryInto; use embedded_svc::wifi::{AuthMethod, ClientConfiguration, Configuration}; use esp_idf_svc::hal::prelude::Peripherals; use esp_idf_svc::hal::task::block_on; use esp_idf_svc::log::EspLogger; use esp_idf_svc::timer::EspTaskTimerService; use esp_idf_svc::wifi::{AsyncWifi, EspWifi}; use esp_idf_svc::{eventloop::EspSystemEventLoop, nvs::EspDefaultNvsPartition}; use log::info; const SSID: &str = env!("WIFI_SSID"); const PASSWORD: &str = env!("WIFI_PASS"); fn main() -> anyhow::Result<()> { esp_idf_svc::sys::link_patches(); EspLogger::initialize_default(); let wifi = block_on_send(connect_wifi())?; let ip_info = wifi.wifi().sta_netif().get_ip_info()?; info!("Wifi DHCP info: {:?}", ip_info); info!("Shutting down in 5s..."); std::thread::sleep(core::time::Duration::from_secs(5)); Ok(()) } fn block_on_send(f: F) -> F::Output where F: core::future::Future + Send + 'static, // These constraints are why this additional example exists in the first place { block_on(f) } async fn connect_wifi() -> anyhow::Result>> { let peripherals = Peripherals::take()?; let sys_loop = EspSystemEventLoop::take()?; let timer_service = EspTaskTimerService::new()?; let nvs = EspDefaultNvsPartition::take()?; let mut wifi = AsyncWifi::wrap( EspWifi::new(peripherals.modem, sys_loop.clone(), Some(nvs))?, sys_loop, timer_service, )?; let wifi_configuration: Configuration = Configuration::Client(ClientConfiguration { ssid: SSID.try_into().unwrap(), bssid: None, auth_method: AuthMethod::WPA2Personal, password: PASSWORD.try_into().unwrap(), channel: None, ..Default::default() }); wifi.set_configuration(&wifi_configuration)?; wifi.start().await?; info!("Wifi started"); wifi.connect().await?; info!("Wifi connected"); wifi.wait_netif_up().await?; info!("Wifi netif up"); Ok(wifi) }