Skip to content

Commit d361a07

Browse files
committed
feat: return sensor reading; update readme
1 parent 6194b5f commit d361a07

File tree

4 files changed

+81
-20
lines changed

4 files changed

+81
-20
lines changed

Cargo.lock

Lines changed: 0 additions & 7 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,5 +6,4 @@ license = "Apache-2.0"
66

77
[dependencies]
88
embedded-hal = "1.0.0"
9-
panic-halt = "0.2.0"
109

README.md

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,61 @@
11
# embedded-dht-rs
22

3+
Welcome to `embedded-dht-rs`, a Rust library designed to make working with DHT sensors a breeze!
4+
5+
This library only depends on `embedded_hal`, making it versatile and compatible with virtually any microcontroller.
6+
7+
### Features:
8+
- **DHT11 sensor support**: Fully implemented and ready to use.
9+
- **DHT22 sensor support**: Currently in progress – stay tuned!
10+
11+
We’ve tested it with the ESP32-WROOM, and you can find a detailed example below to help you get started.
12+
13+
## Getting Started
14+
15+
### Example
16+
17+
```rust
18+
#![no_std]
19+
#![no_main]
20+
21+
use embedded_dht_rs::Dht11;
22+
use esp_backtrace as _;
23+
use esp_hal::{
24+
clock::ClockControl,
25+
delay::Delay,
26+
gpio::{Io, Level, OutputOpenDrain, Pull},
27+
peripherals::Peripherals,
28+
prelude::*,
29+
system::SystemControl,
30+
};
31+
#[entry]
32+
fn main() -> ! {
33+
let peripherals = Peripherals::take();
34+
let system = SystemControl::new(peripherals.SYSTEM);
35+
let clocks = ClockControl::boot_defaults(system.clock_control).freeze();
36+
let io = Io::new(peripherals.GPIO, peripherals.IO_MUX);
37+
38+
esp_println::logger::init_logger_from_env();
39+
40+
let delay = Delay::new(&clocks);
41+
42+
let gpio4 = OutputOpenDrain::new(io.pins.gpio4, Level::High, Pull::None);
43+
let mut led_sensor = Dht11::new(gpio4, delay);
44+
45+
loop {
46+
delay.delay(1000.millis());
47+
let reading = led_sensor.read().unwrap();
48+
log::info!(
49+
"Temperature: {}, humidity: {}",
50+
reading.temperature,
51+
reading.humidity
52+
);
53+
}
54+
}
55+
```
56+
57+
---
58+
359
![steps](/docs/steps.png)
460

561
## Step 1

src/lib.rs

Lines changed: 25 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -2,23 +2,28 @@
22

33
use embedded_hal::{
44
delay::DelayNs,
5-
digital::{ErrorType, InputPin, OutputPin, PinState}
5+
digital::{ErrorType, InputPin, OutputPin, PinState},
66
};
77

88
pub struct Dht11<P: InputPin + OutputPin, D: DelayNs> {
99
pin: P,
1010
delay: D,
1111
}
1212

13+
pub struct SensorReading {
14+
pub humidity: u8,
15+
pub temperature: i8,
16+
}
17+
1318
impl<P: InputPin + OutputPin, D: DelayNs> Dht11<P, D> {
1419
pub fn new(pin: P, delay: D) -> Self {
15-
Self { pin, delay }
20+
Self { pin, delay }
1621
}
1722

18-
pub fn read(&mut self) -> Result<bool, <P as ErrorType>::Error> {
23+
pub fn read(&mut self) -> Result<SensorReading, <P as ErrorType>::Error> {
1924
// Start communication: pull pin low for 18ms, then release.
2025
let _ = self.pin.set_low();
21-
self.delay.delay_ms(18);
26+
self.delay.delay_ms(18);
2227
let _ = self.pin.set_high();
2328

2429
// Wait for sensor to respond.
@@ -36,12 +41,13 @@ impl<P: InputPin + OutputPin, D: DelayNs> Dht11<P, D> {
3641
let checksum = self.read_byte();
3742

3843
// TODO
39-
panic!("{:?} {:?} {:?} {:?} {:?}", humidity_integer, humidity_decimal, temperature_integer, temperature_decimal, checksum);
40-
41-
return self.pin.is_high();
44+
// panic!("{:?} {:?} {:?} {:?} {:?}", humidity_integer, humidity_decimal, temperature_integer, temperature_decimal, checksum);
45+
Ok(SensorReading {
46+
humidity: humidity_integer.unwrap(),
47+
temperature: temperature_integer.unwrap() as i8,
48+
})
4249
}
4350

44-
4551
fn read_byte(&mut self) -> Result<u8, <P as ErrorType>::Error> {
4652
let mut byte: u8 = 0;
4753
for n in 0..8 {
@@ -64,15 +70,22 @@ impl<P: InputPin + OutputPin, D: DelayNs> Dht11<P, D> {
6470
/// # Arguments
6571
///
6672
/// * `state` - The target `PinState` to wait for (either `Low` or `High`).
67-
fn wait_until_state(&mut self, state: PinState) -> Result<(), <P as ErrorType>::Error>{
73+
fn wait_until_state(&mut self, state: PinState) -> Result<(), <P as ErrorType>::Error> {
6874
loop {
6975
match state {
70-
PinState::Low => if self.pin.is_low()? { break; },
71-
PinState::High => if self.pin.is_high()? { break; }
76+
PinState::Low => {
77+
if self.pin.is_low()? {
78+
break;
79+
}
80+
}
81+
PinState::High => {
82+
if self.pin.is_high()? {
83+
break;
84+
}
85+
}
7286
};
7387
self.delay.delay_us(1);
7488
}
7589
Ok(())
7690
}
7791
}
78-

0 commit comments

Comments
 (0)