Skip to main content

wifiauto1/
wifiauto1.rs

1#![allow(missing_docs)]
2//! Minimal WiFiAuto example based on the struct docs.
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 core::convert::Infallible;
13use device_envoy::{
14    Result,
15    button::PressedTo,
16    flash_array::FlashArray,
17    wifi_auto::{WifiAuto, WifiAutoEvent},
18};
19use embassy_net::dns::DnsQueryType;
20use embassy_time::{Duration, Timer};
21
22#[embassy_executor::main]
23async fn main(spawner: embassy_executor::Spawner) -> ! {
24    let err = inner_main(spawner).await.unwrap_err();
25    core::panic!("{err}");
26}
27
28async fn inner_main(spawner: embassy_executor::Spawner) -> Result<Infallible> {
29    let p = embassy_rp::init(Default::default());
30
31    let [wifi_flash] = FlashArray::<1>::new(p.FLASH)?;
32
33    let wifi_auto = WifiAuto::new(
34        p.PIN_23,  // CYW43 power
35        p.PIN_24,  // CYW43 clock
36        p.PIN_25,  // CYW43 chip select
37        p.PIN_29,  // CYW43 data
38        p.PIO0,    // WiFi PIO
39        p.DMA_CH0, // WiFi DMA
40        wifi_flash,
41        p.PIN_13, // Button for reconfiguration
42        PressedTo::Ground,
43        "PicoAccess", // Captive-portal SSID
44        [],           // Any extra fields
45        spawner,
46    )?;
47
48    let (stack, _button) = wifi_auto
49        .connect(|event| async move {
50            match event {
51                WifiAutoEvent::CaptivePortalReady => {
52                    defmt::info!("Captive portal ready");
53                }
54                WifiAutoEvent::Connecting { .. } => {
55                    defmt::info!("Connecting to WiFi");
56                }
57                WifiAutoEvent::ConnectionFailed => {
58                    defmt::info!("WiFi connection failed");
59                }
60            }
61            Ok(())
62        })
63        .await?;
64
65    // The stack is ready for network operations (for example, NTP or HTTP).
66    defmt::info!("WiFi connected");
67
68    loop {
69        if let Ok(addresses) = stack.dns_query("google.com", DnsQueryType::A).await {
70            defmt::info!("google.com: {:?}", addresses);
71        } else {
72            defmt::info!("google.com: lookup failed");
73        }
74        Timer::after(Duration::from_secs(15)).await;
75    }
76}