Made esp connect to wifi and display ip configuration on epd

This commit is contained in:
zenonet
2025-09-07 15:27:16 +02:00
parent cb048caf24
commit ec8ca3ae00
3 changed files with 41 additions and 6 deletions

1
.gitignore vendored
View File

@@ -2,3 +2,4 @@
/.embuild
/target
/Cargo.lock
wifi-creds.txt

View File

@@ -29,6 +29,7 @@ esp-idf-hal = "0.45.2"
epd-waveshare = "0.6.0"
embedded-graphics = "0.8.1"
embedded-hal = "1.0.0"
heapless = "0.8"
# --- Optional Embassy Integration ---
# esp-idf-svc = { version = "0.51", features = ["critical-section", "embassy-time-driver", "embassy-sync"] }

View File

@@ -1,7 +1,10 @@
use std::str::FromStr;
use embedded_graphics::{mono_font::MonoTextStyleBuilder, prelude::Point, Drawable, text::{Baseline, Text, TextStyleBuilder}};
use epd_waveshare::{color::Color, epd7in5_v2::Epd7in5, prelude::WaveshareDisplay};
use esp_idf_hal::{delay::{Delay, Ets}, gpio::{Gpio19, PinDriver}, spi::{config, Spi, SpiDriver, SpiDriverConfig}};
use esp_idf_svc::hal::{prelude::*, spi::SpiDeviceDriver};
use esp_idf_svc::{eventloop::EspSystemEventLoop, hal::{prelude::*, spi::SpiDeviceDriver}, nvs::EspDefaultNvsPartition, wifi::Configuration};
use esp_idf_svc::wifi::{EspWifi, BlockingWifi};
use embedded_hal::delay::DelayNs;
@@ -49,26 +52,56 @@ fn main() {
// Allocating on the stack makes the chip crash. We gotta put it on the heap
let mut display = Box::new(epd_waveshare::epd7in5_v2::Display7in5::default());
display.set_rotation(epd_waveshare::prelude::DisplayRotation::Rotate180);
display.set_rotation(epd_waveshare::prelude::DisplayRotation::Rotate0);
let mut delay = Ets;
let mut epd7in5 = Epd7in5::new(&mut spi, busy, dc, rst, &mut delay, None).unwrap();
log::info!("Created epd object");
log::info!("Created display object");
// Create a file called wifi-creds.txt and write SSID and password into it separated by a line break
let mut creds = include_str!("../wifi-creds.txt").split('\n');
let ssid: heapless::String<32> = heapless::String::<32>::from_str(creds.next().unwrap()).unwrap();
let password: heapless::String<64> = heapless::String::<64>::from_str(creds.next().unwrap()).unwrap();
// Connecting to wifi
let sysloop = EspSystemEventLoop::take().unwrap();
let nvs = EspDefaultNvsPartition::take().unwrap();
let mut wifi =
BlockingWifi::wrap(
EspWifi::new(peripherals.modem, sysloop.clone(), Some(nvs)).unwrap(), sysloop.clone()).unwrap();
wifi.set_configuration(&Configuration::Client(esp_idf_svc::wifi::ClientConfiguration {
ssid,
password,
auth_method: esp_idf_svc::wifi::AuthMethod::WPA2WPA3Personal,
..Default::default()
})
).unwrap();
wifi.start().unwrap();
wifi.connect().unwrap();
wifi.wait_netif_up().unwrap();
while !wifi.is_connected().unwrap(){
let config = wifi.get_configuration().unwrap();
println!("Waiting for station {:?}", config);
}
let ip_info = wifi.wifi().sta_netif().get_ip_info().unwrap();
draw_text(&mut display, "Hello esp", 5, 50);
draw_text(&mut display, &format!("{:#?}", ip_info), 5, 5);
log::info!("Draw text done");
let res = epd7in5.update_and_display_frame(&mut spi, &*display.buffer(), &mut delay);
if let Err(e) = res{
log::info!("{}", e);
}
epd7in5.sleep(&mut spi, &mut delay).unwrap();
}
fn draw_text(display: &mut epd_waveshare::epd7in5_v2::Display7in5, text: &str, x: i32, y: i32) {