use embedded_svc::wifi::{Configuration, ClientConfiguration, AccessPointConfiguration, AuthMethod}; use esp_idf_svc::{wifi::EspWifi, eventloop::EspSystemEventLoop}; use esp_idf_sys::{self as _, EspError}; // If using the `binstart` feature of `esp-idf-sys`, always keep this module imported use esp_idf_hal::prelude::Peripherals; fn main() -> Result<(), EspError> { // It is necessary to call this function once. Otherwise some patches to the runtime // implemented by esp-idf-sys might not link properly. See https://github.com/esp-rs/esp-idf-template/issues/71 esp_idf_sys::link_patches(); // Bind the log crate to the ESP Logging facilities esp_idf_svc::log::EspLogger::initialize_default(); // Get Peripherals and sysloop ready let sysloop = EspSystemEventLoop::take(); let peripherals = Peripherals::take().unwrap(); let modem = peripherals.modem; // Get AP SSID and Password and compile time const SSID: &str = env!("WIFI-SSID"); const PWD: &str= env!("WIFI-PASSWORD"); // Enable WIFI Module let mut wifi = EspWifi::new(modem, sysloop.clone()?, None)?; wifi.set_configuration(&Configuration::Client(ClientConfiguration::default()))?; wifi.start()?; // Scan and print nearby local networks; for _ in 0..3 { // Synchronously perform a scan and return nearby APs let ap_scan_res = wifi.scan()?; let nets = ap_scan_res.into_iter().map(|ap| (ap.ssid, ap.signal_strength, rssi_to_percentage(ap.signal_strength))); println!("\n\n======================================="); for net in nets { println!("{}: {}dBm, {:.2}% Signal Strength", net.0, net.1, net.2); } println!("======================================="); } // Disable the WIFI module and change configuration to AP wifi.stop()?; let mut ap_config = AccessPointConfiguration::default(); ap_config.ssid = heapless::String::<32>::from(SSID); ap_config.auth_method = AuthMethod::WPA2Personal; ap_config.password = heapless::String::<64>::from(PWD); wifi.set_configuration(&Configuration::AccessPoint(ap_config))?; wifi.start()?; loop { println!("{:?}", wifi.is_up()); std::thread::sleep(std::time::Duration::from_millis(100)); } } fn rssi_to_percentage(rssi: i8) -> f32 { const MIN_RSSI: i8 = -100; // Minimum RSSI value in dBm const MAX_RSSI: i8 = -5; // Maximum RSSI value in dBm // Clamp the RSSI value within the specified range let clamped_rssi = rssi.min(MAX_RSSI).max(MIN_RSSI); // Convert the clamped RSSI to a percentage let percentage = 100.0 * (clamped_rssi - MIN_RSSI) as f32 / (MAX_RSSI - MIN_RSSI) as f32; //Return the percentage RSSI percentage }