rust-bananapi-dht11
An experiment to implement a DHT11 (temperature/humidity sensor) driver on a Banana Pi using Rust.
Wiring
After the resistor VCC/GND setup, communication is done through an GPIO pin.
Image source
DHT11 Protocol
The DHT11 uses the same pin for Input and Output. The protocols starts by the controller signalling the start of the reading. Then the communication flow inverts, with the DHT11 sending some sort of header, followed by the readings (humidity and then temperature). Bits are indicate by the time the pin is left high or low. More details can be found here.
Implementation
The implementation is implemented in Rust and is quite naive, in the sense that nothing like interrupts or timers are used. When writing, it just updates pin values and sleeps. On reading, it polls the pin level and checks timestamps. It means that if the Linux scheduler doesn't cooperates, the timing will be compromised, and reading will fail.
The Banana Pi is running the latest version of Bananian, a Debian-based distro tailored for it.
The implementation depends on the Banana Pi's GPIO library, available here.
Pin #1 is being used.
Compilation and Running
Instead of installing the whole toolchain on my machine, I'm using a pre-backed Docker image: rustcross/armv7-unknown-linux-gnueabihf
.
To get started, you can launch an instance of the image with:
cd "$project"
docker run --rm -v "$PWD:/work" rustcross/armv7-unknown-linux-gnueabihf:latest bash
Once there, you first need to compile the GPIO library:
cd
git clone https://github.com/LeMaker/WiringBP -b bananapi
cd WiringBP/wiringPi
make CC=arm-linux-gnueabihf-gcc
make install
Afterwards, you can build the project using Cargo:
cd /work
cargo build --target=armv7-unknown-linux-gnueabihf --release
This generates a binary on the target dir (target/armv7-unknown-linux-gnueabihf/release/dht11
) that can be copied and run inside the Banana Pi.