Own wifi #65
+1
-1
@@ -2,7 +2,7 @@
|
||||
runner = "espflash flash --monitor --chip esp32s3"
|
||||
|
||||
[env]
|
||||
ESP_LOG = "debug"
|
||||
ESP_LOG = "info"
|
||||
WM_CONN = '{"ssid": "Wokwi-GUEST", "psk": "", "data": {}}'
|
||||
|
||||
[build]
|
||||
|
||||
+6
-1
@@ -52,7 +52,11 @@ binary_serde = "1.0.25"
|
||||
static_assertions = { version = "1.1.0", default-features = false }
|
||||
embedded-text = "0.7.3"
|
||||
esp-nvs = { version = "0.5.0", features = ["esp32s3"] }
|
||||
esp-backtrace = { version = "0.19.0", features = ["esp32s3", "panic-handler", "println"] }
|
||||
esp-backtrace = { version = "0.19.0", features = [
|
||||
"esp32s3",
|
||||
"panic-handler",
|
||||
"println",
|
||||
] }
|
||||
esp-radio = { version = "0.18.0", features = ["esp32s3", "wifi"] }
|
||||
|
||||
[build-dependencies]
|
||||
@@ -82,6 +86,7 @@ opt-level = 's'
|
||||
[features]
|
||||
default = []
|
||||
wokwi = []
|
||||
nop_tertiary = []
|
||||
|
||||
[lints.clippy]
|
||||
manual_async_fn = "allow"
|
||||
|
||||
+14
-6
@@ -129,15 +129,24 @@ async fn main(spawner: Spawner) {
|
||||
.expect("failed to create nvs"),
|
||||
));
|
||||
|
||||
let wifi_store = WifiStore::new(nvs.clone()).await.expect("Could not initialize WiFi store");
|
||||
let init_credentials = Wifi::initialize_credentials(&wifi_store).await.expect("Could not get init WiFi credentials");
|
||||
let wifi = Wifi::new(esp_peripherals.WIFI, init_credentials).await.expect("Could not initialize WiFi");
|
||||
let wifi_store = WifiStore::new(nvs.clone())
|
||||
.await
|
||||
.expect("Could not initialize WiFi store");
|
||||
|
||||
let mut wifi = Wifi::new(esp_peripherals.WIFI).expect("Could not initialize WiFi");
|
||||
|
||||
wifi.connect_with_stored_credentials(&wifi_store)
|
||||
.await
|
||||
.expect("could not connect initially");
|
||||
|
||||
let card_store = CardStore::new(nvs.clone())
|
||||
.await
|
||||
.expect("Could not initialize Card Store");
|
||||
|
||||
let store = Store { card_store, wifi_store };
|
||||
let store = Store {
|
||||
card_store,
|
||||
wifi_store,
|
||||
};
|
||||
let rtc = Rtc::new(esp_peripherals.LPWR);
|
||||
|
||||
let peripherals = Box::leak(Box::new(Peripherals {
|
||||
@@ -191,8 +200,7 @@ async fn main(spawner: Spawner) {
|
||||
.expect("nfc scanner task failed"),
|
||||
);
|
||||
spawner.spawn(
|
||||
wait_for_wifi_credentials(outputs, peripherals)
|
||||
.expect("wifi credential task failed"),
|
||||
wait_for_wifi_credentials(outputs, peripherals).expect("wifi credential task failed"),
|
||||
);
|
||||
|
||||
core::future::pending::<()>().await
|
||||
|
||||
@@ -14,6 +14,14 @@ pub struct TertiaryDisplayPinConfiguration<'a> {
|
||||
pub i2c: I2cBus<'a>,
|
||||
}
|
||||
|
||||
#[cfg(feature = "nop_tertiary")]
|
||||
impl<'a> TertiaryDisplayPinConfiguration<'a> {
|
||||
pub fn build(self) -> Result<TertiaryDisplay<'a>, Self> {
|
||||
Ok(TertiaryDisplay { phantom: &() })
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(not(feature = "nop_tertiary"))]
|
||||
impl<'a> TertiaryDisplayPinConfiguration<'a> {
|
||||
pub fn build(self) -> Result<TertiaryDisplay<'a>, Self> {
|
||||
let device = AtomicDevice::new(self.i2c);
|
||||
@@ -61,19 +69,50 @@ impl<'a> core::fmt::Display for TertiaryDisplayWriteError<'a> {
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(not(feature = "nop_tertiary"))]
|
||||
pub struct TertiaryDisplay<'a> {
|
||||
display: TertiaryDisplayRaw<'a>,
|
||||
charset: CustomCharset,
|
||||
}
|
||||
|
||||
#[cfg(feature = "nop_tertiary")]
|
||||
pub struct TertiaryDisplay<'a> {
|
||||
phantom: &'a (),
|
||||
}
|
||||
|
||||
#[cfg(feature = "nop_tertiary")]
|
||||
impl<'a> TertiaryDisplay<'a> {
|
||||
pub const WIDTH: usize = 16;
|
||||
pub const ROWS: usize = 2;
|
||||
|
||||
pub fn inner_mut(&mut self) -> &mut TertiaryDisplayRaw<'a> {
|
||||
&mut self.display
|
||||
pub fn load_charset(&mut self, charset: CustomCharset) -> Result<(), TertiaryDisplayError<'a>> {
|
||||
log::info!("nop tertiary");
|
||||
Ok(())
|
||||
}
|
||||
|
||||
pub fn clear(&mut self) -> Result<(), TertiaryDisplayError<'a>> {
|
||||
log::info!("nop tertiary");
|
||||
Ok(())
|
||||
}
|
||||
|
||||
/// Writes text to the LCD. Normal strings wrap by default but this can be controlled
|
||||
/// via explicit [`Lines::checked`].
|
||||
pub fn write<'b, L: Into<Lines<'b>>>(
|
||||
&mut self,
|
||||
text: L,
|
||||
) -> Result<(), TertiaryDisplayWriteError<'a>> {
|
||||
log::info!("nop tertiary");
|
||||
log::info!("{:?}", text.into());
|
||||
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(not(feature = "nop_tertiary"))]
|
||||
impl<'a> TertiaryDisplay<'a> {
|
||||
pub const WIDTH: usize = 16;
|
||||
pub const ROWS: usize = 2;
|
||||
|
||||
pub fn load_charset(&mut self, charset: CustomCharset) -> Result<(), TertiaryDisplayError<'a>> {
|
||||
self.charset = charset;
|
||||
for (index, charmap) in self
|
||||
|
||||
+46
-35
@@ -1,8 +1,6 @@
|
||||
use crate::Mutex;
|
||||
use esp_radio::wifi::sta::StationConfig;
|
||||
use esp_radio::wifi::Config;
|
||||
use crate::alloc::string::ToString;
|
||||
use crate::Signal;
|
||||
use crate::alloc::string::ToString;
|
||||
use crate::navigation::outputs::Outputs;
|
||||
use crate::peripherals::Peripherals;
|
||||
#[cfg(not(feature = "wokwi"))]
|
||||
@@ -11,7 +9,9 @@ use crate::peripherals::storage::wifistore::WifiStoreError;
|
||||
use alloc::string::String;
|
||||
use core::error::Error;
|
||||
use core::fmt::Display;
|
||||
use esp_radio::wifi::Config;
|
||||
use esp_radio::wifi::WifiController;
|
||||
use esp_radio::wifi::sta::StationConfig;
|
||||
use esp_radio::*;
|
||||
|
||||
#[derive(Debug)]
|
||||
@@ -22,7 +22,6 @@ pub struct WifiCredentials {
|
||||
|
||||
pub static WIFI_CREDENTIALS: Signal<WifiCredentials> = Signal::new();
|
||||
|
||||
|
||||
#[derive(Debug)]
|
||||
pub struct Wifi {
|
||||
controller: WifiController<'static>,
|
||||
@@ -61,49 +60,49 @@ impl From<esp_radio::wifi::WifiError> for WifiError {
|
||||
}
|
||||
|
||||
impl Wifi {
|
||||
pub async fn new(
|
||||
wifi: esp_hal::peripherals::WIFI<'static>,
|
||||
credentials: Option<WifiCredentials>
|
||||
) -> Result<Self, WifiError> {
|
||||
pub fn new(wifi: esp_hal::peripherals::WIFI<'static>) -> Result<Self, WifiError> {
|
||||
log::info!("creating new wifi");
|
||||
let wifi = wifi::new(wifi, Default::default())?;
|
||||
|
||||
let mut instance = Self {
|
||||
Ok(Self {
|
||||
controller: wifi.0,
|
||||
interfaces: wifi.1
|
||||
};
|
||||
|
||||
if let Some(creds) = credentials {
|
||||
instance.set_station(creds).await?;
|
||||
}
|
||||
|
||||
Ok(instance)
|
||||
interfaces: wifi.1,
|
||||
})
|
||||
}
|
||||
|
||||
// get wifi credentials from env WIFI_SSID; WIFI_PASS (priority) or storage
|
||||
pub async fn initialize_credentials(
|
||||
pub async fn connect_with_stored_credentials(
|
||||
&mut self,
|
||||
wifi_store: &WifiStore,
|
||||
) -> Result<Option<WifiCredentials>, WifiError> {
|
||||
) -> Result<(), WifiError> {
|
||||
let option_stored_credentials = wifi_store.load_credentials().await?;
|
||||
|
||||
let env_ssid = option_env!("WIFI_SSID");
|
||||
let env_pass = option_env!("WIFI_PASS");
|
||||
|
||||
if env_ssid.is_none() && option_stored_credentials.is_none() {
|
||||
|
|
||||
log::info!("no initial wifi credentials");
|
||||
return Ok(None);
|
||||
log::info!("no initial wifi credentials. do not connect");
|
||||
return Ok(());
|
||||
}
|
||||
|
||||
let stored_credentials = option_stored_credentials.expect("checked value before");
|
||||
let stored_credentials = option_stored_credentials.unwrap_or(WifiCredentials {
|
||||
pass: "".to_string(),
|
||||
ssid: "".to_string(),
|
||||
});
|
||||
|
||||
|
ede1998
commented
This prevents passwordless wifi networks from working ;D This prevents passwordless wifi networks from working ;D
|
||||
let ssid = env_ssid.map_or(stored_credentials.ssid, |v| v.to_string());
|
||||
let pass = env_pass.map_or(stored_credentials.pass, |v| v.to_string());
|
||||
|
||||
log::info!("initial wifi credentials:\nssid:{ssid}, pass:{pass}");
|
||||
|
||||
Ok(Some(WifiCredentials {ssid, pass}))
|
||||
let credentials = WifiCredentials { ssid, pass };
|
||||
|
||||
self.set_station(credentials).await?;
|
||||
self.connect().await?;
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
pub async fn connect(&mut self) -> Result<(), WifiError>{
|
||||
pub async fn connect(&mut self) -> Result<(), WifiError> {
|
||||
log::info!("connecting wifi...");
|
||||
|
||||
let stationinfo = self.controller.connect_async().await?;
|
||||
@@ -114,12 +113,14 @@ impl Wifi {
|
||||
}
|
||||
|
||||
pub async fn set_station(&mut self, credentials: WifiCredentials) -> Result<(), WifiError> {
|
||||
let station_config = StationConfig::default().with_ssid(credentials.ssid).with_password(credentials.pass);
|
||||
self.controller.set_config(&Config::Station(station_config))?;
|
||||
let station_config = StationConfig::default()
|
||||
.with_ssid(credentials.ssid)
|
||||
.with_password(credentials.pass);
|
||||
self.controller
|
||||
.set_config(&Config::Station(station_config))?;
|
||||
Ok(())
|
||||
}
|
||||
|
||||
|
||||
#[cfg(feature = "wokwi")]
|
||||
pub async fn setup(
|
||||
wifi: esp_hal::peripherals::WIFI<'static>,
|
||||
@@ -159,16 +160,28 @@ impl Wifi {
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
#[embassy_executor::task]
|
||||
pub async fn wait_for_wifi_credentials(_outputs: &'static Mutex<Outputs>, peripherals: &'static Peripherals) {
|
||||
pub async fn wait_for_wifi_credentials(
|
||||
_outputs: &'static Mutex<Outputs>,
|
||||
peripherals: &'static Peripherals,
|
||||
) {
|
||||
loop {
|
||||
log::info!("Waiting for new WiFi credentials");
|
||||
let credentials = WIFI_CREDENTIALS.wait().await;
|
||||
|
||||
log::info!("Received new WiFi credentials:\nssid: {}, pass: {}", credentials.ssid, credentials.pass);
|
||||
if let Err(err) = peripherals.store.lock().await.wifi_store.save_credentials(&credentials).await {
|
||||
log::info!(
|
||||
"Received new WiFi credentials:\nssid: {}, pass: {}",
|
||||
credentials.ssid,
|
||||
credentials.pass
|
||||
);
|
||||
if let Err(err) = peripherals
|
||||
.store
|
||||
.lock()
|
||||
.await
|
||||
.wifi_store
|
||||
.save_credentials(&credentials)
|
||||
.await
|
||||
{
|
||||
log::error!("Error storing credentials: {err}");
|
||||
continue;
|
||||
}
|
||||
@@ -188,5 +201,3 @@ pub async fn wait_for_wifi_credentials(_outputs: &'static Mutex<Outputs>, periph
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
Reference in New Issue
Block a user
Nit: triple
/(///) for doc comments