Module wifi

Source
Expand description

Perform common core WiFi functions such as join a network, resolve a DNS hostname, etc.

§Usage

let mut wifi = Wifi::init(spi, esp_pins, &mut delay).unwrap();

let result = wifi.join(ssid, passphrase);
defmt::info!("Join Result: {:?}", result);

defmt::info!("Entering main loop");

let mut sleep: u32 = 1500;
loop {
    match wifi.get_connection_status() {
        Ok(status) => {
            defmt::info!("Connection status: {:?}", status);
            delay.delay_ms(sleep);

            if status == ConnectionStatus::Connected {
                defmt::info!("Connected to network: {:?}", SSID);

                // The IPAddresses of two DNS servers to resolve hostnames with.
                // Note that failover from ip1 to ip2 is fully functional.
                let ip1: IpAddress = [9, 9, 9, 9];
                let ip2: IpAddress = [8, 8, 8, 8];
                let dns_result = wifi.set_dns(ip1, Some(ip2));

                defmt::info!("set_dns result: {:?}", dns_result);

                match wifi.resolve(hostname) {
                    Ok(ip) => {
                        defmt::info!("Server IP: {:?}", ip);
                    }
                    Err(e) => {
                        defmt::error!("Failed to resolve hostname {}", hostname);
                        defmt::error!("Err: {}", e);
                    }
                }

                defmt::info!("Leaving network: {:?}", ssid);
                wifi.leave().ok();
            } else if status == ConnectionStatus::Disconnected {
                sleep = 20000; // No need to loop as often after disconnecting
            }
        }
        Err(e) => {
            defmt::error!("Failed to get connection result: {:?}", e);
        }
    }
}

Structs§

Wifi
Base type for controlling an ESP32-WROOM NINA firmware-based WiFi board.

Enums§

ConnectionStatus
An enumerated type that represents the current WiFi network connection status.