From cf5edd0be15566e36847e6eb90323c67830f7aa0 Mon Sep 17 00:00:00 2001 From: ivmarkov Date: Sat, 11 Jan 2025 07:21:30 +0000 Subject: [PATCH] DHXP with a hostname example --- CHANGELOG.md | 4 ++ examples/wifi_dhcp_with_hostname.rs | 91 +++++++++++++++++++++++++++++ 2 files changed, 95 insertions(+) create mode 100644 examples/wifi_dhcp_with_hostname.rs diff --git a/CHANGELOG.md b/CHANGELOG.md index 83efbd3f1c7..d06a2dfc664 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -5,6 +5,10 @@ All notable changes to this project will be documented in this file. The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/), and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html). +## [Unreleased] +### Added +- New example, `wifi_dhcp_with_hostname` to demonstrate setting a custom hostname when establishing a DHCP connection + ## [0.50.1] - 2025-01-06 ### Fixed - Fix ambiguous name error (a compilation issue when the NimBLE component is enabled in esp-idf-sys) diff --git a/examples/wifi_dhcp_with_hostname.rs b/examples/wifi_dhcp_with_hostname.rs new file mode 100644 index 00000000000..bb48493f7f1 --- /dev/null +++ b/examples/wifi_dhcp_with_hostname.rs @@ -0,0 +1,91 @@ +//! Example of using a blocking Wifi with a DHCP configuration that has a user-supplied host name +//! +//! Add your own SSID and password for the access point +//! +//! Once the wifi is connected, the hostname will be set to "foo" +//! Try pinging it from your PC with `ping foo` + +use core::convert::TryInto; + +use embedded_svc::wifi::{AuthMethod, ClientConfiguration, Configuration as WifiConfiguration}; + +use esp_idf_svc::hal::prelude::Peripherals; +use esp_idf_svc::ipv4::{ + ClientConfiguration as IpClientConfiguration, Configuration as IpConfiguration, + DHCPClientSettings, +}; +use esp_idf_svc::log::EspLogger; +use esp_idf_svc::netif::{EspNetif, NetifConfiguration, NetifStack}; +use esp_idf_svc::wifi::{BlockingWifi, EspWifi, WifiDriver}; +use esp_idf_svc::{eventloop::EspSystemEventLoop, nvs::EspDefaultNvsPartition}; + +use log::info; + +const SSID: &str = env!("WIFI_SSID"); +const PASSWORD: &str = env!("WIFI_PASS"); + +fn main() -> anyhow::Result<()> { + esp_idf_svc::sys::link_patches(); + EspLogger::initialize_default(); + + let peripherals = Peripherals::take()?; + let sys_loop = EspSystemEventLoop::take()?; + let nvs = EspDefaultNvsPartition::take()?; + + let wifi = WifiDriver::new(peripherals.modem, sys_loop.clone(), Some(nvs))?; + let wifi = configure_wifi(wifi)?; + + let mut wifi = BlockingWifi::wrap(wifi, sys_loop)?; + connect_wifi(&mut wifi)?; + + let ip_info = wifi.wifi().sta_netif().get_ip_info()?; + + info!("Wifi Interface info: {:?}", ip_info); + + loop { + std::thread::sleep(core::time::Duration::from_secs(5)); + } +} + +fn configure_wifi(wifi: WifiDriver) -> anyhow::Result { + let mut wifi = EspWifi::wrap_all( + wifi, + // Note that setting a custom hostname can be used with any network adapter, not just Wifi + // I.e. that would work with Eth as well, because DHCP is an L3 protocol + EspNetif::new_with_conf(&NetifConfiguration { + ip_configuration: Some(IpConfiguration::Client(IpClientConfiguration::DHCP( + DHCPClientSettings { + hostname: Some("foo".try_into().unwrap()), + }, + ))), + ..NetifConfiguration::wifi_default_client() + })?, + #[cfg(esp_idf_esp_wifi_softap_support)] + EspNetif::new(NetifStack::Ap)?, + )?; + + let wifi_configuration = WifiConfiguration::Client(ClientConfiguration { + ssid: SSID.try_into().unwrap(), + bssid: None, + auth_method: AuthMethod::WPA2Personal, + password: PASSWORD.try_into().unwrap(), + channel: None, + ..Default::default() + }); + wifi.set_configuration(&wifi_configuration)?; + + Ok(wifi) +} + +fn connect_wifi(wifi: &mut BlockingWifi>) -> anyhow::Result<()> { + wifi.start()?; + info!("Wifi started"); + + wifi.connect()?; + info!("Wifi connected"); + + wifi.wait_netif_up()?; + info!("Wifi netif up"); + + Ok(()) +}