Skip to content

Commit

Permalink
Create WiFi module to connect to network
Browse files Browse the repository at this point in the history
  • Loading branch information
arosspope committed Oct 1, 2023
1 parent 95aa654 commit 0d0a7c0
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 9 deletions.
20 changes: 11 additions & 9 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,9 @@ use led::{RGB8, WS2812RMT};
mod dot_display;
use dot_display::DotDisplay;

mod wifi;
use wifi::Wifi;

#[toml_cfg::toml_config]
pub struct Config {
#[default("")]
Expand All @@ -21,29 +24,28 @@ pub struct Config {
wifi_psk: &'static str,
}


fn main() -> Result<()> {
// 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();

let peripherals = Peripherals::take().unwrap();

// The constant CONFIG is auto-generated by toml_config
let app_config = CONFIG;
let mut led = WS2812RMT::new(peripherals.pins.gpio18, peripherals.rmt.channel0)?;
led.set_pixel(RGB8::new(50, 50, 0))?;

// Associate to network and obtain DHCP IP
info!("Loading with credentials, ssid:{:?} psk:{:?}", app_config.wifi_ssid, app_config.wifi_psk);
let mut wifi = Wifi::init(peripherals.modem, &app_config.wifi_ssid, &app_config.wifi_psk);
Wifi::start(&mut wifi);
let ip = wifi.sta_netif().get_ip_info().unwrap();
info!("IP info: {:?}", ip);

// Setup for the MAX7219 display
// let data = peripherals.pins.gpio0.into();
// let cs = peripherals.pins.gpio1.into();
// let sck = peripherals.pins.gpio2.into();
// let display = MAX7219::from_pins(1, data, cs, sck).unwrap();

// let mut dp = DotDisplay::from(display).unwrap();
let data = PinDriver::output(peripherals.pins.gpio0).unwrap();
let cs = PinDriver::output(peripherals.pins.gpio1).unwrap();
let sck = PinDriver::output(peripherals.pins.gpio2).unwrap();
Expand All @@ -52,7 +54,7 @@ fn main() -> Result<()> {

let mut seed = 0;

dp.turn_on_display()?;
dp.turn_on_display().expect("Failed to turn on display");
loop {
let mut input: [u8; 8] = [0, 0, 0, 0, 0, 0, 0, 0];

Expand All @@ -70,6 +72,6 @@ fn main() -> Result<()> {
led.set_pixel(RGB8::new(0, 50, 0))?; // Green
}

std::thread::sleep(std::time::Duration::from_millis(50));
std::thread::sleep(std::time::Duration::from_secs(1));
}
}
45 changes: 45 additions & 0 deletions src/wifi.rs
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:
}
}

0 comments on commit 0d0a7c0

Please sign in to comment.