-
Hi All, Just trying to get one of the wifi examples to work but I get the below error when I run it on the ESP32.
main.rs: #![no_std]
#![no_main]
extern crate alloc;
use embassy_executor::Spawner;
use embassy_net::{tcp::TcpSocket, Ipv4Address, Stack, StackResources};
use embassy_net_driver::HardwareAddress;
use embassy_time::Duration;
use embassy_time::Timer;
use embedded_io_async::Write;
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();
let timg1 = TimerGroup::new(peripherals.TIMG1);
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(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(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
} config.toml: [target.xtensa-esp32-none-elf]
runner = "espflash flash --monitor"
[env]
ESP_LOG="info"
EMBASSY_EXECUTOR_TASK_ARENA_SIZE="16384"
[build]
rustflags = [
"-C", "link-arg=-nostartfiles",
]
target = "xtensa-esp32-none-elf"
[unstable]
build-std = ["core", "alloc"] Cargo.toml: [package]
name = "esp32-monitor"
version = "0.1.0"
edition = "2021"
license = "MIT OR Apache-2.0"
[features]
esp32 = ["esp-hal/esp32", "esp-backtrace/esp32", "esp-hal-embassy/esp32", "esp-println/esp32", "esp-wifi/esp32"]
[dependencies]
esp-backtrace = { version = "0.14.2", features = [
"esp32",
"exception-handler",
"panic-handler",
"println",
] }
esp-hal = { version = "0.21.0", features = [ "esp32" ] }
esp-println = { version = "0.12.0", features = ["esp32", "log"] }
log = { version = "0.4.22" }
embedded-io = "0.6.1"
esp-wifi = { version = "0.10.1", features = ["esp32", "phy-enable-usb", "utils", "wifi", "log", "esp-alloc", "embassy-net", "log"], default-features = false}
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",
] }
esp-alloc = "0.5.0"
embassy-executor = { version = "0.6.1", features = ["integrated-timers"] }
embassy-net = {version = "0.4.0", features= [ "tcp", "udp", "dhcpv4", "medium-ethernet"]}
embassy-net-driver = "0.2.0"
embedded-io-async = "0.6.1"
static_cell = "2.1.0"
esp-hal-embassy = { version = "0.4.0", features = ["esp32"] }
cfg-if = "1.0.0"
embassy-time = "0.3.2"
[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 I've been head-butting a wall for the past few hours so some help or guidance would be very appreciated. |
Beta Was this translation helpful? Give feedback.
Answered by
bugadani
Nov 4, 2024
Replies: 2 comments 2 replies
-
You don't seem to be calling |
Beta Was this translation helpful? Give feedback.
1 reply
Answer selected by
JurajSadel
-
@ellahin does the above answer solve your problem? If yes, please select it as the answer and close the issue. Thanks |
Beta Was this translation helpful? Give feedback.
1 reply
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
You don't seem to be calling
esp_hal_embassy::init
anywhere.