From 6786acdebcd9e7ccc8dd60accabc11d9f7d4b55e Mon Sep 17 00:00:00 2001 From: Luke Else Date: Sun, 2 Jul 2023 18:12:19 +0100 Subject: [PATCH] Updated code to start up AP after 3 local network scans --- Cargo.toml | 1 + src/main.rs | 27 ++++++++++++++++++++++++--- 2 files changed, 25 insertions(+), 3 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index abe7552..95e21a0 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -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" diff --git a/src/main.rs b/src/main.rs index b868ded..c8b32bd 100644 --- a/src/main.rs +++ b/src/main.rs @@ -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 {