//! # HTTP Server
use core::str;
use std::{
sync::{Arc, Mutex},
thread::sleep,
time::Duration,
};
use bsc::{temp_sensor::BoardTempSensor, wifi::wifi};
use embedded_svc::{
http::server::{registry::Registry, Response},
io::Write,
};
use esp32_c3_dkc02_bsc as bsc;
use esp_idf_svc::http::server::{Configuration, EspHttpServer};
use esp_idf_sys as _; // If using the `binstart` feature of `esp-idf-sys`, always keep this module imported
#[toml_cfg::toml_config]
pub struct Config {
#[default("Wokwi-GUEST")]
wifi_ssid: &'static str,
#[default("")]
wifi_psk: &'static str,
}
fn main() -> anyhow::Result<()> {
esp_idf_sys::link_patches();
let _wifi = wifi(CONFIG.wifi_ssid, CONFIG.wifi_psk)?;
let server_config = Configuration::default();
let mut server = EspHttpServer::new(&server_config)?;
server.handle_get("/", |_request, response| {
let html = index_html();
let mut writer = response.into_writer()?;
writer.write_all(html.as_bytes())?;
Ok(())
})?;
let temp_sensor_main = Arc::new(Mutex::new(BoardTempSensor::new_taking_peripherals()));
let temp_sensor = temp_sensor_main.clone();
server.handle_get("/temperature", move |_request, response| {
let temp_val = temp_sensor.lock().unwrap().read_owning_peripherals();
let html = temperature(temp_val);
let mut writer = response.into_writer()?;
writer.write_all(html.as_bytes())?;
Ok(())
})?;
println!("server awaiting connection");
loop {
sleep(Duration::from_millis(1000));
}
}
fn templated(content: impl AsRef<str>) -> String {
format!(
r#"
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>esp-rs web server</title>
</head>
<body>
{}
</body>
</html>
"#,
content.as_ref()
)
}
fn index_html() -> String {
templated("Hello from mcu!")
}
fn temperature(val: f32) -> String {
templated(format!("chip temperature: {:.2}°C", val))
}
Loading
esp32-c3-rust-1
esp32-c3-rust-1