diff --git a/Cargo.lock b/Cargo.lock index c04476e..8917ced 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1314,12 +1314,32 @@ version = "1.0.22" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "a50f4cf475b65d88e057964e0e9bb1f0aa9bbb2036dc65c64596b42932536984" +[[package]] +name = "scd4x" +version = "0.4.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "455974f1daa137284e9e282362d1074f004028a77ee84c6bb1508c9e89646153" +dependencies = [ + "embedded-hal 1.0.0", + "log", + "sensirion-i2c", +] + [[package]] name = "semihosting" version = "0.1.21" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "5669d73a3503b5d34db3359c8c87d1b33ba68b2630c153e340d106c15a8626a7" +[[package]] +name = "sensirion-i2c" +version = "0.4.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "640a03c95c176226cf159ae2727f7fe3245546022639d61459af46d2878aec06" +dependencies = [ + "embedded-hal 1.0.0", +] + [[package]] name = "serde" version = "1.0.228" @@ -1607,6 +1627,7 @@ dependencies = [ "esp-radio", "esp-rtos", "lcd-lcm1602-i2c", + "scd4x", ] [[package]] diff --git a/Cargo.toml b/Cargo.toml index e319eec..27f488c 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -52,10 +52,11 @@ esp-radio = { version = "0.17.0", features = [ critical-section = "1.2.0" -# Custom +#Custom bosch-bme680 = "1.0.4" lcd-lcm1602-i2c = "0.3.0" -embedded-hal-bus = { version = "0.3.0" } +embedded-hal-bus = { version = "0.3.0", features = ["alloc"] } +scd4x = { version = "0.4.1", features = ["scd41"] } [profile.dev] # Rust debug is too slow. diff --git a/pcb/pcb.kicad_sch b/pcb/pcb.kicad_sch index f8aeae1..5d8e5ff 100644 --- a/pcb/pcb.kicad_sch +++ b/pcb/pcb.kicad_sch @@ -1678,6 +1678,16 @@ (color 0 0 0 0) (uuid "cf8cb18c-5d85-4906-b859-cd8e38359337") ) + (wire + (pts + (xy 170.18 97.79) (xy 170.18 100.33) + ) + (stroke + (width 0) + (type default) + ) + (uuid "01dc0079-bd53-48c8-94eb-55f6ba5ce4f6") + ) (wire (pts (xy 151.13 90.17) (xy 151.13 88.9) @@ -2803,6 +2813,72 @@ ) ) ) + (symbol + (lib_id "power:GND") + (at 170.18 100.33 0) + (unit 1) + (exclude_from_sim no) + (in_bom yes) + (on_board yes) + (dnp no) + (fields_autoplaced yes) + (uuid "cc45ee7c-6fd5-49f0-a151-7ef6e29f2dd9") + (property "Reference" "#PWR09" + (at 170.18 106.68 0) + (effects + (font + (size 1.27 1.27) + ) + (hide yes) + ) + ) + (property "Value" "GND" + (at 170.18 105.41 0) + (effects + (font + (size 1.27 1.27) + ) + ) + ) + (property "Footprint" "" + (at 170.18 100.33 0) + (effects + (font + (size 1.27 1.27) + ) + (hide yes) + ) + ) + (property "Datasheet" "" + (at 170.18 100.33 0) + (effects + (font + (size 1.27 1.27) + ) + (hide yes) + ) + ) + (property "Description" "Power symbol creates a global label with name \"GND\" , ground" + (at 170.18 100.33 0) + (effects + (font + (size 1.27 1.27) + ) + (hide yes) + ) + ) + (pin "1" + (uuid "dfe0cbec-99f9-40e8-9902-5e595ac03b10") + ) + (instances + (project "" + (path "/f8392987-3ddd-437f-94e8-8fae610849f1" + (reference "#PWR09") + (unit 1) + ) + ) + ) + ) (symbol (lib_id "custom_symbols:BME680_Module") (at 168.91 88.9 90) diff --git a/src/main.rs b/src/main.rs index eb61cc9..6cd9efc 100644 --- a/src/main.rs +++ b/src/main.rs @@ -7,12 +7,20 @@ )] #![deny(clippy::large_stack_frames)] +use core::cell::RefCell; + +use alloc::rc::Rc; +use bosch_bme680::{Bme680, DeviceAddress}; use embassy_executor::Spawner; -use embassy_time::{Duration, Timer}; +use embassy_time::{Delay, Duration, Timer}; +use embedded_hal_bus::i2c::RcDevice; use esp_backtrace as _; use esp_hal::clock::CpuClock; use esp_hal::gpio::{Level, Output, OutputConfig}; +use esp_hal::i2c::master::{Config as I2cConfig, I2c}; use esp_hal::timer::timg::TimerGroup; +use esp_hal::{Async, Blocking}; +use scd4x::Scd4x; extern crate alloc; @@ -35,8 +43,6 @@ async fn main(spawner: Spawner) { esp_alloc::heap_allocator!(#[esp_hal::ram(reclaimed)] size: 98768); - let pin = Output::new(peripherals.GPIO2, Level::High, OutputConfig::default()); - let timg0 = TimerGroup::new(peripherals.TIMG0); esp_rtos::start(timg0.timer0); @@ -47,7 +53,21 @@ async fn main(spawner: Spawner) { esp_radio::wifi::new(&radio_init, peripherals.WIFI, Default::default()) .expect("Failed to initialize Wi-Fi controller"); + let pin = Output::new(peripherals.GPIO2, Level::High, OutputConfig::default()); + + let i2c_v33 = I2c::new(peripherals.I2C0, I2cConfig::default()) + .unwrap() + .with_scl(peripherals.GPIO22) + .with_sda(peripherals.GPIO21); + let shared_i2c_v33 = Rc::new(RefCell::new(i2c_v33)); + spawner.spawn(flash_led(pin)).unwrap(); + spawner + .spawn(read_bme_680(RcDevice::new(shared_i2c_v33.clone()))) + .unwrap(); + spawner + .spawn(read_scd_41(RcDevice::new(shared_i2c_v33.clone()))) + .unwrap(); } #[embassy_executor::task] @@ -57,3 +77,48 @@ async fn flash_led(mut pin: Output<'static>) { Timer::after(Duration::from_secs(1)).await; } } + +#[embassy_executor::task] +async fn read_bme_680(i2c: RcDevice>) { + let mut sensor = Bme680::new( + i2c, + DeviceAddress::Primary, + Delay, + &bosch_bme680::Configuration::default(), + 20, + ) + .unwrap(); + + loop { + Timer::after(Duration::from_secs(5)).await; + + let data = sensor.measure().unwrap(); + esp_println::println!("Got data: {:?}", data); + } +} + +#[embassy_executor::task] +async fn read_scd_41(i2c: RcDevice>) { + let mut sensor = Scd4x::new(i2c, Delay); + sensor.wake_up(); + sensor.stop_periodic_measurement().unwrap(); + sensor.reinit().unwrap(); + + let serial = sensor.serial_number().unwrap(); + esp_println::println!("serial: {serial:#04x}"); + + sensor.start_periodic_measurement().unwrap(); + esp_println::println!("Waiting for first measurement... (5 sec)"); + + loop { + Timer::after(Duration::from_secs(5)).await; + + let data = sensor.measurement().unwrap(); + esp_println::println!( + "CO2: {}, Temperature: {} °C, Humidity: {} RH", + data.co2, + data.temperature, + data.humidity + ); + } +}