Crates.io | dht-mmap-rust |
lib.rs | dht-mmap-rust |
version | 0.1.0 |
source | src |
created_at | 2024-05-12 18:46:57.874351 |
updated_at | 2024-05-12 18:46:57.874351 |
description | Simple access to DHT11 and DHT22 sensors for the Raspberry PI |
homepage | |
repository | https://github.com/krusema/dht-mmap-rust |
max_upload_size | |
id | 1237717 |
size | 20,114 |
This library enables simple reading of DHT11 and DHT22 temperature+humidity sensors using the GPIO pins on a
Raspberry PI.
In order to achieve sufficiently fast memory access, this library directly accesses the memory registers for pin
control.
I wrote this library because the other libraries I tried did not work on my PI. This library should work out of the box.
The file /dev/gpiomem
needs to be accessible, so the program either needs to be run as root or have group-based access
to the file.
If you want to run your program to run on a user other than root,
this StackOverflow answer has instructions.
This is already configured by default on raspbian.
fn main() {
// The sensor is a DHT11 connected on pin 23
let mut dht = Dht::new(DhtType::Dht11, 23).unwrap();
// Important: DHT sensor reads fail sometimes. In an actual program, if a read fails you should retry multiple times until
// the read succeeds.
// For more information, see documentation on `read()`
let reading = dht.read().unwrap();
println!(
"Temperature {} °C, Humidity {}%RH",
reading.temperature(),
reading.humidity()
);
}
This repo contains two tests,
one that assumes a DHT11 is connected on GPIO pin 2 and
one that assumes a DHT22 is connected on GPIO pin 3.
This library works on Raspberry PIs and not other embedded devices such as Arduino and the likes. That is why it needs so little boilerplate.