-
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.
- Loading branch information
Showing
9 changed files
with
137 additions
and
4 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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,3 +2,4 @@ | |
/.embuild | ||
/target | ||
/Cargo.lock | ||
.env |
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
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 |
---|---|---|
@@ -1,3 +1,18 @@ | ||
fn main() { | ||
use std::env; | ||
|
||
fn main() -> anyhow::Result<()> { | ||
dotenv::dotenv().ok(); | ||
embuild::espidf::sysenv::output(); | ||
|
||
let wifi_ssid = env::var("WIFI_SSID").unwrap_or_else(|_| "internet".into()); | ||
let wifi_pass = env::var("WIFI_PASS").unwrap_or_else(|_| "password".into()); | ||
let server_port = env::var("SERVER_PORT").unwrap_or_else(|_| "8080".into()); | ||
|
||
println!("cargo:rustc-env=WIFI_SSID={wifi_ssid}"); | ||
println!("cargo:rustc-env=WIFI_PASS={wifi_pass}"); | ||
println!("cargo:rustc-env=SERVER_PORT={server_port}"); | ||
|
||
Ok(()) | ||
} | ||
|
||
// EOF |
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,3 @@ | ||
WIFI_SSID=WIFI | ||
WIFI_PASSWORD=PASSWORD | ||
SERVER_PORT=8080 |
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 |
---|---|---|
@@ -1 +1,2 @@ | ||
pub mod server; | ||
pub mod 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,16 +1,47 @@ | ||
use esp32_rust_example::*; | ||
use esp32_rust_example::{wifi::Wifi, *}; | ||
use esp_idf_svc::hal::task::block_on; | ||
|
||
use anyhow::Ok; | ||
use esp_idf_sys::esp_app_desc; | ||
use log::info; | ||
|
||
esp_app_desc!(); | ||
|
||
const WIFI_SSID: &str = env!("WIFI_SSID"); | ||
|
||
fn main() -> anyhow::Result<()> { | ||
esp_idf_svc::sys::link_patches(); | ||
|
||
esp_idf_svc::log::EspLogger::initialize_default(); | ||
|
||
log::info!("Hello, world!"); | ||
|
||
log::info!("WIFI_SSID: {}", WIFI_SSID); | ||
|
||
log::info!("Starting server..."); | ||
|
||
let mut wifi = Wifi::new()?; | ||
|
||
block_on(run(&mut wifi))?; | ||
|
||
info!("Shutting down in 5s..."); | ||
|
||
std::thread::sleep(core::time::Duration::from_secs(5)); | ||
|
||
Ok(()) | ||
} | ||
|
||
async fn run(wifi: &mut Wifi<'static>) -> anyhow::Result<()> { | ||
wifi.configure().await?; | ||
wifi.connect().await?; | ||
|
||
server::server().await?; | ||
|
||
/* loop { | ||
tokio::time::sleep(tokio::time::Duration::from_millis(1)).await; | ||
} | ||
wifi.disconnect().await?; | ||
*/ | ||
Ok(()) | ||
} |
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,64 @@ | ||
use embedded_svc::wifi::{AuthMethod, ClientConfiguration, Configuration}; | ||
use esp_idf_svc::eventloop::EspSystemEventLoop; | ||
use esp_idf_svc::hal::peripherals::Peripherals; | ||
use esp_idf_svc::nvs::EspDefaultNvsPartition; | ||
use esp_idf_svc::timer::EspTaskTimerService; | ||
use esp_idf_svc::wifi::{AsyncWifi, EspWifi}; | ||
use esp_idf_sys as _; | ||
use esp_idf_sys::EspError; | ||
use log::info; | ||
|
||
const WIFI_SSID: &str = env!("WIFI_SSID"); | ||
const WIFI_PASS: &str = env!("WIFI_PASS"); | ||
|
||
pub struct Wifi<'a> { | ||
wifi: AsyncWifi<EspWifi<'a>>, | ||
} | ||
|
||
impl<'a> Wifi<'a> { | ||
pub fn new() -> anyhow::Result<Self> { | ||
let peripherals = Peripherals::take()?; | ||
let sys_loop = EspSystemEventLoop::take()?; | ||
let timer_service = EspTaskTimerService::new()?; | ||
let nvs = EspDefaultNvsPartition::take()?; | ||
|
||
let wifi = AsyncWifi::wrap( | ||
EspWifi::new(peripherals.modem, sys_loop.clone(), Some(nvs))?, | ||
sys_loop, | ||
timer_service, | ||
)?; | ||
Ok(Self { wifi }) | ||
} | ||
|
||
pub async fn configure(&mut self) -> Result<(), EspError> { | ||
info!("Setting Wi-Fi credentials..."); | ||
|
||
let wifi_configuration: Configuration = Configuration::Client(ClientConfiguration { | ||
ssid: WIFI_SSID.try_into().unwrap(), | ||
bssid: None, | ||
auth_method: AuthMethod::WPAWPA2Personal, | ||
password: WIFI_PASS.try_into().unwrap(), | ||
channel: None, | ||
}); | ||
|
||
self.wifi.set_configuration(&wifi_configuration)?; | ||
|
||
info!("Starting Wi-Fi driver..."); | ||
self.wifi.start().await | ||
} | ||
|
||
pub async fn connect(&mut self) -> Result<(), EspError> { | ||
self.wifi.connect().await?; | ||
info!("Wifi connected"); | ||
|
||
self.wifi.wait_netif_up().await?; | ||
info!("Wifi netif up"); | ||
Ok(()) | ||
} | ||
|
||
pub async fn disconnect(&mut self) -> Result<(), EspError> { | ||
self.wifi.disconnect().await?; | ||
info!("Wifi disconnected"); | ||
Ok(()) | ||
} | ||
} |