Skip to content

Commit 6194b5f

Browse files
committed
feat: read 5 bytes from dht sensor
1 parent a11dad6 commit 6194b5f

File tree

5 files changed

+37
-2
lines changed

5 files changed

+37
-2
lines changed

Cargo.lock

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

Cargo.toml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,3 +6,5 @@ license = "Apache-2.0"
66

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

README.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,3 +29,7 @@ The DHT11 outputs 40 bits of data through the DATA pin, and the microprocessor r
2929

3030
After outputting a low signal for 50 microseconds, the DHT11 completes sending the 40 bits of data and switches the DATA pin back to input mode, which, along with the pull-up resistor, returns to a high state. Meanwhile, the DHT11 internally re-measures the environmental temperature and humidity, records the new data, and waits for the next external signal.
3131

32+
33+
## Example Schematic
34+
35+
![step3](/docs/example_esp32_dht11.png)

docs/example_esp32_dht11.png

132 KB
Loading

src/lib.rs

Lines changed: 24 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
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> {
@@ -28,13 +28,35 @@ impl<P: InputPin + OutputPin, D: DelayNs> Dht11<P, D> {
2828
let _ = self.wait_until_state(PinState::High);
2929
let _ = self.wait_until_state(PinState::Low);
3030

31-
// Start reading
31+
// Start reading 40 bits
32+
let humidity_integer = self.read_byte();
33+
let humidity_decimal = self.read_byte();
34+
let temperature_integer = self.read_byte();
35+
let temperature_decimal = self.read_byte();
36+
let checksum = self.read_byte();
3237

3338
// TODO
39+
panic!("{:?} {:?} {:?} {:?} {:?}", humidity_integer, humidity_decimal, temperature_integer, temperature_decimal, checksum);
3440

3541
return self.pin.is_high();
3642
}
3743

44+
45+
fn read_byte(&mut self) -> Result<u8, <P as ErrorType>::Error> {
46+
let mut byte: u8 = 0;
47+
for n in 0..8 {
48+
let _ = self.wait_until_state(PinState::High);
49+
self.delay.delay_us(30);
50+
let is_bit_1 = self.pin.is_high();
51+
if is_bit_1.unwrap() {
52+
let bit_mask = 1 << (7 - (n % 8));
53+
byte |= bit_mask;
54+
let _ = self.wait_until_state(PinState::Low);
55+
}
56+
}
57+
Ok(byte)
58+
}
59+
3860
/// Waits until the pin reaches the specified state.
3961
///
4062
/// This helper function continuously polls the pin until it reaches the desired `PinState`.

0 commit comments

Comments
 (0)