Created
December 9, 2022 14:29
-
-
Save rajeshpachaikani/2ef7c11b4d7e8a6704dba0f323bcdd6d to your computer and use it in GitHub Desktop.
RUST - ESP32-C3 Connect to WiFi
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
// Dependencies | |
// esp-idf-sys = { version = "0.31.11", features = ["binstart"] } | |
// esp-idf-svc = "0.43.4" | |
// esp-idf-hal = "0.39.3" | |
// embedded-hal = "0.2.7" | |
// embedded-svc = "0.23.1" | |
use std::{ | |
thread::sleep, | |
time::Duration, | |
}; | |
use esp_idf_sys as _; | |
use esp_idf_hal::{ | |
peripherals::Peripherals, | |
}; | |
use esp_idf_svc::{ | |
wifi::EspWifi, | |
nvs::EspDefaultNvsPartition, | |
eventloop::EspSystemEventLoop, | |
}; | |
use embedded_svc::wifi::{ClientConfiguration, Wifi, Configuration}; | |
fn main(){ | |
esp_idf_sys::link_patches();//Needed for esp32-rs | |
println!("Entered Main function!"); | |
let peripherals = Peripherals::take().unwrap(); | |
let sys_loop = EspSystemEventLoop::take().unwrap(); | |
let nvs = EspDefaultNvsPartition::take().unwrap(); | |
let mut wifi_driver = EspWifi::new( | |
peripherals.modem, | |
sys_loop, | |
Some(nvs) | |
).unwrap(); | |
wifi_driver.set_configuration(&Configuration::Client(ClientConfiguration{ | |
ssid: "YOUR_SSID".into(), | |
password: "YOUR_PASSWORD".into(), | |
..Default::default() | |
})).unwrap(); | |
wifi_driver.start().unwrap(); | |
wifi_driver.connect().unwrap(); | |
while !wifi_driver.is_connected().unwrap(){ | |
let config = wifi_driver.get_configuration().unwrap(); | |
println!("Waiting for station {:?}", config); | |
} | |
println!("Should be connected now"); | |
loop{ | |
println!("IP info: {:?}", wifi_driver.sta_netif().get_ip_info().unwrap()); | |
sleep(Duration::new(10,0)); | |
} | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment