* More granular init and deinit per driver - Rework EspWifiInit - No longer require EspWifiInitFor - Add Drop impls for each driver, and add Drop for EspWifiController to fully deinit the stack * unwrap! more stuff * fixup examples and esp-now * unwrap less stuff * review feedback * seal wifi traits, allow rng or trng to init esp-wifi * changelog and migration guide * return wifi error in esp now constructor instead of panic
161 lines
4.3 KiB
Rust
161 lines
4.3 KiB
Rust
//! DHCP Example
|
|
//!
|
|
//!
|
|
//! Set SSID and PASSWORD env variable before running this example.
|
|
//!
|
|
//! This gets an ip address via DHCP then performs an HTTP get request to some "random" server
|
|
//! When using USB-SERIAL-JTAG you may have to activate the feature `phy-enable-usb` in the esp-wifi crate.
|
|
|
|
//% FEATURES: esp-wifi esp-wifi/wifi-default esp-wifi/wifi esp-wifi/utils
|
|
//% CHIPS: esp32 esp32s2 esp32s3 esp32c2 esp32c3 esp32c6
|
|
|
|
#![no_std]
|
|
#![no_main]
|
|
|
|
extern crate alloc;
|
|
|
|
use embedded_io::*;
|
|
use esp_alloc as _;
|
|
use esp_backtrace as _;
|
|
use esp_hal::{
|
|
prelude::*,
|
|
rng::Rng,
|
|
time::{self, Duration},
|
|
timer::timg::TimerGroup,
|
|
};
|
|
use esp_println::{print, println};
|
|
use esp_wifi::{
|
|
init,
|
|
wifi::{
|
|
utils::create_network_interface,
|
|
AccessPointInfo,
|
|
ClientConfiguration,
|
|
Configuration,
|
|
WifiError,
|
|
WifiStaDevice,
|
|
},
|
|
wifi_interface::WifiStack,
|
|
};
|
|
use smoltcp::{
|
|
iface::SocketStorage,
|
|
wire::{IpAddress, Ipv4Address},
|
|
};
|
|
|
|
const SSID: &str = env!("SSID");
|
|
const PASSWORD: &str = env!("PASSWORD");
|
|
|
|
#[entry]
|
|
fn main() -> ! {
|
|
esp_println::logger::init_logger_from_env();
|
|
let peripherals = esp_hal::init({
|
|
let mut config = esp_hal::Config::default();
|
|
config.cpu_clock = CpuClock::max();
|
|
config
|
|
});
|
|
|
|
esp_alloc::heap_allocator!(72 * 1024);
|
|
|
|
let timg0 = TimerGroup::new(peripherals.TIMG0);
|
|
|
|
let init = init(
|
|
timg0.timer0,
|
|
Rng::new(peripherals.RNG),
|
|
peripherals.RADIO_CLK,
|
|
)
|
|
.unwrap();
|
|
|
|
let mut wifi = peripherals.WIFI;
|
|
let mut socket_set_entries: [SocketStorage; 3] = Default::default();
|
|
let (iface, device, mut controller, sockets) =
|
|
create_network_interface(&init, &mut wifi, WifiStaDevice, &mut socket_set_entries).unwrap();
|
|
let now = || time::now().duration_since_epoch().to_millis();
|
|
let wifi_stack = WifiStack::new(iface, device, sockets, now);
|
|
|
|
let client_config = Configuration::Client(ClientConfiguration {
|
|
ssid: SSID.try_into().unwrap(),
|
|
password: PASSWORD.try_into().unwrap(),
|
|
..Default::default()
|
|
});
|
|
let res = controller.set_configuration(&client_config);
|
|
println!("wifi_set_configuration returned {:?}", res);
|
|
|
|
controller.start().unwrap();
|
|
println!("is wifi started: {:?}", controller.is_started());
|
|
|
|
println!("Start Wifi Scan");
|
|
let res: Result<(heapless::Vec<AccessPointInfo, 10>, usize), WifiError> = controller.scan_n();
|
|
if let Ok((res, _count)) = res {
|
|
for ap in res {
|
|
println!("{:?}", ap);
|
|
}
|
|
}
|
|
|
|
println!("{:?}", controller.get_capabilities());
|
|
println!("wifi_connect {:?}", controller.connect());
|
|
|
|
// wait to get connected
|
|
println!("Wait to get connected");
|
|
loop {
|
|
match controller.is_connected() {
|
|
Ok(true) => break,
|
|
Ok(false) => {}
|
|
Err(err) => {
|
|
println!("{:?}", err);
|
|
loop {}
|
|
}
|
|
}
|
|
}
|
|
println!("{:?}", controller.is_connected());
|
|
|
|
// wait for getting an ip address
|
|
println!("Wait to get an ip address");
|
|
loop {
|
|
wifi_stack.work();
|
|
|
|
if wifi_stack.is_iface_up() {
|
|
println!("got ip {:?}", wifi_stack.get_ip_info());
|
|
break;
|
|
}
|
|
}
|
|
|
|
println!("Start busy loop on main");
|
|
|
|
let mut rx_buffer = [0u8; 1536];
|
|
let mut tx_buffer = [0u8; 1536];
|
|
let mut socket = wifi_stack.get_socket(&mut rx_buffer, &mut tx_buffer);
|
|
|
|
loop {
|
|
println!("Making HTTP request");
|
|
socket.work();
|
|
|
|
socket
|
|
.open(IpAddress::Ipv4(Ipv4Address::new(142, 250, 185, 115)), 80)
|
|
.unwrap();
|
|
|
|
socket
|
|
.write(b"GET / HTTP/1.0\r\nHost: www.mobile-j.de\r\n\r\n")
|
|
.unwrap();
|
|
socket.flush().unwrap();
|
|
|
|
let deadline = time::now() + Duration::secs(20);
|
|
let mut buffer = [0u8; 512];
|
|
while let Ok(len) = socket.read(&mut buffer) {
|
|
let to_print = unsafe { core::str::from_utf8_unchecked(&buffer[..len]) };
|
|
print!("{}", to_print);
|
|
|
|
if time::now() > deadline {
|
|
println!("Timeout");
|
|
break;
|
|
}
|
|
}
|
|
println!();
|
|
|
|
socket.disconnect();
|
|
|
|
let deadline = time::now() + Duration::secs(5);
|
|
while time::now() < deadline {
|
|
socket.work();
|
|
}
|
|
}
|
|
}
|