#![no_std]
#![no_main]
#![feature(type_alias_impl_trait)]
use embassy_net::tcp::TcpSocket;
use embassy_net::{
Config, IpListenEndpoint, Ipv4Address, Ipv4Cidr, Stack, StackResources, StaticConfigV4,
};
#[path = "util.rs"]
mod examples_util;
use examples_util::hal;
use embassy_executor::Spawner;
use embassy_time::{Duration, Timer};
use esp_backtrace as _;
use esp_println::{print, println};
use esp_wifi::wifi::{AccessPointConfiguration, Configuration};
use esp_wifi::wifi::{WifiApDevice, WifiController, WifiDevice, WifiEvent, WifiState};
use esp_wifi::{initialize, EspWifiInitFor};
use hal::clock::ClockControl;
use hal::Rng;
use hal::{embassy, peripherals::Peripherals, prelude::*, timer::TimerGroup};
use static_cell::make_static;
#[main]
async fn main(spawner: Spawner) -> ! {
#[cfg(feature = "log")]
esp_println::logger::init_logger(log::LevelFilter::Info);
let peripherals = Peripherals::take();
let system = peripherals.SYSTEM.split();
let clocks = ClockControl::max(system.clock_control).freeze();
#[cfg(target_arch = "xtensa")]
let timer = hal::timer::TimerGroup::new(peripherals.TIMG1, &clocks).timer0;
#[cfg(target_arch = "riscv32")]
let timer = hal::systimer::SystemTimer::new(peripherals.SYSTIMER).alarm0;
let init = initialize(
EspWifiInitFor::Wifi,
timer,
Rng::new(peripherals.RNG),
system.radio_clock_control,
&clocks,
)
.unwrap();
let wifi = peripherals.WIFI;
let (wifi_interface, controller) =
esp_wifi::wifi::new_with_mode(&init, wifi, WifiApDevice).unwrap();
let timer_group0 = TimerGroup::new(peripherals.TIMG0, &clocks);
embassy::init(&clocks, timer_group0);
let config = Config::ipv4_static(StaticConfigV4 {
address: Ipv4Cidr::new(Ipv4Address::new(192, 168, 2, 1), 24),
gateway: Some(Ipv4Address::from_bytes(&[192, 168, 2, 1])),
dns_servers: Default::default(),
});
let seed = 1234; // very random, very secure seed
// Init network stack
let stack = &*make_static!(Stack::new(
wifi_interface,
config,
make_static!(StackResources::<3>::new()),
seed
));
spawner.spawn(connection(controller)).ok();
spawner.spawn(net_task(&stack)).ok();
let mut rx_buffer = [0; 1536];
let mut tx_buffer = [0; 1536];
loop {
if stack.is_link_up() {
break;
}
Timer::after(Duration::from_millis(500)).await;
}
println!("Connect to the AP `esp-wifi` and point your browser to http://192.168.2.1:8080/");
println!("Use a static IP in the range 192.168.2.2 .. 192.168.2.255, use gateway 192.168.2.1");
let mut socket = TcpSocket::new(&stack, &mut rx_buffer, &mut tx_buffer);
socket.set_timeout(Some(embassy_time::Duration::from_secs(10)));
loop {
println!("Wait for connection...");
let r = socket
.accept(IpListenEndpoint {
addr: None,
port: 8080,
})
.await;
println!("Connected...");
if let Err(e) = r {
println!("connect error: {:?}", e);
continue;
}
use embedded_io_async::Write;
let mut buffer = [0u8; 1024];
let mut pos = 0;
loop {
match socket.read(&mut buffer).await {
Ok(0) => {
println!("read EOF");
break;
}
Ok(len) => {
let to_print =
unsafe { core::str::from_utf8_unchecked(&buffer[..(pos + len)]) };
if to_print.contains("\r\n\r\n") {
print!("{}", to_print);
println!();
break;
}
pos += len;
}
Err(e) => {
println!("read error: {:?}", e);
break;
}
};
}
let r = socket
.write_all(
b"HTTP/1.0 200 OK\r\n\r\n\
<html>\
<body>\
<h1>Hello Rust! Hello esp-wifi!</h1>\
</body>\
</html>\r\n\
",
)
.await;
if let Err(e) = r {
println!("write error: {:?}", e);
}
let r = socket.flush().await;
if let Err(e) = r {
println!("flush error: {:?}", e);
}
Timer::after(Duration::from_millis(1000)).await;
socket.close();
Timer::after(Duration::from_millis(1000)).await;
socket.abort();
}
}
#[embassy_executor::task]
async fn connection(mut controller: WifiController<'static>) {
println!("start connection task");
println!("Device capabilities: {:?}", controller.get_capabilities());
loop {
match esp_wifi::wifi::get_wifi_state() {
WifiState::ApStarted => {
// wait until we're no longer connected
controller.wait_for_event(WifiEvent::ApStop).await;
Timer::after(Duration::from_millis(5000)).await
}
_ => {}
}
if !matches!(controller.is_started(), Ok(true)) {
let client_config = Configuration::AccessPoint(AccessPointConfiguration {
ssid: "esp-wifi".try_into().unwrap(),
..Default::default()
});
controller.set_configuration(&client_config).unwrap();
println!("Starting wifi");
controller.start().await.unwrap();
println!("Wifi started!");
}
}
}
#[embassy_executor::task]
async fn net_task(stack: &'static Stack<WifiDevice<'static, WifiApDevice>>) {
stack.run().await
}
Loading
esp32-c3-devkitm-1
esp32-c3-devkitm-1