Made esp connect to wifi and display ip configuration on epd
This commit is contained in:
45
src/main.rs
45
src/main.rs
@@ -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) {
|
||||
|
||||
Reference in New Issue
Block a user