Skip to main content

wifiauto3/
wifiauto3.rs

1#![allow(missing_docs)]
2//! WifiAuto example with a custom website field for DNS lookups.
3
4#![cfg(feature = "wifi")]
5#![no_std]
6#![no_main]
7#![allow(clippy::future_not_send, reason = "single-threaded")]
8
9extern crate defmt_rtt as _;
10extern crate panic_probe as _;
11
12use device_envoy::{
13    Error, Result,
14    button::PressedTo,
15    flash_array::FlashArray,
16    wifi_auto::fields::{TextField, TextFieldStatic, TimezoneField, TimezoneFieldStatic},
17    wifi_auto::{WifiAuto, WifiAutoEvent},
18};
19
20#[embassy_executor::main]
21async fn main(spawner: embassy_executor::Spawner) -> ! {
22    let err = inner_main(spawner).await.unwrap_err();
23    core::panic!("{err}");
24}
25
26async fn inner_main(spawner: embassy_executor::Spawner) -> Result<core::convert::Infallible> {
27    let p = embassy_rp::init(Default::default());
28
29    let [wifi_flash, website_flash, timezone_flash] = FlashArray::<3>::new(p.FLASH)?;
30
31    static WEBSITE_STATIC: TextFieldStatic<32> = TextField::new_static();
32    let website_field = TextField::new(
33        &WEBSITE_STATIC,
34        website_flash,
35        "website",
36        "Website",
37        "google.com",
38    );
39
40    // Create timezone field
41    static TIMEZONE_STATIC: TimezoneFieldStatic = TimezoneField::new_static();
42    let timezone_field = TimezoneField::new(&TIMEZONE_STATIC, timezone_flash);
43
44    let wifi_auto = WifiAuto::new(
45        p.PIN_23,  // CYW43 power
46        p.PIN_24,  // CYW43 clock
47        p.PIN_25,  // CYW43 chip select
48        p.PIN_29,  // CYW43 data
49        p.PIO0,    // WiFi PIO
50        p.DMA_CH0, // WiFi DMA
51        wifi_flash,
52        p.PIN_13, // Button for reconfiguration
53        PressedTo::Ground,
54        "PicoAccess",                    // Captive-portal SSID
55        [website_field, timezone_field], // Custom fields
56        spawner,
57    )?;
58
59    let (stack, _button) = wifi_auto
60        .connect(|event| async move {
61            match event {
62                WifiAutoEvent::CaptivePortalReady => {
63                    defmt::info!("Captive portal ready");
64                }
65                WifiAutoEvent::Connecting {
66                    try_index,
67                    try_count,
68                } => {
69                    defmt::info!(
70                        "Connecting to WiFi (attempt {} of {})...",
71                        try_index + 1,
72                        try_count
73                    );
74                }
75                WifiAutoEvent::ConnectionFailed => {
76                    defmt::info!("WiFi connection failed");
77                }
78            }
79            Ok(())
80        })
81        .await?;
82
83    let website = website_field.text()?.unwrap_or_default();
84    let offset_minutes = timezone_field
85        .offset_minutes()?
86        .ok_or(Error::MissingCustomWifiAutoField)?;
87    defmt::info!("Timezone offset minutes: {}", offset_minutes);
88
89    loop {
90        let query_name = website.as_str();
91        if let Ok(addresses) = stack
92            .dns_query(query_name, embassy_net::dns::DnsQueryType::A)
93            .await
94        {
95            defmt::info!("{}: {:?}", query_name, addresses);
96        } else {
97            defmt::info!("{}: lookup failed", query_name);
98        }
99
100        embassy_time::Timer::after(embassy_time::Duration::from_secs(15)).await;
101    }
102}