Added buttons on page to turn on/off an LED on pin 17

This commit is contained in:
Wyatt Marchand
2026-08-24 19:45:54 -07:00
parent c997bc6139
commit bc1be6e4e5
+34 -8
View File
@@ -1,8 +1,10 @@
use std::sync::{Arc, Mutex};
use std::thread;
use std::time::Duration;
use esp_idf_svc::eth::{BlockingEth, EspEth, EthDriver, SpiEthChipset};
use esp_idf_svc::eventloop::EspSystemEventLoop;
use esp_idf_svc::hal::gpio::{Output, PinDriver};
use esp_idf_svc::hal::peripherals::Peripherals;
use esp_idf_svc::hal::prelude::*;
use esp_idf_svc::hal::spi::{config::DriverConfig, Dma, SpiDriver};
@@ -19,11 +21,14 @@ fn main() -> anyhow::Result<()> {
let sysloop = EspSystemEventLoop::take()?;
let pins = peripherals.pins;
// LED on IO17
let led = Arc::new(Mutex::new(PinDriver::output(pins.gpio17)?));
let spi_driver = SpiDriver::new(
peripherals.spi2,
pins.gpio13, // SCLK
pins.gpio11, // MOSI (sdo)
Some(pins.gpio12), // MISO (sdi)
pins.gpio11, // MOSI
Some(pins.gpio12), // MISO
&DriverConfig {
dma: Dma::Auto(4096),
..Default::default()
@@ -32,13 +37,13 @@ fn main() -> anyhow::Result<()> {
let eth_driver = EthDriver::new_spi(
spi_driver,
pins.gpio10, // INT
Some(pins.gpio14), // CS
Some(pins.gpio9), // RST
pins.gpio10, // INT
Some(pins.gpio14), // CS
Some(pins.gpio9), // RST
SpiEthChipset::W5500,
20.MHz().into(),
Some(&[0x02, 0x00, 0x00, 0x12, 0x34, 0x56]), // locally-administered MAC
None, // phy addr — default
Some(&[0x02, 0x00, 0x00, 0x12, 0x34, 0x56]),
None,
sysloop.clone(),
)?;
@@ -54,12 +59,33 @@ fn main() -> anyhow::Result<()> {
info!("Got IP: {:?}", ip_info);
let mut server = EspHttpServer::new(&HttpServerConfig::default())?;
server.fn_handler("/", Method::Get, |req| {
let html = "<html><body><h1>Hello World</h1></body></html>";
let html = r#"<html>
<body>
<h1>Hello World</h1>
<button onclick="fetch('/led/on', {method:'POST'})">LED ON</button>
<button onclick="fetch('/led/off', {method:'POST'})">LED OFF</button>
</body>
</html>"#;
req.into_ok_response()?.write_all(html.as_bytes())?;
anyhow::Ok(())
})?;
let led_on = led.clone();
server.fn_handler("/led/on", Method::Post, move |req| {
led_on.lock().unwrap().set_high()?;
req.into_ok_response()?.write_all(b"on")?;
anyhow::Ok(())
})?;
let led_off = led.clone();
server.fn_handler("/led/off", Method::Post, move |req| {
led_off.lock().unwrap().set_low()?;
req.into_ok_response()?.write_all(b"off")?;
anyhow::Ok(())
})?;
info!("Server up. Browse to http://{}/", ip_info.ip);
loop {