wifi: rewrite wifi task

This commit is contained in:
2025-12-07 13:14:07 +01:00
parent 20cc634714
commit 4235b54959
3 changed files with 143 additions and 140 deletions

View File

@@ -1,127 +1,114 @@
use bleps::{ad_structure::{create_advertising_data, AdStructure, BR_EDR_NOT_SUPPORTED, LE_GENERAL_DISCOVERABLE}, attribute_server::AttributeServer, gatt, Ble, HciConnector};
use embassy_sync::channel::DynamicReceiver;
use alloc::string::ToString;
use embassy_executor::Spawner;
use embassy_sync::pubsub::DynSubscriber;
use esp_hal::timer::AnyTimer;
use esp_wifi::ble::controller::BleConnector;
use esp_wifi::{EspWifiController, wifi::{ClientConfiguration, WifiDevice}};
use log::*;
use alloc::format;
use crate::events::Notification;
use embassy_net::dns::DnsSocket;
use embassy_net::tcp::client::{TcpClient, TcpClientState};
use embassy_net::{Config, StackResources};
use nalgebra::Vector2;
use reqwless::client::{HttpClient, TlsConfig};
use static_cell::StaticCell;
pub async fn ble_task(_notify: DynSubscriber<'static, Notification>, wifi_init: &esp_wifi::EspWifiController<'_>, bluetooth_device: esp_hal::peripherals::BT<'static>) {
info!("Setting up BLE stack");
let connector = BleConnector::new(wifi_init, bluetooth_device);
let get_millis = || esp_hal::time::Instant::now().duration_since_epoch().as_millis();
let hci = HciConnector::new(connector, get_millis);
let mut ble = Ble::new(&hci);
ble.init().unwrap();
ble.cmd_set_le_advertising_parameters().unwrap();
ble.cmd_set_le_advertising_data(
create_advertising_data(&[
AdStructure::Flags(LE_GENERAL_DISCOVERABLE | BR_EDR_NOT_SUPPORTED),
AdStructure::ServiceUuids16(&[Uuid::Uuid16(0x0001)]),
AdStructure::CompleteLocalName("Renderbug!")
])
.unwrap()
).unwrap();
ble.cmd_set_le_advertise_enable(true).unwrap();
use crate::{backoff::Backoff, events::{Prediction, Telemetry}};
let mut wf1 = |_offset: usize, data: &[u8]| {
info!("Read serial data! {data:?}");
};
// https://nextcloud.malloc.hackerbots.net/nextcloud/index.php/apps/phonetrack/logGet/cc668656ef51680c99b0eb6e5323a459/renderbug?lat=LAT&lon=LON&alt=ALT&acc=ACC&bat=BAT&sat=SAT&speed=SPD&bearing=DIR&timestamp=TIME
// Other useful characteristics:
// 0x2A67 - Location and speed
// 0x2A00 - Device name
// 0x2B90 - Device time
// Permitted characteristics:
// Acceleration
// Force
// Length
// Linear position
// Rotational speed
// Temperature
// Torque
// Useful app that logs data: https://github.com/a2ruan/ArduNetApp?tab=readme-ov-file
// Requires service 4fafc201-1fb5-459e-8fcc-c5c9c331914b, characteristic beb5483e-36e1-4688-b7f5-ea07361b26a8
let s = &b""[..];
gatt!([service {
uuid: "6E400001-B5A3-F393-E0A9-E50E24DCCA9E", // Nordic UART
characteristics: [
characteristic {
uuid: "6E400003-B5A3-F393-E0A9-E50E24DCCA9E", // TX from device, everything is sent as notifications
notify: true,
name: "tx",
value: s
},
characteristic {
uuid: "6E400002-B5A3-F393-E0A9-E50E24DCCA9E", // RX from phone
write: wf1
},
]
}]);
let mut rng = bleps::no_rng::NoRng;
let mut _srv = AttributeServer::new(&mut ble, &mut gatt_attributes, &mut rng);
info!("BLE running!");
// TODO: Somehow need to recreate the attributeserver after disconnecting?
/*loop {
let notification = match notify.try_receive() {
Err(_) => None,
Ok(Notification::Beat) => Some("beat"),
_ => None
};
match notification {
None => {
srv.do_work().unwrap();
Timer::after_millis(5).await;
},
Some(serial_data) => {
for chunk in serial_data.as_bytes().chunks(20) {
srv.do_work_with_notification(Some(NotificationData::new(tx_handle, chunk))).unwrap();
}
srv.do_work_with_notification(Some(NotificationData::new(tx_handle, &b"\n"[..]))).unwrap();
}
}
}*/
#[embassy_executor::task]
async fn net_task(mut runner: embassy_net::Runner<'static, WifiDevice<'static>>) {
info!("Network stack is running");
runner.run().await
}
static RESOURCES: StaticCell<StackResources<5>> = StaticCell::new();
// TODO: Wifi task needs to know when there is data to upload, so it only connects when needed.
#[embassy_executor::task]
pub async fn wireless_task(_notify: DynamicReceiver<'static, Notification>, timer: AnyTimer<'static>, rng: esp_hal::peripherals::RNG<'static>, _wifi_device: esp_hal::peripherals::WIFI<'static>, _bluetooth_device: esp_hal::peripherals::BT<'static>) {
let rng = esp_hal::rng::Rng::new(rng);
let _wifi_init =
esp_wifi::init(timer, rng).expect("Failed to initialize WIFI/BLE controller");
pub async fn wireless_task(mut telemetry: DynSubscriber<'static, Telemetry>, wifi_init: &'static mut EspWifiController<'static>, wifi_device: esp_hal::peripherals::WIFI<'static>) {
let (mut wifi, interfaces) = esp_wifi::wifi::new(wifi_init, wifi_device)
.expect("Failed to initialize WIFI!");
wifi.set_configuration(&esp_wifi::wifi::Configuration::Client(
ClientConfiguration {
ssid: "The Frequency".to_string(),
auth_method: esp_wifi::wifi::AuthMethod::WPA2Personal,
password: "thepasswordkenneth".to_string(),
..Default::default()
}
)).unwrap();
wifi.set_mode(esp_wifi::wifi::WifiMode::Sta).unwrap();
wifi.set_power_saving(esp_wifi::config::PowerSaveMode::Maximum).unwrap();
wifi.start_async().await.unwrap();
//ble_task(notify, &wifi_init, bluetooth_device).await;
let device = interfaces.sta;
// TODO: Somehow grab a real random seed from main()
let seed = 0;
let config = Config::dhcpv4(Default::default());
let (stack, runner) = embassy_net::new(device, config, RESOURCES.init_with(|| { StackResources::new() }), seed as u64);
info!("Launching network task");
Spawner::for_current_executor().await.must_spawn(net_task(runner));
/*
loop {
let (mut wifi, _interfaces) = esp_wifi::wifi::new(&wifi_init, wifi_device)
.expect("Failed to initialize WIFI controller"); }
Backoff::from_secs(3).forever().attempt(async || {
info!("Connecting to wifi...");
match wifi.connect_async().await {
Ok(_) => Ok(()),
Err(e) => {
error!("Wifi error: {e:?}");
Err(())
}
}
}).await.unwrap();
info!("Waiting for DHCP");
stack.wait_config_up().await;
info!("Online!");
let ip_cfg = stack.config_v4().unwrap();
info!("ip={ip_cfg:?}");
let mut rx_buf = [0; 4096];
let mut tx_buf = [0; 4096];
let dns = DnsSocket::new(stack);
let tcp_state = TcpClientState::<1, 4096, 4096>::new();
let tcp = TcpClient::new(stack, &tcp_state);
let tls = TlsConfig::new(
seed as u64,
&mut rx_buf,
&mut tx_buf,
reqwless::client::TlsVerify::None,
);
let mut client = HttpClient::new_with_tls(&tcp, &dns, tls);
loop {
//let results = wifi.scan_n_async(16).await.unwrap();
wifi.set_configuration(&esp_wifi::wifi::Configuration::Client(
ClientConfiguration {
ssid: "The Frequency".to_string(),
auth_method: esp_wifi::wifi::AuthMethod::WPA2Personal,
password: "thepasswordkenneth".to_string(),
..Default::default()
if let Telemetry::Prediction(Prediction::Location(coords)) = telemetry.next_message_pure().await {
if let Err(e) = push_location(&mut client, coords).await {
error!("HTTP error in publishing location: {e:?}");
break
}
)).unwrap();
if wifi.connect_async().await.is_ok() {
info!("Connected to wifi!");
while wifi.is_connected().unwrap() {
Timer::after_secs(60).await;
}
info!("Disconnected.");
}
Timer::after_secs(30).await;
}
}
*/
}
async fn push_location(client: &mut HttpClient<'_, TcpClient<'_, 1, 4096, 4096>, DnsSocket<'_>>, location: Vector2<f64>) -> Result<(), reqwless::Error> {
let mut buffer = [0u8; 4096];
let base = "https://nextcloud.malloc.hackerbots.net/nextcloud/index.php/apps/phonetrack/logGet/a062000067304e9dee6590f1b8f9e0db/renderbug";
let url = format!("{base}?lat={}&lon={}", location.y, location.x);
info!("Pushing to {url}");
let mut http_req = client
.request(
reqwless::request::Method::GET,
&url,
)
.await?;
let response = http_req.send(&mut buffer).await?;
info!("Got response");
let res = response.body().read_to_end().await?;
let content = core::str::from_utf8(res).unwrap();
info!("{content}");
Ok(())
}