From 312e977a7a993fd215739464eae633c0be27dc56 Mon Sep 17 00:00:00 2001 From: ryankopf Date: Sat, 16 Mar 2024 12:10:19 -0500 Subject: [PATCH 1/5] Updating to latest. --- .cargo/config.toml | 2 +- Cargo.toml | 5 +++-- components_esp32.lock | 2 +- examples/webserver.rs | 37 ++++++++++++++++++------------------- src/ble.rs | 9 +++++++-- src/wifi_handler.rs | 9 +++++++-- 6 files changed, 37 insertions(+), 27 deletions(-) diff --git a/.cargo/config.toml b/.cargo/config.toml index cebd1c3..fbc68bc 100644 --- a/.cargo/config.toml +++ b/.cargo/config.toml @@ -13,6 +13,6 @@ build-std = ["std", "panic_abort"] [env] MCU="esp32" # Note: this variable is not used by the pio builder (`cargo build --features pio`) -ESP_IDF_VERSION = "v5.1.1" +ESP_IDF_VERSION = "v5.2.1" CARGO_WORKSPACE_DIR = { value = "", relative = true } \ No newline at end of file diff --git a/Cargo.toml b/Cargo.toml index 407d84f..3ddff2e 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -26,17 +26,18 @@ embassy = ["esp-idf-svc/embassy-sync", "esp-idf-svc/critical-section", "esp-idf- [dependencies] log = { version = "0.4", default-features = false } -esp-idf-svc = { version = "0.47", default-features = false } +esp-idf-svc = { version = "0.48", default-features = false } embedded-svc = "0.26.4" anyhow = "1.0.79" bstr = { version = "1.8.0", default-features = false } -esp32-nimble = "0.5.0" +esp32-nimble = "0.6.0" tokio = { version = "*", features = ["rt", "time", "sync","macros"] } lazy_static = "1.4.0" uuid = { version = "1.2.2", default-features = false, features = ["macro-diagnostics"] } rgb565 = "0.1.3" image = "0.24.8" +heapless = "0.8.0" frankenstein = { version = "0.30", default-features = false, features = ["telegram-trait"]} serde = { version = "1", features = ["derive"]} serde_json = { version = "1"} diff --git a/components_esp32.lock b/components_esp32.lock index 10763e6..6e08dbe 100644 --- a/components_esp32.lock +++ b/components_esp32.lock @@ -3,7 +3,7 @@ dependencies: component_hash: null source: type: idf - version: 5.1.1 + version: 5.2.1 manifest_hash: 3e6980bc5fca475de2a39d94c3ea3944c0528040d862609e810d1d8f5ea7f661 target: esp32 version: 1.0.0 diff --git a/examples/webserver.rs b/examples/webserver.rs index 3dcd8d3..2b6ef66 100644 --- a/examples/webserver.rs +++ b/examples/webserver.rs @@ -27,25 +27,24 @@ fn main() -> Result<()> { }; let camera = Camera::new( - peripherals.pins.gpio32, - peripherals.pins.gpio0, - peripherals.pins.gpio5, - peripherals.pins.gpio18, - peripherals.pins.gpio19, - peripherals.pins.gpio21, - peripherals.pins.gpio36, - peripherals.pins.gpio39, - peripherals.pins.gpio34, - peripherals.pins.gpio35, - peripherals.pins.gpio25, - peripherals.pins.gpio23, - peripherals.pins.gpio22, - peripherals.pins.gpio26, - peripherals.pins.gpio27, + /* PWDN */ peripherals.pins.gpio33, // Adjust X as needed + /* XCLK */ peripherals.pins.gpio21, + /* D0 (Y2) */ peripherals.pins.gpio4, + /* D1 (Y3) */ peripherals.pins.gpio5, + /* D2 (Y4) */ peripherals.pins.gpio18, + /* D3 (Y5) */ peripherals.pins.gpio19, + /* D4 (Y6) */ peripherals.pins.gpio36, + /* D5 (Y7) */ peripherals.pins.gpio39, + /* D6 (Y8) */ peripherals.pins.gpio34, + /* D7 (Y9) */ peripherals.pins.gpio35, + /* VSYNC */ peripherals.pins.gpio25, + /* HREF */ peripherals.pins.gpio23, + /* PCLK */ peripherals.pins.gpio22, + /* SDA (SIOD) */ peripherals.pins.gpio26, + /* SCL (SIOC) */ peripherals.pins.gpio27, esp_idf_sys::camera::pixformat_t_PIXFORMAT_JPEG, esp_idf_sys::camera::framesize_t_FRAMESIZE_UXGA, - ) - .unwrap(); + ).unwrap(); let mut server = EspHttpServer::new(&esp_idf_svc::http::server::Configuration::default())?; @@ -66,13 +65,13 @@ fn main() -> Result<()> { response.write_all("no framebuffer".as_bytes())?; } - Ok(()) + Ok::<(), anyhow::Error>(()) })?; server.fn_handler("/", Method::Get, |request| { let mut response = request.into_ok_response()?; response.write_all("ok".as_bytes())?; - Ok(()) + Ok::<(), anyhow::Error>(()) })?; loop { diff --git a/src/ble.rs b/src/ble.rs index df15437..7fea248 100644 --- a/src/ble.rs +++ b/src/ble.rs @@ -2,7 +2,7 @@ use std::time::Duration; use anyhow::bail; use esp32_nimble::utilities::BleUuid; -use esp32_nimble::{BLEAdvertising, BLEScan, BLEServer, NimbleProperties}; +use esp32_nimble::{BLEAdvertising, BLEAdvertisementData, BLEScan, BLEServer, NimbleProperties}; use lazy_static::lazy_static; use log::{info, warn}; use tokio::select; @@ -52,7 +52,12 @@ pub async fn ble_advertise_task( ); notifying_characteristic.lock().set_value(b"uptime: 0"); - advertising.name(name).add_service_uuid(*UUID_BLE_SERVICE); + // advertising.name(name).add_service_uuid(*UUID_BLE_SERVICE); + advertising.set_data( + BLEAdvertisementData::new() + .name("ESP32-GATT-Server") + .add_service_uuid(BleUuid::Uuid16(0xABCD)), + ); advertising.start().expect("ble_advertising.start()"); diff --git a/src/wifi_handler.rs b/src/wifi_handler.rs index 358df77..5aa7615 100644 --- a/src/wifi_handler.rs +++ b/src/wifi_handler.rs @@ -50,9 +50,14 @@ pub fn my_wifi( None }; + let mut ssid_heapless = heapless::String::<32>::new(); + ssid_heapless.push_str(ssid).expect("SSID exceeds heapless String capacity"); + let mut pass_heapless = heapless::String::<64>::new(); + pass_heapless.push_str(pass).expect("Password exceeds heapless String capacity"); + wifi.set_configuration(&Configuration::Client(ClientConfiguration { - ssid: ssid.into(), - password: pass.into(), + ssid: ssid_heapless, + password: pass_heapless, channel, auth_method, ..Default::default() From 0c4b5e5920db39f26cd4f56744a44f6026024eaf Mon Sep 17 00:00:00 2001 From: ryankopf Date: Sat, 16 Mar 2024 13:11:15 -0500 Subject: [PATCH 2/5] Remove heapless, use try_into --- Cargo.toml | 1 - src/wifi_handler.rs | 9 ++------- 2 files changed, 2 insertions(+), 8 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index 3ddff2e..f6cc72b 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -37,7 +37,6 @@ lazy_static = "1.4.0" uuid = { version = "1.2.2", default-features = false, features = ["macro-diagnostics"] } rgb565 = "0.1.3" image = "0.24.8" -heapless = "0.8.0" frankenstein = { version = "0.30", default-features = false, features = ["telegram-trait"]} serde = { version = "1", features = ["derive"]} serde_json = { version = "1"} diff --git a/src/wifi_handler.rs b/src/wifi_handler.rs index 5aa7615..906c501 100644 --- a/src/wifi_handler.rs +++ b/src/wifi_handler.rs @@ -50,14 +50,9 @@ pub fn my_wifi( None }; - let mut ssid_heapless = heapless::String::<32>::new(); - ssid_heapless.push_str(ssid).expect("SSID exceeds heapless String capacity"); - let mut pass_heapless = heapless::String::<64>::new(); - pass_heapless.push_str(pass).expect("Password exceeds heapless String capacity"); - wifi.set_configuration(&Configuration::Client(ClientConfiguration { - ssid: ssid_heapless, - password: pass_heapless, + ssid: ssid.try_into().expect("SSID could not be converted to heapless String"), + password: pass.try_into().expect("Password could not be converted to heapless String"), channel, auth_method, ..Default::default() From 11c9ebd7537952a3be1d2699297f2fd82fa0b644 Mon Sep 17 00:00:00 2001 From: ryankopf Date: Sat, 16 Mar 2024 13:12:40 -0500 Subject: [PATCH 3/5] Fix BLE Advertisement Data --- src/ble.rs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/ble.rs b/src/ble.rs index 7fea248..50086ae 100644 --- a/src/ble.rs +++ b/src/ble.rs @@ -55,8 +55,8 @@ pub async fn ble_advertise_task( // advertising.name(name).add_service_uuid(*UUID_BLE_SERVICE); advertising.set_data( BLEAdvertisementData::new() - .name("ESP32-GATT-Server") - .add_service_uuid(BleUuid::Uuid16(0xABCD)), + .name(name) + .add_service_uuid(*UUID_BLE_SERVICE), ); advertising.start().expect("ble_advertising.start()"); From f837b4d29e90efadf207edd5b1d2a1ee1a34aefd Mon Sep 17 00:00:00 2001 From: ryankopf Date: Sat, 16 Mar 2024 13:13:39 -0500 Subject: [PATCH 4/5] Leave the pins as-is. --- examples/webserver.rs | 30 +++++++++++++++--------------- 1 file changed, 15 insertions(+), 15 deletions(-) diff --git a/examples/webserver.rs b/examples/webserver.rs index 2b6ef66..ccf1b87 100644 --- a/examples/webserver.rs +++ b/examples/webserver.rs @@ -27,21 +27,21 @@ fn main() -> Result<()> { }; let camera = Camera::new( - /* PWDN */ peripherals.pins.gpio33, // Adjust X as needed - /* XCLK */ peripherals.pins.gpio21, - /* D0 (Y2) */ peripherals.pins.gpio4, - /* D1 (Y3) */ peripherals.pins.gpio5, - /* D2 (Y4) */ peripherals.pins.gpio18, - /* D3 (Y5) */ peripherals.pins.gpio19, - /* D4 (Y6) */ peripherals.pins.gpio36, - /* D5 (Y7) */ peripherals.pins.gpio39, - /* D6 (Y8) */ peripherals.pins.gpio34, - /* D7 (Y9) */ peripherals.pins.gpio35, - /* VSYNC */ peripherals.pins.gpio25, - /* HREF */ peripherals.pins.gpio23, - /* PCLK */ peripherals.pins.gpio22, - /* SDA (SIOD) */ peripherals.pins.gpio26, - /* SCL (SIOC) */ peripherals.pins.gpio27, + peripherals.pins.gpio32, + peripherals.pins.gpio0, + peripherals.pins.gpio5, + peripherals.pins.gpio18, + peripherals.pins.gpio19, + peripherals.pins.gpio21, + peripherals.pins.gpio36, + peripherals.pins.gpio39, + peripherals.pins.gpio34, + peripherals.pins.gpio35, + peripherals.pins.gpio25, + peripherals.pins.gpio23, + peripherals.pins.gpio22, + peripherals.pins.gpio26, + peripherals.pins.gpio27, esp_idf_sys::camera::pixformat_t_PIXFORMAT_JPEG, esp_idf_sys::camera::framesize_t_FRAMESIZE_UXGA, ).unwrap(); From 913508498948f17b90baa354e96626e46c2586f8 Mon Sep 17 00:00:00 2001 From: Kezi Date: Sat, 16 Mar 2024 20:08:58 +0100 Subject: [PATCH 5/5] update deps, config file --- .github/workflows/rust.yml | 2 +- .gitignore | 5 +---- Cargo.toml | 11 +++++++---- build.rs | 17 +++++++++++++++++ cfg.toml.example | 5 +++++ examples/idotmatrix/idotmatrixble.rs | 12 ++++++------ examples/telegram_bot/main.rs | 16 ++++++++++------ examples/webserver.rs | 15 ++++++++++----- src/ble.rs | 14 ++++++++------ src/config.rs | 15 +++++++++++++++ src/lib.rs | 1 + src/wifi_handler.rs | 8 ++++++-- 12 files changed, 87 insertions(+), 34 deletions(-) create mode 100644 cfg.toml.example create mode 100644 src/config.rs diff --git a/.github/workflows/rust.yml b/.github/workflows/rust.yml index 4bfeafa..c0f94d6 100644 --- a/.github/workflows/rust.yml +++ b/.github/workflows/rust.yml @@ -18,4 +18,4 @@ jobs: default: true ldproxy: true - name: Run cargo check - run: cargo check + run: cargo check --bins --examples diff --git a/.gitignore b/.gitignore index 85af13a..39d9c42 100644 --- a/.gitignore +++ b/.gitignore @@ -3,7 +3,4 @@ /target /Cargo.lock -wifi_pass.txt -wifi_ssid.txt -examples/telegram_bot/bot_token.txt -examples/telegram_bot/owner_id.txt +cfg.toml \ No newline at end of file diff --git a/Cargo.toml b/Cargo.toml index f6cc72b..d357492 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -27,7 +27,7 @@ embassy = ["esp-idf-svc/embassy-sync", "esp-idf-svc/critical-section", "esp-idf- [dependencies] log = { version = "0.4", default-features = false } esp-idf-svc = { version = "0.48", default-features = false } -embedded-svc = "0.26.4" +embedded-svc = "0.27.1" anyhow = "1.0.79" bstr = { version = "1.8.0", default-features = false } @@ -36,16 +36,18 @@ tokio = { version = "*", features = ["rt", "time", "sync","macros"] } lazy_static = "1.4.0" uuid = { version = "1.2.2", default-features = false, features = ["macro-diagnostics"] } rgb565 = "0.1.3" -image = "0.24.8" +image = "0.25" frankenstein = { version = "0.30", default-features = false, features = ["telegram-trait"]} serde = { version = "1", features = ["derive"]} serde_json = { version = "1"} idotmatrix = {git = "https://github.com/Kezii/idotmatrix.git"} -esp-idf-hal = "*" -esp-idf-sys = "*" +esp-idf-hal = "0.43" +esp-idf-sys = "0.34" thiserror = "1.0.56" +toml-cfg = "=0.1.3" + [[package.metadata.esp-idf-sys.extra_components]] component_dirs = "components/esp32-camera" bindings_header = "components/bindings.h" @@ -54,3 +56,4 @@ bindings_module = "camera" [build-dependencies] embuild = "0.31.3" +toml-cfg = "=0.1.3" \ No newline at end of file diff --git a/build.rs b/build.rs index 112ec3f..a7b9290 100644 --- a/build.rs +++ b/build.rs @@ -1,3 +1,20 @@ +#[toml_cfg::toml_config] +pub struct Config { + #[default("")] + wifi_ssid: &'static str, + #[default("")] + wifi_psk: &'static str, + #[default("")] + bot_token: &'static str, + #[default(0)] + owner_id: i64, +} + fn main() { + // Check if the `cfg.toml` file exists and has been filled out. + if !std::path::Path::new("cfg.toml").exists() { + panic!("You need to create a `cfg.toml` file with your Wi-Fi credentials! Use `cfg.toml.example` as a template."); + } + embuild::espidf::sysenv::output(); } diff --git a/cfg.toml.example b/cfg.toml.example new file mode 100644 index 0000000..817f4ec --- /dev/null +++ b/cfg.toml.example @@ -0,0 +1,5 @@ +[espcam] +wifi_ssid = "FBI Surveillance Van" +wifi_psk = "hunter2" +bot_token = "change_me" +bot_owner_id = 12345678 \ No newline at end of file diff --git a/examples/idotmatrix/idotmatrixble.rs b/examples/idotmatrix/idotmatrixble.rs index 63d233a..18f217c 100644 --- a/examples/idotmatrix/idotmatrixble.rs +++ b/examples/idotmatrix/idotmatrixble.rs @@ -1,10 +1,10 @@ use crate::Camera; use anyhow::Result; use bstr::ByteSlice; -use esp32_nimble::{uuid128, BLEClient, BLEDevice, BLEReturnCode}; +use esp32_nimble::{uuid128, BLEClient, BLEDevice, BLEError}; use esp_idf_sys::camera; use espcam::espcam::FrameBuffer; -use image::{ImageBuffer, ImageOutputFormat, Rgb}; +use image::{ImageBuffer, ImageFormat, Rgb}; use log::{error, info}; pub struct IDMBle<'a> { @@ -15,7 +15,7 @@ impl<'a> IDMBle<'a> { pub async fn new( ble_device: &'a BLEDevice, client: &'a mut BLEClient, - ) -> Result { + ) -> Result { let ble_scan = ble_device.get_scan(); info!("Scanning for BLE devices..."); @@ -44,11 +44,11 @@ impl<'a> IDMBle<'a> { Ok(Self { characteristic }) } else { error!("No device found"); - Err(BLEReturnCode::fail().unwrap_err()) + Err(BLEError::fail().unwrap_err()) } } - pub async fn send_data(&mut self, bytes: &[u8]) -> Result<(), BLEReturnCode> { + pub async fn send_data(&mut self, bytes: &[u8]) -> Result<(), BLEError> { for (counter, chunk) in bytes.chunks(512).enumerate() { let succ = self.characteristic.write_value(chunk, true).await; info!("progress: {}%", (counter * chunk.len()) * 100 / bytes.len()); @@ -90,7 +90,7 @@ pub async fn idotmatrix_stream_task(camera: Camera<'_>) -> Result<()> { let mut c = std::io::Cursor::new(Vec::new()); info!("Writing png"); - scaled.write_to(&mut c, ImageOutputFormat::Png).unwrap(); + scaled.write_to(&mut c, ImageFormat::Png).unwrap(); info!("Creating command"); let command = idotmatrix::IDMCommand::UploadPng(c.into_inner()); diff --git a/examples/telegram_bot/main.rs b/examples/telegram_bot/main.rs index 861a216..2435068 100644 --- a/examples/telegram_bot/main.rs +++ b/examples/telegram_bot/main.rs @@ -3,7 +3,7 @@ use anyhow::{bail, Result}; use bot_api::{telegram_post_multipart, Esp32Api}; use esp_idf_hal::gpio::PinDriver; use esp_idf_svc::{eventloop::EspSystemEventLoop, hal::peripherals::Peripherals}; -use espcam::{espcam::Camera, wifi_handler::my_wifi}; +use espcam::{config::get_config, espcam::Camera, wifi_handler::my_wifi}; use frankenstein::{ ForwardMessageParams, GetUpdatesParams, SendChatActionParams, SendMessageParams, TelegramApi, }; @@ -29,10 +29,14 @@ fn main() -> Result<()> { let mut flash_led = PinDriver::output(peripherals.pins.gpio4).unwrap(); flash_led.set_low().unwrap(); - let wifi_ssid = include_str!("../../wifi_ssid.txt"); - let wifi_pass = include_str!("../../wifi_pass.txt"); + let config = get_config(); - let _wifi = match my_wifi(wifi_ssid, wifi_pass, peripherals.modem, sysloop) { + let _wifi = match my_wifi( + config.wifi_ssid, + config.wifi_psk, + peripherals.modem, + sysloop, + ) { Ok(inner) => inner, Err(err) => { bail!("Could not connect to Wi-Fi network: {:?}", err) @@ -63,8 +67,8 @@ fn main() -> Result<()> { let mut bot_state = BotState { should_use_flash: false, public_use: false, - owner_id: include_str!("owner_id.txt").parse().unwrap(), - bot_token: include_str!("bot_token.txt"), + owner_id: config.bot_owner_id, + bot_token: config.bot_token, }; let api = Esp32Api::new(bot_state.bot_token); diff --git a/examples/webserver.rs b/examples/webserver.rs index ccf1b87..eef4161 100644 --- a/examples/webserver.rs +++ b/examples/webserver.rs @@ -6,7 +6,7 @@ use esp_idf_svc::{ hal::peripherals::Peripherals, http::{server::EspHttpServer, Method}, }; -use espcam::{espcam::Camera, wifi_handler::my_wifi}; +use espcam::{config::get_config, espcam::Camera, wifi_handler::my_wifi}; fn main() -> Result<()> { esp_idf_svc::sys::link_patches(); @@ -16,10 +16,14 @@ fn main() -> Result<()> { let peripherals = Peripherals::take().unwrap(); - let wifi_ssid = include_str!("../wifi_ssid.txt"); - let wifi_pass = include_str!("../wifi_pass.txt"); + let config = get_config(); - let _wifi = match my_wifi(wifi_ssid, wifi_pass, peripherals.modem, sysloop) { + let _wifi = match my_wifi( + config.wifi_ssid, + config.wifi_psk, + peripherals.modem, + sysloop, + ) { Ok(inner) => inner, Err(err) => { bail!("Could not connect to Wi-Fi network: {:?}", err) @@ -44,7 +48,8 @@ fn main() -> Result<()> { peripherals.pins.gpio27, esp_idf_sys::camera::pixformat_t_PIXFORMAT_JPEG, esp_idf_sys::camera::framesize_t_FRAMESIZE_UXGA, - ).unwrap(); + ) + .unwrap(); let mut server = EspHttpServer::new(&esp_idf_svc::http::server::Configuration::default())?; diff --git a/src/ble.rs b/src/ble.rs index 50086ae..624ac8f 100644 --- a/src/ble.rs +++ b/src/ble.rs @@ -2,7 +2,7 @@ use std::time::Duration; use anyhow::bail; use esp32_nimble::utilities::BleUuid; -use esp32_nimble::{BLEAdvertising, BLEAdvertisementData, BLEScan, BLEServer, NimbleProperties}; +use esp32_nimble::{BLEAdvertisementData, BLEAdvertising, BLEScan, BLEServer, NimbleProperties}; use lazy_static::lazy_static; use log::{info, warn}; use tokio::select; @@ -53,11 +53,13 @@ pub async fn ble_advertise_task( notifying_characteristic.lock().set_value(b"uptime: 0"); // advertising.name(name).add_service_uuid(*UUID_BLE_SERVICE); - advertising.set_data( - BLEAdvertisementData::new() - .name(name) - .add_service_uuid(*UUID_BLE_SERVICE), - ); + advertising + .set_data( + BLEAdvertisementData::new() + .name(name) + .add_service_uuid(*UUID_BLE_SERVICE), + ) + .unwrap(); advertising.start().expect("ble_advertising.start()"); diff --git a/src/config.rs b/src/config.rs new file mode 100644 index 0000000..7c7e268 --- /dev/null +++ b/src/config.rs @@ -0,0 +1,15 @@ +#[toml_cfg::toml_config] +pub struct Config { + #[default("")] + wifi_ssid: &'static str, + #[default("")] + wifi_psk: &'static str, + #[default("")] + bot_token: &'static str, + #[default(0)] + bot_owner_id: i64, +} + +pub fn get_config() -> Config { + CONFIG +} diff --git a/src/lib.rs b/src/lib.rs index 14470b0..8b1a790 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -1,3 +1,4 @@ pub mod ble; +pub mod config; pub mod espcam; pub mod wifi_handler; diff --git a/src/wifi_handler.rs b/src/wifi_handler.rs index 906c501..ff5d6de 100644 --- a/src/wifi_handler.rs +++ b/src/wifi_handler.rs @@ -51,8 +51,12 @@ pub fn my_wifi( }; wifi.set_configuration(&Configuration::Client(ClientConfiguration { - ssid: ssid.try_into().expect("SSID could not be converted to heapless String"), - password: pass.try_into().expect("Password could not be converted to heapless String"), + ssid: ssid + .try_into() + .expect("SSID could not be converted to heapless String"), + password: pass + .try_into() + .expect("Password could not be converted to heapless String"), channel, auth_method, ..Default::default()