WiFi Example
This commit is contained in:
187
src/main.rs
187
src/main.rs
@@ -1,43 +1,192 @@
|
||||
//! embassy hello world
|
||||
//! Embassy DHCP Example
|
||||
//!
|
||||
//! This is an example of running the embassy executor with multiple tasks
|
||||
//! concurrently.
|
||||
//!
|
||||
//! 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
|
||||
//!
|
||||
//! Because of the huge task-arena size configured this won't work on ESP32-S2
|
||||
|
||||
//% CHIPS: esp32 esp32c2 esp32c3 esp32c6 esp32h2 esp32s2 esp32s3
|
||||
//% FEATURES: embassy esp-hal/unstable
|
||||
//% FEATURES: embassy esp-wifi esp-wifi/wifi esp-hal/unstable
|
||||
//% CHIPS: esp32 esp32s2 esp32s3 esp32c2 esp32c3 esp32c6
|
||||
|
||||
#![no_std]
|
||||
#![no_main]
|
||||
|
||||
use embassy_executor::Spawner;
|
||||
use embassy_net::{Runner, StackResources};
|
||||
use embassy_time::{Duration, Timer};
|
||||
use esp_alloc as _;
|
||||
use esp_backtrace as _;
|
||||
use esp_hal::timer::timg::TimerGroup;
|
||||
use esp_hal::{clock::CpuClock, rng::Rng, timer::timg::TimerGroup};
|
||||
use esp_println::println;
|
||||
use esp_wifi::{
|
||||
init,
|
||||
wifi::{ClientConfiguration, Configuration, WifiController, WifiDevice, WifiEvent, WifiState},
|
||||
EspWifiController,
|
||||
};
|
||||
|
||||
esp_bootloader_esp_idf::esp_app_desc!();
|
||||
|
||||
#[embassy_executor::task]
|
||||
async fn run() {
|
||||
loop {
|
||||
esp_println::println!("Hello world from embassy using esp-hal-async!");
|
||||
Timer::after(Duration::from_millis(1_000)).await;
|
||||
}
|
||||
// When you are okay with using a nightly compiler it's better to use https://docs.rs/static_cell/2.1.0/static_cell/macro.make_static.html
|
||||
macro_rules! mk_static {
|
||||
($t:ty,$val:expr) => {{
|
||||
static STATIC_CELL: static_cell::StaticCell<$t> = static_cell::StaticCell::new();
|
||||
#[deny(unused_attributes)]
|
||||
let x = STATIC_CELL.uninit().write(($val));
|
||||
x
|
||||
}};
|
||||
}
|
||||
|
||||
const SSID: &str = env!("SSID");
|
||||
const PASSWORD: &str = env!("PASSWORD");
|
||||
|
||||
#[esp_hal_embassy::main]
|
||||
async fn main(spawner: Spawner) {
|
||||
async fn main(spawner: Spawner) -> ! {
|
||||
esp_println::logger::init_logger_from_env();
|
||||
let peripherals = esp_hal::init(esp_hal::Config::default());
|
||||
let config = esp_hal::Config::default().with_cpu_clock(CpuClock::max());
|
||||
let peripherals = esp_hal::init(config);
|
||||
|
||||
esp_println::println!("Init!");
|
||||
esp_alloc::heap_allocator!(size: 72 * 1024);
|
||||
|
||||
let timg0 = TimerGroup::new(peripherals.TIMG0);
|
||||
esp_hal_embassy::init(timg0.timer0);
|
||||
let mut rng = Rng::new(peripherals.RNG);
|
||||
|
||||
spawner.spawn(run()).ok();
|
||||
let esp_wifi_ctrl = &*mk_static!(
|
||||
EspWifiController<'static>,
|
||||
init(timg0.timer0, rng, peripherals.RADIO_CLK).unwrap()
|
||||
);
|
||||
|
||||
let (controller, interfaces) = esp_wifi::wifi::new(esp_wifi_ctrl, peripherals.WIFI).unwrap();
|
||||
|
||||
let wifi_interface = interfaces.sta;
|
||||
|
||||
let timg1 = TimerGroup::new(peripherals.TIMG1);
|
||||
esp_hal_embassy::init(timg1.timer0);
|
||||
|
||||
let config = embassy_net::Config::dhcpv4(Default::default());
|
||||
|
||||
let seed = (rng.random() as u64) << 32 | rng.random() as u64;
|
||||
|
||||
// Init network stack
|
||||
let (stack, runner) = embassy_net::new(
|
||||
wifi_interface,
|
||||
config,
|
||||
mk_static!(StackResources<3>, StackResources::<3>::new()),
|
||||
seed,
|
||||
);
|
||||
|
||||
spawner.spawn(connection(controller)).ok();
|
||||
spawner.spawn(net_task(runner)).ok();
|
||||
|
||||
loop {
|
||||
esp_println::println!("Bing!");
|
||||
Timer::after(Duration::from_millis(5_000)).await;
|
||||
if stack.is_link_up() {
|
||||
break;
|
||||
}
|
||||
Timer::after(Duration::from_millis(500)).await;
|
||||
}
|
||||
|
||||
println!("Waiting to get IP address...");
|
||||
loop {
|
||||
if let Some(config) = stack.config_v4() {
|
||||
println!("Got IP: {}", config.address);
|
||||
break;
|
||||
}
|
||||
Timer::after(Duration::from_millis(500)).await;
|
||||
}
|
||||
|
||||
loop {
|
||||
Timer::after(Duration::from_millis(1_000)).await;
|
||||
// do nothing
|
||||
}
|
||||
|
||||
// let mut rx_buffer = [0; 4096];
|
||||
// let mut tx_buffer = [0; 4096];
|
||||
// use core::net::Ipv4Addr;
|
||||
// use embassy_net::tcp::TcpSocket;
|
||||
// loop {
|
||||
// Timer::after(Duration::from_millis(1_000)).await;
|
||||
|
||||
// let mut socket = TcpSocket::new(stack, &mut rx_buffer, &mut tx_buffer);
|
||||
|
||||
// socket.set_timeout(Some(embassy_time::Duration::from_secs(10)));
|
||||
|
||||
// let remote_endpoint = (Ipv4Addr::new(142, 250, 185, 115), 80);
|
||||
// println!("connecting...");
|
||||
// let r = socket.connect(remote_endpoint).await;
|
||||
// if let Err(e) = r {
|
||||
// println!("connect error: {:?}", e);
|
||||
// continue;
|
||||
// }
|
||||
// println!("connected!");
|
||||
// let mut buf = [0; 1024];
|
||||
// loop {
|
||||
// use embedded_io_async::Write;
|
||||
// let r = socket
|
||||
// .write_all(b"GET / HTTP/1.0\r\nHost: www.mobile-j.de\r\n\r\n")
|
||||
// .await;
|
||||
// if let Err(e) = r {
|
||||
// println!("write error: {:?}", e);
|
||||
// break;
|
||||
// }
|
||||
// let n = match socket.read(&mut buf).await {
|
||||
// Ok(0) => {
|
||||
// println!("read EOF");
|
||||
// break;
|
||||
// }
|
||||
// Ok(n) => n,
|
||||
// Err(e) => {
|
||||
// println!("read error: {:?}", e);
|
||||
// break;
|
||||
// }
|
||||
// };
|
||||
// println!("{}", core::str::from_utf8(&buf[..n]).unwrap());
|
||||
// }
|
||||
// Timer::after(Duration::from_millis(30000)).await;
|
||||
// }
|
||||
}
|
||||
|
||||
#[embassy_executor::task]
|
||||
async fn connection(mut controller: WifiController<'static>) {
|
||||
println!("start connection task");
|
||||
println!("Device capabilities: {:?}", controller.capabilities());
|
||||
loop {
|
||||
if esp_wifi::wifi::wifi_state() == WifiState::StaConnected {
|
||||
// wait until we're no longer connected
|
||||
controller.wait_for_event(WifiEvent::StaDisconnected).await;
|
||||
Timer::after(Duration::from_millis(5000)).await
|
||||
}
|
||||
if !matches!(controller.is_started(), Ok(true)) {
|
||||
let client_config = Configuration::Client(ClientConfiguration {
|
||||
ssid: SSID.into(),
|
||||
password: PASSWORD.into(),
|
||||
..Default::default()
|
||||
});
|
||||
controller.set_configuration(&client_config).unwrap();
|
||||
println!("Starting wifi");
|
||||
controller.start_async().await.unwrap();
|
||||
println!("Wifi started!");
|
||||
|
||||
println!("Scan");
|
||||
let result = controller.scan_n_async(10).await.unwrap();
|
||||
for ap in result {
|
||||
println!("{:?}", ap);
|
||||
}
|
||||
}
|
||||
println!("About to connect...");
|
||||
|
||||
match controller.connect_async().await {
|
||||
Ok(_) => println!("Wifi connected!"),
|
||||
Err(e) => {
|
||||
println!("Failed to connect to wifi: {e:?}");
|
||||
Timer::after(Duration::from_millis(5000)).await
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[embassy_executor::task]
|
||||
async fn net_task(mut runner: Runner<'static, WifiDevice<'static>>) {
|
||||
runner.run().await
|
||||
}
|
||||
|
Reference in New Issue
Block a user