How to use ESPNOW & Wifi together? #3006
-
Hello, Trying to use I'm looking at the documentation, and I'm seeing Calling Looking at How do I get either |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 4 replies
-
Here is a modified ``wifi_access_point` example: //! Access point
//!
//! Creates an open access-point with SSID `esp-wifi`.
//! You can connect to it using a static IP in range 192.168.2.2 .. 192.168.2.255, gateway 192.168.2.1
//!
//! Open http://192.168.2.1:8080/ in your browser
//!
//! On Android you might need to choose _Keep Accesspoint_ when it tells you the WiFi has no internet connection, Chrome might not want to load the URL - you can use a shell and try `curl` and `ping`
//!
//% FEATURES: esp-wifi esp-wifi/wifi esp-wifi/utils esp-hal/unstable esp-wifi/esp-now
//% CHIPS: esp32 esp32s2 esp32s3 esp32c2 esp32c3 esp32c6
#![no_std]
#![no_main]
use blocking_network_stack::Stack;
use embedded_io::*;
use esp_alloc as _;
use esp_backtrace as _;
use esp_hal::{
clock::CpuClock,
main,
rng::Rng,
time::{self, Duration},
timer::timg::TimerGroup,
};
use esp_println::{print, println};
use esp_wifi::{
init,
wifi::{
event::{self, EventExt},
utils::create_network_interface,
AccessPointConfiguration,
Configuration,
WifiApDevice,
}, EspWifiController,
};
use smoltcp::iface::{SocketSet, SocketStorage};
#[main]
fn main() -> ! {
esp_println::logger::init_logger_from_env();
let config = esp_hal::Config::default().with_cpu_clock(CpuClock::max());
let peripherals = esp_hal::init(config);
esp_alloc::heap_allocator!(72 * 1024);
let timg0 = TimerGroup::new(peripherals.TIMG0);
// Set event handlers for wifi before init to avoid missing any.
let mut connections = 0u32;
_ = event::ApStart::replace_handler(|_| esp_println::println!("ap start event"));
event::ApStaconnected::update_handler(move |event| {
connections += 1;
esp_println::println!("connected {}, mac: {:?}", connections, event.0.mac);
});
event::ApStaconnected::update_handler(|event| {
esp_println::println!("connected aid: {}", event.0.aid);
});
event::ApStadisconnected::update_handler(|event| {
esp_println::println!(
"disconnected mac: {:?}, reason: {:?}",
event.0.mac,
event.0.reason
);
});
let mut rng = Rng::new(peripherals.RNG);
let init = init(timg0.timer0, rng.clone(), peripherals.RADIO_CLK).unwrap();
let wifi = peripherals.WIFI;
let (mut wifi,token) = esp_wifi::esp_now::enable_esp_now_with_wifi(unsafe { esp_hal::peripherals::WIFI::steal() });
let (iface, device, mut controller) =
create_network_interface(&init, &mut wifi, WifiApDevice).unwrap();
let now = || time::now().duration_since_epoch().to_millis();
let mut socket_set_entries: [SocketStorage; 3] = Default::default();
let socket_set = SocketSet::new(&mut socket_set_entries[..]);
let mut stack = Stack::new(iface, device, socket_set, now, rng.random());
let client_config = Configuration::AccessPoint(AccessPointConfiguration {
ssid: "esp-wifi".try_into().unwrap(),
channel: 11,
..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());
let mut esp_now = esp_wifi::esp_now::EspNow::new_with_wifi(&init, token).unwrap();
println!("{:?}", controller.capabilities());
stack
.set_iface_configuration(&blocking_network_stack::ipv4::Configuration::Client(
blocking_network_stack::ipv4::ClientConfiguration::Fixed(
blocking_network_stack::ipv4::ClientSettings {
ip: blocking_network_stack::ipv4::Ipv4Addr::from(parse_ip("192.168.2.1")),
subnet: blocking_network_stack::ipv4::Subnet {
gateway: blocking_network_stack::ipv4::Ipv4Addr::from(parse_ip(
"192.168.2.1",
)),
mask: blocking_network_stack::ipv4::Mask(24),
},
dns: None,
secondary_dns: None,
},
),
))
.unwrap();
println!("Start busy loop on main. 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 rx_buffer = [0u8; 1536];
let mut tx_buffer = [0u8; 1536];
let mut socket = stack.get_socket(&mut rx_buffer, &mut tx_buffer);
let waiter = esp_now.send(&esp_wifi::esp_now::BROADCAST_ADDRESS, b"Hello World").unwrap();
waiter.wait().unwrap();
socket.listen(8080).unwrap();
loop {
socket.work();
if !socket.is_open() {
socket.listen(8080).unwrap();
}
if socket.is_connected() {
println!("Connected");
let mut time_out = false;
let deadline = time::now() + Duration::secs(20);
let mut buffer = [0u8; 1024];
let mut pos = 0;
while let Ok(len) = socket.read(&mut buffer[pos..]) {
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;
if time::now() > deadline {
println!("Timeout");
time_out = true;
break;
}
}
if !time_out {
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\
",
)
.unwrap();
socket.flush().unwrap();
}
socket.close();
println!("Done\n");
println!();
}
let deadline = time::now() + Duration::secs(5);
while time::now() < deadline {
socket.work();
}
}
}
fn parse_ip(ip: &str) -> [u8; 4] {
let mut result = [0u8; 4];
for (idx, octet) in ip.split(".").into_iter().enumerate() {
result[idx] = u8::from_str_radix(octet, 10).unwrap();
}
result
} The relevant changes @@ -8,7 +8,7 @@
//! On Android you might need to choose _Keep Accesspoint_ when it tells you the WiFi has no internet connection, Chrome might not want to load the URL - you can use a shell and try `curl` and `ping`
//!
-//% FEATURES: esp-wifi esp-wifi/wifi esp-wifi/utils esp-hal/unstable
+//% FEATURES: esp-wifi esp-wifi/wifi esp-wifi/utils esp-hal/unstable esp-wifi/esp-now
//% CHIPS: esp32 esp32s2 esp32s3 esp32c2 esp32c3 esp32c6
#![no_std]
@@ -34,7 +34,7 @@ use esp_wifi::{
AccessPointConfiguration,
Configuration,
WifiApDevice,
- },
+ }, EspWifiController,
};
use smoltcp::iface::{SocketSet, SocketStorage};
@@ -70,7 +70,9 @@ fn main() -> ! {
let init = init(timg0.timer0, rng.clone(), peripherals.RADIO_CLK).unwrap();
- let mut wifi = peripherals.WIFI;
+ let wifi = peripherals.WIFI;
+ let (mut wifi,token) = esp_wifi::esp_now::enable_esp_now_with_wifi(unsafe { esp_hal::peripherals::WIFI::steal() });
+
let (iface, device, mut controller) =
create_network_interface(&init, &mut wifi, WifiApDevice).unwrap();
let now = || time::now().duration_since_epoch().to_millis();
@@ -81,6 +83,7 @@ fn main() -> ! {
let client_config = Configuration::AccessPoint(AccessPointConfiguration {
ssid: "esp-wifi".try_into().unwrap(),
+ channel: 11,
..Default::default()
});
let res = controller.set_configuration(&client_config);
@@ -89,6 +92,8 @@ fn main() -> ! {
controller.start().unwrap();
println!("is wifi started: {:?}", controller.is_started());
+ let mut esp_now = esp_wifi::esp_now::EspNow::new_with_wifi(&init, token).unwrap();
+
println!("{:?}", controller.capabilities());
stack
@@ -116,6 +121,9 @@ fn main() -> ! {
let mut tx_buffer = [0u8; 1536];
let mut socket = stack.get_socket(&mut rx_buffer, &mut tx_buffer);
+ let waiter = esp_now.send(&esp_wifi::esp_now::BROADCAST_ADDRESS, b"Hello World").unwrap();
+ waiter.wait().unwrap();
+
socket.listen(8080).unwrap();
loop { |
Beta Was this translation helpful? Give feedback.
Here is a modified ``wifi_access_point` example: