Skip to content

Instantly share code, notes, and snippets.

@Sofiman
Last active April 15, 2024 10:12
Show Gist options
  • Save Sofiman/53ae30631bf5721226261c5ee1ea637c to your computer and use it in GitHub Desktop.
Save Sofiman/53ae30631bf5721226261c5ee1ea637c to your computer and use it in GitHub Desktop.
#![no_std]
#![no_main]
use url_lite::Url;
use hal::{
clock::{ClockControl, CpuClock},
peripherals::Peripherals,
prelude::*,
timer::TimerGroup,
Rtc,
Rng
};
use embedded_svc::{
ipv4::Interface,
wifi::{ClientConfiguration, Configuration, Wifi},
};
use esp_wifi::{
wifi::{WifiMode, utils::create_network_interface},
wifi_interface::WifiStack,
current_millis, initialize, EspWifiInitFor
};
use esp_println::{print, println};
use embedded_svc::io::{Read, Write as _};
use esp_backtrace as _;
use smoltcp::{
iface::{SocketStorage},
wire::DnsQueryType
};
const SSID: &str = "";
const PASSWORD: &str = "";
#[entry]
fn main() -> ! {
let peripherals = Peripherals::take();
let mut system = peripherals.SYSTEM.split();
let clocks = ClockControl::configure(system.clock_control, CpuClock::Clock240MHz).freeze();
let mut rtc = Rtc::new(peripherals.RTC_CNTL);
let timer_group0 = TimerGroup::new(
peripherals.TIMG0,
&clocks,
&mut system.peripheral_clock_control,
);
let mut wdt0 = timer_group0.wdt;
let timer_group1 = TimerGroup::new(
peripherals.TIMG1,
&clocks,
&mut system.peripheral_clock_control,
);
let mut wdt1 = timer_group1.wdt;
// Disable watchdog timers
rtc.swd.disable();
rtc.rwdt.disable();
wdt0.disable();
wdt1.disable();
println!("Starting up...");
let timer = timer_group1.timer0;
let init = initialize(
EspWifiInitFor::Wifi,
timer,
Rng::new(peripherals.RNG),
system.radio_clock_control,
&clocks,
)
.unwrap();
let (wifi, _) = peripherals.RADIO.split();
let mut socket_set_entries: [SocketStorage; 4] = Default::default();
let (iface, device, mut controller, sockets) =
create_network_interface(&init, wifi, WifiMode::Sta, &mut socket_set_entries);
let wifi_stack = WifiStack::new(iface, device, sockets, current_millis);
let client_config = Configuration::Client(ClientConfiguration {
ssid: SSID.into(), password: PASSWORD.into(), ..Default::default() });
controller.set_configuration(&client_config).unwrap();
controller.start().unwrap();
println!("Is wifi started: {:?}", controller.is_started());
controller.connect().unwrap();
println!("Wait to get connected");
while !controller.is_connected().unwrap() {}
while !wifi_stack.is_iface_up() { wifi_stack.work(); }
let ip_info = wifi_stack.get_ip_info().unwrap();
println!("got {ip_info:?}");
let mut rx_buffer = [0u8; 1536];
let mut tx_buffer = [0u8; 1536];
let mut socket = wifi_stack.get_socket(&mut rx_buffer, &mut tx_buffer);
// the file must be at least 130048 bytes in size to complete tests (1.3Mb)
let url = Url::parse("http://192.168.0.120/audio-8000.raw").unwrap();
let path = url.path.unwrap();
let hostname = url.host.unwrap();
let host = wifi_stack.dns_query(hostname, DnsQueryType::A).unwrap()[0];
print!("Making HTTP request to {host} for {path}...");
socket.work();
socket.open(host, 80).unwrap();
print!("Writing...");
write!(socket, "GET {} HTTP/1.1\r\nHost: {}\r\nConnection: close\r\n\r\n",
path, hostname).unwrap();
socket.flush().unwrap();
println!("OK\n");
println!("Measuring download speed");
let mut buf = [0u8; 32768];
measure_speed(&mut buf, &mut socket, 1024);
measure_speed(&mut buf, &mut socket, 2048);
measure_speed(&mut buf, &mut socket, 4096);
measure_speed(&mut buf, &mut socket, 8192);
measure_speed(&mut buf, &mut socket, 16384);
measure_speed(&mut buf, &mut socket, 32768);
measure_speed(&mut buf, &mut socket, 32768*2);
todo!()
}
fn measure_speed(buf: &mut [u8], socket: &mut esp_wifi::wifi_interface::Socket, target: usize) {
let mut s = current_millis();
let mut total = 0;
let mut front_idx = 0;
while total < target {
let len = socket.read(&mut buf[front_idx..]).unwrap();
total += len;
front_idx += len;
if front_idx >= buf.len() {
front_idx = 0;
}
}
s = current_millis() - s;
let speed = total as u64 * 1000 / s;
println!("downloaded {}B in {s}ms => {}B/s", target, speed);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment