From c085c1ab360750b5522d1fa8fde0e0ed5c5aeb53 Mon Sep 17 00:00:00 2001 From: andromeda Date: Mon, 6 Apr 2026 21:51:13 +0200 Subject: [PATCH] initialize wifi two-way --- .gitignore | 1 + flake.nix | 5 +++++ src/lib.rs | 31 +++++++++++++++++++++++++++++++ src/main.rs | 48 +++++++++++++++++++++++------------------------- 4 files changed, 60 insertions(+), 25 deletions(-) diff --git a/.gitignore b/.gitignore index 6963297..9e789b7 100644 --- a/.gitignore +++ b/.gitignore @@ -1,2 +1,3 @@ target result* +hook.sh diff --git a/flake.nix b/flake.nix index 9d42c39..ae542ea 100644 --- a/flake.nix +++ b/flake.nix @@ -39,6 +39,11 @@ rustc = toolchain; }).buildPackage { src = ./.; + + # set these to build the project + ACCESS_POINT_SSID = null; + ACCESS_POINT_PASS = null; + # UPDATE .cargo/config.toml to reflect changes here in the runner postInstall = '' mv $out/bin/esp32c6-play $out/bin/.esp32c6-play diff --git a/src/lib.rs b/src/lib.rs index a721c77..a7006ad 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -1,10 +1,14 @@ #![no_std] +extern crate alloc; +use alloc::string::String; +use core::env; use esp_hal::{ clock::CpuClock, time::{Duration, Instant}, }; use esp_hal_smartled::SmartLedsAdapter; +use esp_radio::wifi; use smart_leds::{RGB, SmartLedsWrite as _, brightness}; // default brightness level of RGB led, used by write_rgb @@ -38,6 +42,33 @@ pub fn init() -> esp_hal::peripherals::Peripherals { peripherals } +pub fn mk_wifi_config() -> wifi::ModeConfig { + let access_point_config = wifi::AccessPointConfig::default() + .with_auth_method(wifi::AuthMethod::WpaWpa2Personal) + .with_ssid(String::from("esp32c6")) + .with_password(String::from("password")); + let client_config = wifi::ClientConfig::default() + .with_auth_method(wifi::AuthMethod::WpaWpa2Personal) + .with_ssid(String::from(env!("ACCESS_POINT_SSID"))) + .with_password(String::from(env!("ACCESS_POINT_PASS"))); + wifi::ModeConfig::ApSta(client_config, access_point_config) +} + +pub fn connect_wifi( + wifi_controller: &mut wifi::WifiController, + wifi_config: &wifi::ModeConfig, +) -> () { + wifi_controller + .set_config(wifi_config) + .expect("failed to set wifi config"); + wifi_controller + .start() + .expect("failed to start wifi controller"); + wifi_controller + .connect() + .expect("failed to attempt to connect wifi controller to access point"); +} + // used the site https://deepbluembedded.com/sine-lookup-table-generator-calculator/ to generate const SIN_LOOKUP_U8: [u8; 256] = [ 128, 131, 134, 137, 140, 143, 146, 149, 152, 155, 158, 162, 165, 167, 170, 173, 176, 179, 182, diff --git a/src/main.rs b/src/main.rs index de13de8..dd764c8 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,8 +1,7 @@ #![no_std] #![no_main] -extern crate alloc; -use alloc::string::String; +use core::env; use esp_backtrace as _; // panic_handler use esp_hal::{ interrupt::software::SoftwareInterruptControl, rmt::Rmt, time::Rate, timer::timg::TimerGroup, @@ -11,7 +10,7 @@ use esp_hal_smartled::{SmartLedsAdapter, smart_led_buffer}; use esp_radio::wifi; use smart_leds::RGB; -use esp32c6_play::{init, sin, wait_ms, write_rgb}; +use esp32c6_play::{connect_wifi, init, mk_wifi_config, sin, wait_ms, write_rgb}; // This creates a default app-descriptor required by the esp-idf bootloader. // For more information see: @@ -25,40 +24,39 @@ fn main() -> ! { // initialize heap esp_alloc::heap_allocator!(size: 64 * 1024); - // initialize wifi + // initialize esp-rtos as scheduler for esp-radio let timg0 = TimerGroup::new(peripherals.TIMG0); let software_interrupt = SoftwareInterruptControl::new(peripherals.SW_INTERRUPT); esp_rtos::start(timg0.timer0, software_interrupt.software_interrupt0); - let esp_radio_controller = esp_radio::init().expect("failed to init radio controller"); - let (mut wifi_controller, _) = wifi::new( - &esp_radio_controller, - peripherals.WIFI, - wifi::Config::default(), - ) - .expect("failed to create wifi controller"); - let wifi_config = wifi::ModeConfig::AccessPoint( - wifi::AccessPointConfig::default() - .with_auth_method(wifi::AuthMethod::WpaWpa2Personal) - .with_ssid(String::from("ssid-test")) - .with_password(String::from("password")), - ); - wifi_controller - .set_config(&wifi_config) - .expect("failed to set wifi config"); + // initialize wifi + let radio_controller = esp_radio::init().expect("failed to init radio controller"); + let (mut wifi_controller, _) = + wifi::new(&radio_controller, peripherals.WIFI, wifi::Config::default()) + .expect("failed to init wifi controller"); - wifi_controller - .start() - .expect("failed to start wifi controller"); + let wifi_config = mk_wifi_config(); + connect_wifi(&mut wifi_controller, &wifi_config); - // set up rgb led + // initialize rgb led let mut led_buffer = smart_led_buffer!(1 /* # of leds */); let mut rgb_led = { let frequency = Rate::from_mhz(80); // max frequency of RMT - let rmt = Rmt::new(peripherals.RMT, frequency).expect("failed to initialize RMT0"); + let rmt = Rmt::new(peripherals.RMT, frequency).expect("failed to initialize rmt"); SmartLedsAdapter::new(rmt.channel0, peripherals.GPIO8, &mut led_buffer) }; + // red led and waiting message + write_rgb(&mut rgb_led, RGB::new(255, 20, 20)); + log::info!("connecting..."); + while !wifi_controller + .is_connected() + .expect("failed to check connection status") + { + wait_ms(100); + } + log::info!("connected to the access point"); + loop { for i in 0..255 { let r: u8 = i;