-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Create WiFi module to connect to network
- Loading branch information
Showing
2 changed files
with
56 additions
and
9 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
use embedded_svc::wifi::{AuthMethod, ClientConfiguration, Configuration, Wifi as SvcWifi}; | ||
use esp_idf_hal::modem::Modem; | ||
use esp_idf_svc::{eventloop::EspSystemEventLoop, nvs::EspDefaultNvsPartition, wifi::EspWifi}; | ||
|
||
pub struct Wifi; | ||
|
||
impl Wifi { | ||
pub fn init(modem: Modem, ssid: &'static str, psk: &'static str) -> EspWifi<'static> { | ||
let mut wifi_driver = EspWifi::new( | ||
modem, | ||
EspSystemEventLoop::take().expect("Failed to take system event loop"), | ||
Some(EspDefaultNvsPartition::take().expect("Failed to take default nvs partition")), | ||
) | ||
.expect("Failed to create esp wifi device"); | ||
|
||
wifi_driver | ||
.set_configuration(&Configuration::Client(ClientConfiguration { | ||
// See .cargo/config.toml to set WIFI_SSID and WIFI_PWD env variables | ||
ssid: ssid.into(), | ||
password: psk.into(), | ||
auth_method: AuthMethod::WPA2Personal, | ||
..Default::default() | ||
})) | ||
.expect("Failed to set wifi driver configuration"); | ||
|
||
wifi_driver | ||
} | ||
|
||
pub fn start(wifi_driver: &mut EspWifi<'_>) {// TODO: Make it a result type -> Result<()> { | ||
wifi_driver.start().expect("Failed to start wifi driver"); | ||
wifi_driver.is_started().expect("Failed to start wifi driver"); | ||
wifi_driver.connect().expect("Failed to initiate connection"); | ||
|
||
for _ in 0..10 { | ||
if wifi_driver.is_connected().unwrap() { | ||
// Ok(()); | ||
break; | ||
} | ||
|
||
std::thread::sleep(std::time::Duration::from_secs(1)); | ||
} | ||
|
||
// Ok(()) // TODO: | ||
} | ||
} |