Skip to content

Commit

Permalink
✨ adds wifi
Browse files Browse the repository at this point in the history
  • Loading branch information
chriamue committed Feb 4, 2024
1 parent 0f9f527 commit e705e7b
Show file tree
Hide file tree
Showing 9 changed files with 137 additions and 4 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,4 @@
/.embuild
/target
/Cargo.lock
.env
7 changes: 6 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -30,10 +30,13 @@ embassy = [
]

[dependencies]
anyhow = "1.0.79"
anyhow = "1"
axum = { version = "0.7", features = ["http1", "json"] }
esp-idf-sys = { version = "0.34", features = ["binstart"] }
esp-idf-svc = { version = "0.48", default-features = false }
embedded-svc = { version = "0.27", features = ["experimental"] }
embedded-hal = "1.0"

futures = "0.3"
log = { version = "0.4", default-features = false }
mio = { version = "0.8", features = ["log"] }
Expand All @@ -42,6 +45,8 @@ tokio = { version = "1", features = ["rt", "net", "io-util"] }
tower-http = { version = "0" }

[build-dependencies]
anyhow = "1"
dotenv = "0.15.0"
embuild = "0.31.3"

[dev-dependencies]
Expand Down
13 changes: 13 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,10 @@ If you get an error like:

Try to press the boot button on the ESP32 board and run the command again.

## Setup

Copy `example.env` to `.env` and adjust the settings.

## Building

```bash
Expand All @@ -58,3 +62,12 @@ cargo build
```bash
cargo espflash monitor
```

## License

[MIT](LICENSE)

## See also

- [Tokio running on esp32!](https://github.com/jasta/esp32-tokio-demo)
- [IRC bot with JSON API for esp32](https://github.com/sjm42/esp32ircbot)
17 changes: 16 additions & 1 deletion build.rs
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
3 changes: 3 additions & 0 deletions example.env
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
WIFI_SSID=WIFI
WIFI_PASSWORD=PASSWORD
SERVER_PORT=8080
1 change: 1 addition & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
pub mod server;
pub mod wifi;
33 changes: 32 additions & 1 deletion src/main.rs
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(())
}
2 changes: 1 addition & 1 deletion src/server.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use axum::{response::Html, routing::get, Router};
use log::info;
use tokio::net::TcpListener;

const TCP_LISTENING_PORT: u16 = 80;
const TCP_LISTENING_PORT: &str = env!("SERVER_PORT");

pub async fn server() -> anyhow::Result<()> {
let addr = format!("0.0.0.0:{TCP_LISTENING_PORT}");
Expand Down
64 changes: 64 additions & 0 deletions src/wifi.rs
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(())
}
}

0 comments on commit e705e7b

Please sign in to comment.