From 6f23f9fb8cabc53378e512743d11dfca321cf2b5 Mon Sep 17 00:00:00 2001 From: memdmp Date: Sun, 18 May 2025 12:53:30 +0200 Subject: feat: final changes --- src/bin/server.rs | 116 +++++++++++++++++++++++++++++++++++++++--------------- 1 file changed, 84 insertions(+), 32 deletions(-) (limited to 'src/bin/server.rs') diff --git a/src/bin/server.rs b/src/bin/server.rs index a7ffa9e..ce95d62 100644 --- a/src/bin/server.rs +++ b/src/bin/server.rs @@ -1,11 +1,18 @@ -//! Using a static IP example -//! -//! - set SSID and PASSWORD env variable -//! - set STATIC_IP and GATEWAY_IP env variable (e.g. "192.168.2.191" / "192.168.2.1") -//! - might be necessary to configure your WiFi access point accordingly -//! - uses the given static IP -//! - responds with some HTML content when connecting to port 8080 -//! +// +// Space API HTTP Button ("dumbswitch") Main Entrypoint +// Copyright (C) 2025 memdmp +// +// This program is free software: you can redistribute it and/or modify it under the terms of the GNU Affero General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version. +// +// This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Affero General Public License for more details. +// +// You should have received a copy of the GNU Affero General Public License along with this program. If not, see . +// + +//! Production ESP is built with `cargo r --bin dumbswitch-as-server --features led-as-busy-led -r` +//! Debug ESP is built with `ESP_LOG=debug cargo r --bin dumbswitch-as-server --features led-as-busy-led` +//! To get logs, use while true; do if ls /dev/ | grep ttyACM >/dev/null 2>/dev/null; then echo 'Found Device'; cat /dev/ttyACM*; else sleep 0.001; fi; done +//! To change networks, copy ../network_data.example.rs to ../network_data.rs and change it's contents //% FEATURES: esp-wifi esp-wifi/wifi esp-wifi/utils esp-hal/unstable //% CHIPS: esp32 esp32s2 esp32s3 esp32c2 esp32c3 esp32c6 @@ -19,7 +26,7 @@ use core::str::FromStr; use blocking_network_stack::Stack; use critical_section::Mutex; -use dumbswitch::network_data; +use dumbswitch::{network_data, network_type::Network}; use embedded_io::*; use esp_alloc as _; use esp_backtrace as _; @@ -56,7 +63,7 @@ static BUTTON: Mutex>> = Mutex::new(RefCell::new(None)); static mut LED: OurOption>> = OurOption::None; fn set_open_led_state(val: bool) { - #[cfg(not(feature="led-as-busy-led"))] + #[cfg(not(feature = "led-as-busy-led"))] { let v = unsafe { #[allow(static_mut_refs)] @@ -75,7 +82,7 @@ fn set_open_led_state(val: bool) { // TODO: in future, add another LED for busy fn set_busy_led_state(val: bool) { - #[cfg(feature="led-as-busy-led")] + #[cfg(feature = "led-as-busy-led")] { let v = unsafe { #[allow(static_mut_refs)] @@ -106,8 +113,8 @@ fn main() -> ! { let mut io = Io::new(peripherals.IO_MUX); - // Set GPIO1 as an output, and set its state low initially. - let led = RefCell::new(Output::new(peripherals.GPIO1, Level::Low)); + // Set GPIO0 as an output, and set its state low initially. + let led = RefCell::new(Output::new(peripherals.GPIO0, Level::Low)); let led = unsafe { LED = OurOption::Some(led); #[allow(static_mut_refs)] @@ -117,8 +124,8 @@ fn main() -> ! { } }; - // Set GPIO4 as an input - let mut button = Input::new(peripherals.GPIO3, Pull::Down); + // Set GPIO5 as an input + let mut button = Input::new(peripherals.GPIO5, Pull::Down); info!("Waiting 500ms pre-init"); delay.delay_millis(500); @@ -142,8 +149,6 @@ fn main() -> ! { let mut stack = Stack::new(iface, device, socket_set, now, rng.random()); let client_config = Configuration::Client(ClientConfiguration { - ssid: network_data::SSID.try_into().unwrap(), - password: network_data::PASSWORD.try_into().unwrap(), ..Default::default() }); let res = controller.set_configuration(&client_config); @@ -154,14 +159,59 @@ fn main() -> ! { info!("Start Wifi Scan"); let res: Result<(heapless::Vec, usize), WifiError> = controller.scan_n(); + let mut net: Option = None; if let Ok((res, _count)) = res { for ap in res { - debug!("AP Info: {:?}", ap); + info!("Saw AP: {:?}", ap); + for ap_comp in network_data::NETWORKS { + if ap_comp.ssid == ap.ssid.clone() { + let client_config = Configuration::Client(ClientConfiguration { + ssid: ap_comp.ssid.try_into().unwrap(), + password: ap_comp.password.try_into().unwrap(), + ..Default::default() + }); + controller.set_configuration(&client_config); + net = Some(ap_comp); + }; + } + } + } + + if net.is_none() { + loop { + error!("Failed to connect to wifi: No saved network found."); + let mut high = false; + loop { + delay.delay_millis(1000); + high = !high; + if high { + led.borrow_mut().set_high(); + } else { + led.borrow_mut().set_low(); + } + } } } debug!("{:?}", controller.capabilities()); - debug!("wifi_connect {:?}", controller.connect()); + let connres = controller.connect(); + debug!("wifi_connect {:?}", connres); + if !connres.is_ok() { + error!("Failed to connect to wifi! {:#?}", connres.unwrap_err()); + + let mut high = false; + loop { + delay.delay_millis(2000); + high = !high; + if high { + led.borrow_mut().set_high(); + } else { + led.borrow_mut().set_low(); + } + } + } + + let net = net.unwrap(); // wait to get connected info!("Wait to get connected"); @@ -191,17 +241,15 @@ fn main() -> ! { } debug!("{:?}", controller.is_connected()); - info!("Setting static IP {}", network_data::STATIC_IP); + info!("Setting static IP {}", net.static_ip); stack .set_iface_configuration(&blocking_network_stack::ipv4::Configuration::Client( blocking_network_stack::ipv4::ClientConfiguration::Fixed( blocking_network_stack::ipv4::ClientSettings { - ip: blocking_network_stack::ipv4::Ipv4Addr::from(parse_ip(network_data::STATIC_IP)), + ip: blocking_network_stack::ipv4::Ipv4Addr::from(parse_ip(net.static_ip)), subnet: blocking_network_stack::ipv4::Subnet { - gateway: blocking_network_stack::ipv4::Ipv4Addr::from(parse_ip( - network_data::GATEWAY_IP, - )), + gateway: blocking_network_stack::ipv4::Ipv4Addr::from(parse_ip(net.gateway_ip)), mask: blocking_network_stack::ipv4::Mask(24), }, dns: None, @@ -222,7 +270,7 @@ fn main() -> ! { info!( "Start busy loop on main. Point your browser to http://{}:8080/", - network_data::STATIC_IP + net.static_ip ); let mut rx_buffer = [0u8; 1536]; @@ -265,13 +313,17 @@ fn main() -> ! { let r = socket.write_all( // 0b01100001 - true (open) - (if is_open { b"HTTP/1.0 200 OK\r\n\ + (if is_open { + b"HTTP/1.0 200 OK\r\n\ \r\n\ -a" } else { - // 0b01111010 - false (closed) - b"HTTP/1.0 200 OK\r\n\ +a" + } else { + // 0b01111010 - false (closed) + b"HTTP/1.0 200 OK\r\n\ \r\n\ -z" })); +z" + }), + ); if !r.is_ok() { error!("{:#?}", r.unwrap_err()); continue; @@ -279,7 +331,7 @@ z" })); let r = socket.flush(); if !r.is_ok() { - error!("{:#?}",r.unwrap_err()); + error!("{:#?}", r.unwrap_err()); continue; }; socket.work(); @@ -305,4 +357,4 @@ fn parse_ip(ip: &str) -> [u8; 4] { result[idx] = u8::from_str_radix(octet, 10).unwrap(); } result -} \ No newline at end of file +} -- cgit v1.2.3