DHT11 on Rust: Timeout Error

December 22, 2023, 19:20

josephfourier

I've been trying to use a DHT11 temperature sensor with rppal. My code is the following:
rust
fn main() {
    let mut pin = Gpio::new().unwrap().get(18).unwrap().into_io(Mode::Output);
    let mut led = Gpio::new().unwrap().get(17).unwrap().into_output();
    let mut sensor = Dht11::new(pin);
    for _ in 0..10 {
        led.set_high();
        let measurement = sensor.perform_measurement(&mut Delay::new());
        println!("{:?}", measurement);
        led.set_low();
        sleep(Duration::from_secs(1));
    }
}
but measurement keeps being set to Err(Timeout). I have tried using different IO modes and using different libraries too. This crate is dht11, but I have also tried dht-sensor and simple-dht11, to no avail. I also haven't managed to get a measurement with a different language, Python kept crashing in the venv because it couldn't detect the environment. I connected the VCC to the 3.3V supply, the ground to GND and the data line to GPIO 18. I'm pretty confident it's correctly connected cause I have an LED on my DHT11 that lights up, but I cannot make any measurements whatsoever.

oops.se

The code is incomplete.

josephfourier

Here's the complete main.rs file:
rust
extern crate rppal;
extern crate dht11;

use std::thread::sleep;
use std::time::Duration;
use dht11::Dht11;
use rppal::gpio::Gpio;
use rppal::hal::Delay;

fn main() {
    let pin = Gpio::new().unwrap().get(18).unwrap().into_output_low();
    let mut led = Gpio::new().unwrap().get(17).unwrap().into_output();
    let mut sensor = Dht11::new(pin);
    let mut delay = Delay::new();
    for _ in 0..10 {
        // led.set_high();
        let measurement = sensor.perform_measurement(&mut delay);
        println!("{:?}", measurement);
        led.set_low();
        sleep(Duration::from_secs(1));
    }
}
Here's my cargo.toml file:
toml
[package]
name = "temp-read"
version = "0.1.0"
edition = "2021"

# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

[dependencies]
rppal = {  version = "0.16.0", features = ["hal", "hal-unproven"] }
dht11 = "0.3.1"
simple-dht11 = "0.1.2"
And here's how I crosscompile and deploy to my raspberry:
bash
set -o errexit
set -o nounset
set -o pipefail
set -o xtrace

readonly TARGET_HOST=192.168.1.17
readonly TARGET_PATH=/home/alex/Downloads/temp-read
readonly TARGET_ARCH=aarch64-unknown-linux-gnu
readonly SOURCE_PATH=./target/${TARGET_ARCH}/release/temp-read

cargo zigbuild --release --target=${TARGET_ARCH}
rsync ${SOURCE_PATH} ${TARGET_HOST}:${TARGET_PATH}
ssh -t ${TARGET_HOST} ${TARGET_PATH}

josephfourier

I also tried building and running directly on a raspberry (no crosscompilation)

josephfourier

Also, here's how I connected everything. White is the 3.3V supply, black/orange is the ground and red is the data pin at GPIO 17.