Skip to content

DHT does not work with root user using Raspberry Zero WH #73

Open
@JavanXD

Description

@JavanXD

I am using a clean Raspberry Pi OS install (version: 2021-03-04-raspbian-buster) on a Raspberry Zero WH. You can download the image or you can check the used install instructions here.

I installed the following two packages: sudo pip3 install adafruit-circuitpython-dht && sudo apt-get install libgpiod2

PoC:

import logging
import adafruit_dht
import time
import board
import digitalio

logger = logging.getLogger('HoneyPi.read_dht')
if __name__ == '__main__':
    try:

        SENSOR_PIN = digitalio.Pin(4) # change GPIO pin
        dht = adafruit_dht.DHT22(SENSOR_PIN, use_pulseio=True)

        timer = 0
        while timer <= 10:
            try:
                temperature = dht.temperature
                temperature_f = temperature * (9 / 5) + 32
                humidity = dht.humidity

                print("Temp: {:.1f} F / {:.1f} °C    Humidity: {}% ".format(temperature_f, temperature, humidity))

                break # break while if it worked
            except RuntimeError as error:
                # Errors happen fairly often, DHT's are hard to read, just keep going
                print(error.args[0])
                time.sleep(2.0)
                timer = timer + 2

            if timer > 10: # end reached
                print("Loop finished. Error.")

    except (KeyboardInterrupt, SystemExit):
       pass

    except RuntimeError as error:
        logger.error("RuntimeError in DHT measurement: " + error.args[0])

    except Exception as e:
       logger.exception("Unhandled Exception in DHT measurement")

(full script)

Console:

pi@HoneyPi:~/HoneyPi/rpi-scripts $ sudo -u root python3 read_dht.py
RuntimeError in DHT measurement: Timed out waiting for PulseIn message. Make sure libgpiod is installed.
pi@HoneyPi:~/HoneyPi/rpi-scripts $ sudo -u pi python3 read_dht.py
Temp: 78.3 F / 25.7 °C    Humidity: 66.2% 

Notes:
yes, root user is part of gpio group (sudo usermod -aG gpio root)

I noticed that I am not the only one affected by this issue:

I have been using the Adafruit_DHT library for a long time but because you stopped fixing issues such as this I've migrated to your new library. Unfortunately it turns out that you do not fix issues for Raspi Zero. What I am supposed to use now? Please fix this issue.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions