Created ESP32 app that scans local APs and reports signal strength.

This commit is contained in:
2023-06-19 21:57:53 +01:00
parent e161dbd881
commit 63b6627867
8 changed files with 127 additions and 31 deletions

49
src/main.rs Normal file
View File

@ -0,0 +1,49 @@
use embedded_svc::wifi::{Configuration, ClientConfiguration};
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;
// Enable WIFI Module
let mut wifi = EspWifi::new(modem, sysloop.clone()?, None)?;
wifi.set_configuration(&Configuration::Client(ClientConfiguration::default()))?;
wifi.start()?;
loop {
//Synchronously perform a scan and return nearby APs
let ap_scan_res = wifi.scan()?;
println!("\n\n=======================================");
for net in ap_scan_res.into_iter().map(|ap| (ap.ssid, ap.signal_strength, rssi_to_percentage(ap.signal_strength))) {
println!("{}: {}dBm, {:.2}%", net.0, net.1, net.2);
}
println!("=======================================");
}
}
fn rssi_to_percentage(signal_strength: i8) -> f32 {
let max_i8 = std::u8::MAX as f32;
let percentage = (((signal_strength as f32) / max_i8) + 0.5) * 100.0;
percentage
}
mod tests {
#[cfg(test)]
#[test]
fn rssi_to_percentage_test() {
use super::rssi_to_percentage;
println!("{}", rssi_to_percentage(0));
assert_eq!(rssi_to_percentage(0), 50.0);
}
}