Skip to content

Commit e6d893e

Browse files
committed
Initial import
0 parents  commit e6d893e

File tree

6 files changed

+437
-0
lines changed

6 files changed

+437
-0
lines changed

.github/workflows/ci.yaml

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
on:
2+
- push
3+
- pull_request
4+
name: CI
5+
jobs:
6+
janitorial:
7+
name: janitorial
8+
runs-on: ubuntu-latest
9+
steps:
10+
- uses: actions/checkout@v2
11+
- uses: actions-rs/toolchain@v1
12+
with:
13+
toolchain: stable
14+
profile: minimal
15+
components: clippy
16+
override: true
17+
- uses: actions-rs/clippy-check@v1
18+
with:
19+
toolchain: stable
20+
token: ${{ secrets.GITHUB_TOKEN }}
21+
args: --all-targets --all-features -- -D warnings
22+
- uses: actions-rs/cargo@v1
23+
with:
24+
command: fmt
25+
args: --check
26+
test:
27+
name: test
28+
runs-on: ubuntu-latest
29+
strategy:
30+
matrix:
31+
rust:
32+
- stable
33+
target:
34+
- arm-unknown-linux-gnueabihf
35+
- armv7-unknown-linux-gnueabihf
36+
- aarch64-unknown-linux-gnu
37+
- x86_64-unknown-linux-gnu
38+
feature_flags:
39+
- ''
40+
- '--features std'
41+
steps:
42+
- uses: actions/checkout@v2
43+
- uses: actions-rs/toolchain@v1
44+
with:
45+
toolchain: ${{ matrix.rust }}
46+
target: ${{ matrix.target }}
47+
override: true
48+
- uses: actions-rs/cargo@v1
49+
with:
50+
use-cross: true
51+
command: test
52+
args: --release --target=${{ matrix.target }} ${{ matrix.feature_flags }}

.github/workflows/release.yaml

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
on:
2+
release:
3+
types:
4+
- published
5+
name: release
6+
jobs:
7+
release:
8+
name: Publish to crates.io
9+
runs-on: ubuntu-latest
10+
steps:
11+
- uses: actions/checkout@v2
12+
- uses: actions-rs/toolchain@v1
13+
with:
14+
toolchain: stable
15+
profile: minimal
16+
override: true
17+
- uses: actions-rs/cargo@v1
18+
with:
19+
command: publish
20+
args: --token ${{ secrets.CRATES_IO_TOKEN }}

.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
/target
2+
Cargo.lock

Cargo.toml

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
[package]
2+
name = "dht-embedded"
3+
version = "0.1.0"
4+
description = "Library for reading temperature and humidity from the DHT11 and DHT22 sensors"
5+
license = "MIT OR Apache-2.0"
6+
authors = ["Brian J. Tarricone <brian@tarricone.org>"]
7+
homepage = "https://github.com/kelnos/dht-embedded-rs"
8+
repository = "https://github.com/kelnos/dht-embedded-rs"
9+
readme = "README.md"
10+
categories = [ "embedded", "hardware-support" ]
11+
keywords = [ "temperature", "humidity", "sensor", "dht11", "dht22" ]
12+
edition = "2021"
13+
14+
[features]
15+
default = []
16+
std = []
17+
18+
[dependencies]
19+
embedded-hal = "=1.0.0-alpha.8"
20+
21+
[dev-dependencies]
22+
anyhow = "1"
23+
gpio-cdev = "0.5"
24+
linux-embedded-hal = "0.3"
25+
serial = "0.4"

README.md

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
# dht-embedded
2+
3+
`dht-embedded` is a Rust crate that reads temperature and humidity data
4+
from the DHT11 and DHT22 sensors.
5+
6+
## Prerequisites
7+
8+
* You've connected the DHT sensor to your device (such as a Raspberry
9+
Pi or ESP32) using a GPIO pin.
10+
11+
## Usage
12+
13+
Add the following to your `Cargo.toml`:
14+
15+
```toml
16+
[dependencies]
17+
dht-embedded = "=0.1.0-alpha.1"
18+
```
19+
20+
Note that this crate currently tracks the 1.0.0 alpha releases of
21+
`embedded-hal`, so things can change & break often, and your platform's
22+
`embedded-hal` implementation may not have trait impls for 1.0.0 alphas
23+
at all, let alone the current one this crate supports.
24+
25+
You will need to use an `embedded-hal` implementation for your hardware.
26+
Here's a simple one using `linux-embedded-hal` and `gpio-cdev, which
27+
could be used on a Rasperry Pi.
28+
29+
```rust,no_run,ignore
30+
use dht_embedded::{Dht22, DhtSensor, NoopInterruptControl};
31+
use gpio_cdev::{Chip, LineRequestFlags};
32+
use linux_embedded_hal::{CdevPin, Delay};
33+
use std::{thread::sleep, time::Duration};
34+
35+
fn main() -> anyhow::Result<()> {
36+
let mut gpiochip = Chip::new("/dev/gpiochip0")?;
37+
let line = gpiochip.get_line(17)?;
38+
let handle = line.request(LineRequestFlags::INPUT | LineRequestFlags::OUTPUT, 1, "dht-sensor")?;
39+
let pin = CdevPin::new(handle)?;
40+
let mut sensor = Dht22::new(NoopInterruptControl, Delay, pin);
41+
42+
loop {
43+
match sensor.read() {
44+
Ok(reading) => println!("{}°C, {}% RH", reading.temperature(), reading.humidity()),
45+
Err(e) => eprintln!("Error: {}", e),
46+
}
47+
48+
sleep(Duration::from_millis(2100));
49+
}
50+
}
51+
```
52+
53+
(To be fair, the Linux kernel includes a driver for DHT sensors, and
54+
honestly it's probably better to use that driver, since kernel space can
55+
disable interrupts and get much more precise timing than we can.)
56+
57+
## Why
58+
59+
A search of crates.io might yield several different implementations of
60+
this driver. I wrote this because none of the others worked for me,
61+
and, upon examination of their code, I found they used a completely
62+
different protocols for reading from the sensor, protocols I couldn't
63+
find documented anywhere as what's supposed to work. This crate
64+
implements one of the simpler protocols that doesn't require access to a
65+
system clock, but still seems to work most of the time.

0 commit comments

Comments
 (0)