From 923e7e0a59e2a3c2268b1f6b18b586489f580346 Mon Sep 17 00:00:00 2001 From: Priec Date: Tue, 30 Sep 2025 22:56:11 +0200 Subject: [PATCH] esp sa teraz pripaja na wifi --- test1/.cargo/config.toml | 2 -- test1/.env_temp | 2 ++ test1/.gitignore | 1 + test1/Cargo.lock | 7 +++++++ test1/Cargo.toml | 2 ++ test1/Makefile | 9 +++++++++ test1/build.rs | 13 +++++++++++++ test1/src/bin/main.rs | 28 +++++++++++++++++++++++----- 8 files changed, 57 insertions(+), 7 deletions(-) create mode 100644 test1/.env_temp create mode 100644 test1/Makefile diff --git a/test1/.cargo/config.toml b/test1/.cargo/config.toml index c1f0e2f..a50cb3d 100644 --- a/test1/.cargo/config.toml +++ b/test1/.cargo/config.toml @@ -3,8 +3,6 @@ runner = "espflash flash --monitor --chip esp32" [env] ESP_LOG="info" -SSID = "nazov_wifi" -PASSWORD = "heslo" [build] rustflags = [ diff --git a/test1/.env_temp b/test1/.env_temp new file mode 100644 index 0000000..b23f039 --- /dev/null +++ b/test1/.env_temp @@ -0,0 +1,2 @@ +SSID = "nazov_wifi_siete" +PASSWORD = "heslo_od_wifi" diff --git a/test1/.gitignore b/test1/.gitignore index 234ae30..ea52893 100644 --- a/test1/.gitignore +++ b/test1/.gitignore @@ -4,6 +4,7 @@ target/ .vscode/ .zed/ .helix/ +.env # These are backup files generated by rustfmt **/*.rs.bk diff --git a/test1/Cargo.lock b/test1/Cargo.lock index ab360fc..4f446f0 100644 --- a/test1/Cargo.lock +++ b/test1/Cargo.lock @@ -234,6 +234,12 @@ dependencies = [ "litrs", ] +[[package]] +name = "dotenvy" +version = "0.15.7" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1aaf95b3e5c8f23aa320147307562d361db0ae0d51242340f558153b4eb2439b" + [[package]] name = "embassy-embedded-hal" version = "0.3.2" @@ -1388,6 +1394,7 @@ name = "test1" version = "0.1.0" dependencies = [ "critical-section", + "dotenvy", "embassy-executor", "embassy-net", "embassy-time 0.4.0", diff --git a/test1/Cargo.toml b/test1/Cargo.toml index 8eed50e..f967823 100644 --- a/test1/Cargo.toml +++ b/test1/Cargo.toml @@ -67,6 +67,8 @@ static_cell = "2.1.1" rust-mqtt = { version = "0.3.0", default-features = false, features = ["no_std"] } heapless = "0.9.1" +[build-dependencies] +dotenvy = "0.15.7" [profile.dev] # Rust debug is too slow. diff --git a/test1/Makefile b/test1/Makefile new file mode 100644 index 0000000..2845b25 --- /dev/null +++ b/test1/Makefile @@ -0,0 +1,9 @@ +.PHONY: all build flash + +all: build flash + +build: + cargo build --release + +flash: + cargo espflash flash --release --monitor diff --git a/test1/build.rs b/test1/build.rs index a76b496..09afabe 100644 --- a/test1/build.rs +++ b/test1/build.rs @@ -1,4 +1,17 @@ fn main() { + // load .env and pass SSID/PASSWORD to compiler + if let Ok(dotenv_path) = dotenvy::dotenv() { + // Only rebuild if .env changes + println!("cargo:rerun-if-changed={}", dotenv_path.display()); + } + + if let Ok(ssid) = std::env::var("SSID") { + println!("cargo:rustc-env=SSID={}", ssid); + } + if let Ok(password) = std::env::var("PASSWORD") { + println!("cargo:rustc-env=PASSWORD={}", password); + } + linker_be_nice(); // make sure linkall.x is the last linker script (otherwise might cause problems with flip-link) println!("cargo:rustc-link-arg=-Tlinkall.x"); diff --git a/test1/src/bin/main.rs b/test1/src/bin/main.rs index 505abe5..9ba430c 100644 --- a/test1/src/bin/main.rs +++ b/test1/src/bin/main.rs @@ -12,6 +12,7 @@ use esp_backtrace as _; use esp_hal::clock::CpuClock; use esp_hal::timer::timg::TimerGroup; use log::info; +use esp_wifi::wifi::{ClientConfiguration, Configuration, WifiController}; extern crate alloc; @@ -19,6 +20,10 @@ extern crate alloc; // For more information see: esp_bootloader_esp_idf::esp_app_desc!(); +// Pull SSID/PASSWORD from .cargo/config.toml at compile time +const SSID: &str = env!("SSID"); +const PASSWORD: &str = env!("PASSWORD"); + #[esp_hal_embassy::main] async fn main(spawner: Spawner) { // generator version: 0.5.0 @@ -39,15 +44,28 @@ async fn main(spawner: Spawner) { let timer1 = TimerGroup::new(peripherals.TIMG0); let wifi_init = esp_wifi::init(timer1.timer0, rng).expect("Failed to initialize WIFI/BLE controller"); - let (mut _wifi_controller, _interfaces) = esp_wifi::wifi::new(&wifi_init, peripherals.WIFI) + let (mut controller, _interfaces) = esp_wifi::wifi::new(&wifi_init, peripherals.WIFI) .expect("Failed to initialize WIFI controller"); - // TODO: Spawn some tasks - let _ = spawner; + let client_conf = Configuration::Client(ClientConfiguration { + ssid: SSID.try_into().unwrap(), + password: PASSWORD.try_into().unwrap(), + ..Default::default() + }); + controller.set_configuration(&client_conf).unwrap(); + info!("Starting WiFi..."); + controller.start_async().await.unwrap(); + info!("WiFi started, connecting..."); + + match controller.connect_async().await { + Ok(_) => info!("Connected to WiFi!"), + Err(e) => info!("Failed to connect: {:?}", e), + } + + // keep task alive loop { - info!("Hello world!"); - Timer::after(Duration::from_secs(1)).await; + Timer::after(Duration::from_secs(5)).await; } // for inspiration have a look at the examples at https://github.com/esp-rs/esp-hal/tree/esp-hal-v1.0.0-rc.0/examples/src/bin