-
Hi all! I am trying to get a project generated by esp-template working with embassy by looking into Cargo.toml and the examples in this repo. Currently I am stuck at getting Can someone help me figure out what am I missing? Here is the error I am getting while trying to build:
Cargo toml: [package]
name = "esp-wifi-example"
version = "0.0.0"
edition = "2021"
license = "MIT OR Apache-2.0"
publish = false
[dependencies]
aligned = { version = "0.4.2", optional = true }
bleps = { git = "https://github.com/bjoernQ/bleps", package = "bleps", rev = "a5148d8ae679e021b78f53fd33afb8bb35d0b62e", features = [
"macros",
"async",
] }
bt-hci = "0.1.1"
cfg-if = "1.0.0"
critical-section = "1.1.3"
embassy-executor = { version = "0.6.0" }
embassy-futures = "0.1.1"
embassy-net = { version = "0.4.0", features = [
"tcp",
"udp",
"dhcpv4",
"medium-ethernet",
] }
embassy-sync = "0.6.0"
embassy-time = "0.3.2"
embassy-usb = { version = "0.2.0", default-features = false }
embedded-can = "0.4.1"
embedded-graphics = "0.8.1"
embedded-hal-async = "1.0.0"
embedded-io = { version = "0.6.1", default-features = false }
embedded-io-async = "0.6.1"
embedded-storage = "0.3.1"
esp-alloc = { version = "0.5.0" }
esp-backtrace = { version = "0.14.2", features = [
"exception-handler",
"panic-handler",
"println",
] }
esp-hal = { version = "0.21.0", features = ["log"] }
esp-hal-embassy = { version = "0.4.0", optional = true }
esp-ieee802154 = { version = "0.3.1", optional = true }
esp-println = { version = "0.12.0", features = ["log"] }
esp-storage = { version = "0.3.1", optional = true }
esp-wifi = { version = "0.10.1", optional = true }
fugit = "0.3.7"
heapless = "0.8.0"
hmac = { version = "0.12.1", default-features = false }
ieee80211 = { version = "0.4.0", default-features = false }
ieee802154 = "0.6.1"
lis3dh-async = "0.9.3"
log = "0.4.22"
nb = "1.1.0"
portable-atomic = { version = "1.9.0", default-features = false }
sha2 = { version = "0.10.8", default-features = false }
smoltcp = { version = "0.11.0", default-features = false, features = [
"medium-ethernet",
"socket-raw",
] }
ssd1306 = "0.8.4"
static_cell = { version = "2.1.0", features = ["nightly"] }
trouble-host = { git = "https://github.com/embassy-rs/trouble", package = "trouble-host", rev = "4f1114ce58e96fe54f5ed7e726f66e1ad8d9ce54", features = [
"log",
"gatt",
] }
usb-device = "0.3.2"
usbd-serial = "0.2.2"
[features]
default = ["esp32c6", "embassy", "embassy-generic-timers", "esp-wifi"]
esp32c6 = [
"esp-hal/esp32c6",
"esp-backtrace/esp32c6",
"esp-hal-embassy/esp32c6",
"esp-println/esp32c6",
"esp-wifi/esp32c6",
"esp-ieee802154/esp32c6",
]
esp-wifi = [
"dep:esp-wifi",
"esp-wifi/wifi",
"esp-wifi/async",
"esp-wifi/embassy-net",
"esp-wifi/utils",
"esp-wifi/wifi-default",
]
embassy = [
"dep:esp-hal-embassy",
"embassy-executor/task-arena-size-12288",
]
embassy-generic-timers = ["embassy-time/generic-queue-8"]
[profile.dev.package.esp-wifi]
opt-level = 3
[profile.release]
codegen-units = 1
debug = 2
debug-assertions = false
incremental = false
opt-level = 3
lto = 'fat'
overflow-checks = false the //! Embassy 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
//!
//! Because of the huge task-arena size configured this won't work on ESP32-S2
//% FEATURES: embassy embassy-generic-timers esp-wifi esp-wifi/async esp-wifi/embassy-net esp-wifi/wifi-default esp-wifi/wifi esp-wifi/utils
//% CHIPS: esp32 esp32s2 esp32s3 esp32c2 esp32c3 esp32c6
#![no_std]
#![no_main]
use embassy_executor::Spawner;
use embassy_net::{tcp::TcpSocket, Ipv4Address, Stack, StackResources};
use embassy_time::{Duration, Timer};
use esp_alloc as _;
use esp_backtrace as _;
use esp_hal::{prelude::*, rng::Rng, timer::timg::TimerGroup};
use esp_println::println;
use esp_wifi::{
init,
wifi::{
ClientConfiguration,
Configuration,
WifiController,
WifiDevice,
WifiEvent,
WifiStaDevice,
WifiState,
},
EspWifiInitFor,
};
// 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) -> ! {
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(
EspWifiInitFor::Wifi,
timg0.timer0,
Rng::new(peripherals.RNG),
peripherals.RADIO_CLK,
)
.unwrap();
let wifi = peripherals.WIFI;
let (wifi_interface, controller) =
esp_wifi::wifi::new_with_mode(&init, wifi, WifiStaDevice).unwrap();
use esp_hal::timer::systimer::{SystemTimer, Target};
let systimer = SystemTimer::new(peripherals.SYSTIMER).split::<Target>();
esp_hal_embassy::init(systimer.alarm0);
let config = embassy_net::Config::dhcpv4(Default::default());
let seed = 1234; // very random, very secure seed
// Init network stack
let stack = &*mk_static!(
Stack<WifiDevice<'_, WifiStaDevice>>,
Stack::new(
wifi_interface,
config,
mk_static!(StackResources<3>, StackResources::<3>::new()),
seed
)
);
spawner.spawn(connection(controller)).ok();
spawner.spawn(net_task(&stack)).ok();
let mut rx_buffer = [0; 4096];
let mut tx_buffer = [0; 4096];
loop {
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;
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 = (Ipv4Address::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(3000)).await;
}
}
#[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::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.try_into().unwrap(),
password: PASSWORD.try_into().unwrap(),
..Default::default()
});
controller.set_configuration(&client_config).unwrap();
println!("Starting wifi");
controller.start().await.unwrap();
println!("Wifi started!");
}
println!("About to connect...");
match controller.connect().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(stack: &'static Stack<WifiDevice<'static, WifiStaDevice>>) {
stack.run().await
} Related issuesNote it seems closely related to this and this error but I've applied the last suggestion (adding |
Beta Was this translation helpful? Give feedback.
Replies: 2 comments 3 replies
-
We most likely will replace esp-template with https://github.com/esp-rs/esp-generate - the later should already support embassy + esp-wifi so you could give it a try (but esp-generate is under heavy development) |
Beta Was this translation helpful? Give feedback.
-
Ok getting from the generated template to working code needs some additional modifications to [package]
name = "wific6"
version = "0.1.0"
edition = "2021"
[dependencies]
esp-backtrace = { version = "0.14.1", features = [
"esp32c6",
"exception-handler",
"panic-handler",
"println",
]}
esp-hal = { version = "0.21.0", features = [
"esp32c6",
] }
esp-println = { version = "0.12.0", features = ["esp32c6", "log"] }
log = { version = "0.4.21" }
esp-alloc = { version = "0.5.0" }
embedded-io = "0.6.1"
embedded-io-async = "0.6.1"
esp-wifi = { version = "0.10.1", default-features=false, features = [
"esp32c6",
"phy-enable-usb",
"utils",
"wifi",
"esp-alloc",
"log",
"async",
"embassy-net",
] }
heapless = { version = "0.8.0", default-features = false }
smoltcp = { version = "0.11.0", default-features = false, features = [
"medium-ethernet",
"proto-dhcpv4",
"proto-igmp",
"proto-ipv4",
"socket-dhcpv4",
"socket-icmp",
"socket-raw",
"socket-tcp",
"socket-udp",
] }
embassy-executor = { version = "0.6.0", features = [
"task-arena-size-12288"
] }
embassy-time = { version = "0.3.1", features = ["generic-queue-8"] }
esp-hal-embassy = { version = "0.4.0", features = ["esp32c6"] }
static_cell = { version = "2.1.0", features = ["nightly"] }
embassy-net = { version = "0.4.0", features = [ "tcp", "udp", "dhcpv4", "medium-ethernet"] }
[profile.dev]
# Rust debug is too slow.
# For debug builds always builds with some optimization
opt-level = "s"
[profile.release]
codegen-units = 1 # LLVM can perform better optimizations using a single thread
debug = 2
debug-assertions = false
incremental = false
lto = 'fat'
opt-level = 's'
overflow-checks = false For completeness here is #![no_std]
#![no_main]
use embassy_executor::Spawner;
use embassy_net::{tcp::TcpSocket, Ipv4Address, Stack, StackResources};
use embassy_time::{Duration, Timer};
use esp_alloc as _;
use esp_backtrace as _;
use esp_hal::{prelude::*, rng::Rng, timer::timg::TimerGroup};
use esp_println::println;
use esp_wifi::{
init,
wifi::{
ClientConfiguration,
Configuration,
WifiController,
WifiDevice,
WifiEvent,
WifiStaDevice,
WifiState,
},
EspWifiInitFor,
};
// 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) -> ! {
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(
EspWifiInitFor::Wifi,
timg0.timer0,
Rng::new(peripherals.RNG),
peripherals.RADIO_CLK,
)
.unwrap();
let wifi = peripherals.WIFI;
let (wifi_interface, controller) =
esp_wifi::wifi::new_with_mode(&init, wifi, WifiStaDevice).unwrap();
use esp_hal::timer::systimer::{SystemTimer, Target};
let systimer = SystemTimer::new(peripherals.SYSTIMER).split::<Target>();
esp_hal_embassy::init(systimer.alarm0);
let config = embassy_net::Config::dhcpv4(Default::default());
let seed = 1234; // very random, very secure seed
// Init network stack
let stack = &*mk_static!(
Stack<WifiDevice<'_, WifiStaDevice>>,
Stack::new(
wifi_interface,
config,
mk_static!(StackResources<3>, StackResources::<3>::new()),
seed
)
);
spawner.spawn(connection(controller)).ok();
spawner.spawn(net_task(&stack)).ok();
let mut rx_buffer = [0; 4096];
let mut tx_buffer = [0; 4096];
loop {
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;
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 = (Ipv4Address::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(3000)).await;
}
}
#[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::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.try_into().unwrap(),
password: PASSWORD.try_into().unwrap(),
..Default::default()
});
controller.set_configuration(&client_config).unwrap();
println!("Starting wifi");
controller.start().await.unwrap();
println!("Wifi started!");
}
println!("About to connect...");
match controller.connect().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(stack: &'static Stack<WifiDevice<'static, WifiStaDevice>>) {
stack.run().await
} |
Beta Was this translation helpful? Give feedback.
Ok getting from the generated template to working code needs some additional modifications to
Cargo.toml