Updated code to start up AP after 3 local network scans

This commit is contained in:
Luke Else 2023-07-02 18:12:19 +01:00
parent 8b2abd2f8d
commit 6786acdebc
2 changed files with 25 additions and 3 deletions

View File

@ -33,6 +33,7 @@ esp-idf-sys = { version = "0.33", default-features = false }
esp-idf-hal = { version = "0.41", optional = true, default-features = false }
esp-idf-svc = { version = "0.46", optional = true, default-features = false }
embedded-svc = { version = "0.25", optional = true, default-features = false }
heapless = "0.7.16"
[build-dependencies]
embuild = "0.31.2"

View File

@ -1,4 +1,4 @@
use embedded_svc::wifi::{Configuration, ClientConfiguration};
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;
@ -15,14 +15,19 @@ fn main() -> Result<(), EspError> {
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()?;
loop {
//Synchronously perform a scan and return nearby APs
// 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=======================================");
@ -31,6 +36,22 @@ fn main() -> Result<(), EspError> {
}
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 {