10

I'm running the latest raspberry pi os "Linux raspberrypi 5.4.51-v7l+ #1327 SMP Thu Jul 23 11:04:39 BST 2020 armv7l GNU/Linux" on a Raspberrypi 4B 4Gb.

I've installed Python3 sudo apt-get install python3-dev python3-pip

Updated setuptools, wheel and pip sudo python3 -m pip install --upgrade pip setuptools wheel

And installed Adafruit_DHT module sudo pip3 install Adafruit_DHT

After that i've connected my DHT22 to my rpi on gpio4 and created the following python script:

import Adafruit_DHT
import time
from datetime import datetime

DHT_SENSOR = Adafruit_DHT.DHT22
DHT_PIN = 4
PROBE_NAME = "PI4"

humidity, temperature = Adafruit_DHT.read_retry(DHT_SENSOR, DHT_PIN)

if humidity is not None and temperature is not None:
    print("{2} - T={0:0.1f} H={1:0.1f}".format(temperature, humidity, datetime.now()))
else:
    print("Failed to retrieve data from humidity sensor")

Than i run it sudo python3 temp.py

and i get the following error

Traceback (most recent call last):
  File "temp.py", line 11, in <module>
    humidity, temperature = Adafruit_DHT.read_retry(DHT_SENSOR, DHT_PIN)
  File "/usr/local/lib/python3.7/dist-packages/Adafruit_DHT/common.py", line 94, in read_retry
    humidity, temperature = read(sensor, pin, platform)
  File "/usr/local/lib/python3.7/dist-packages/Adafruit_DHT/common.py", line 80, in read
    platform = get_platform()
  File "/usr/local/lib/python3.7/dist-packages/Adafruit_DHT/common.py", line 60, in get_platform
    from . import Beaglebone_Black
  File "/usr/local/lib/python3.7/dist-packages/Adafruit_DHT/Beaglebone_Black.py", line 24, in <module>
    from . import Beaglebone_Black_Driver as driver
ImportError: cannot import name 'Beaglebone_Black_Driver' from 'Adafruit_DHT' (/usr/local/lib/python3.7/dist-packages/Adafruit_DHT/__init__.py)

Any idea how to get it working?

I've done the exact same steps on a raspberry pi zero w and it works out of the box

Flavio CF Oliveira
  • 5,235
  • 13
  • 40
  • 63
  • I would guess this is an environment variable issue. – Jortega Aug 03 '20 at 15:14
  • Or a problem with detecting the correct hardware. The hardware identification in /proc/cpuinfo seems to have been updated with the latest OS patches (reported elsewhere, haven't confirmed myself yet). – PMF Aug 04 '20 at 16:50

5 Answers5

20

In "/usr/local/lib/python3.7/dist-packages/Adafruit_DHT/platform_detect.py", you can add the followings at line #112 in the elif ladder, so it should workaround the issue.

elif match.group(1) == 'BCM2711':
    return 3

It looks like the hardware name in the /proc/cpuinfo has been changed due to the recent raspbian upgrade.

5

The solution of Kotaro Hashimoto works. I had the same problem with my Pi4.

The real problem is that AdaFruit no longer supports nor updates this old Adafruit_DHT library. The new library from AdaFruit for this sensor is "Adafruit_CircuitPython_DHT" can be found here. It could be a good idea to update your code to this new library.

https://github.com/adafruit/Adafruit_CircuitPython_DHT

DavidE
  • 51
  • 2
  • Currently it's buggy and didn't work for my `DHT22_AM2302` sensor while the Kotaro's solution worked without issues. – minerals Oct 10 '20 at 17:57
  • The `adafruit_dht.DHT22` works for both `DHT22` and `AM2302`. However, there is an issue of synchronicity in that if you instantiate the class and immediately after access either of the `.temperature`, `.humidity` dynamic attributes, the sensor is not ready. A split second sleep fixes it. In the old library `Adafruit_DHT`, this would give 0, 0. – Matteo Ferla Dec 19 '20 at 11:15
  • Also, using the `use_pulseio=False` argument, e.g. `dht = adafruit_dht.DHT22(board.D4, use_pulseio=False)` makes the sensor crash as opposed to hanging in a way similar to the `Adafruit_DHT` module – Matteo Ferla Dec 19 '20 at 16:01
3

After Raspberry Pi OS released, and Adafruit DHT library is deprecated, I have the same issue.

I solved it by using the new released library from Adafruit.

Adafruit CircuitPython DHT

Try this example:

import adafruit_dht
from board import *

# GPIO17
SENSOR_PIN = D17

dht22 = adafruit_dht.DHT22(SENSOR_PIN, use_pulseio=False)

temperature = dht22.temperature
humidity = dht22.humidity

print(f"Humidity= {humidity:.2f}")
print(f"Temperature= {temperature:.2f}°C")

Output:

Humidity= 65.70
Temperature= 22.30°C

Considering my environment:

  • Raspberry Pi 4 Model B (RAM 4GB)
  • Python 3.7
  • Raspberry Pi OS
  • DHT 22 (AM2302)
Meqdad Dev
  • 131
  • 1
  • 2
  • 10
  • Which library do you recommend using a Raspberry Zero WH? The "Adafruit CircuitPython DHT" does not work on Raspberry Zero WH. – Javan R. Aug 14 '21 at 14:15
  • Sorry @JavanR. , but currently I don't have Raspberry Pi Zero WH to test for you, but I've seen some tutorials are using the same library, so maybe you need to check out your environment again. Take a look on this article from [here](https://peppe8o.com/using-raspberry-pi-with-dht11-temperature-and-humidity-sensor-and-python/). I hope this helps. Best luck – Meqdad Dev Sep 01 '21 at 08:56
0

I tried using circuitpython and did as instructed, it all worked ok, until i stopped the python script and tried restarting it only to get errors like:

Unable to set line 18 to input Timed out waiting for PulseIn message

and i just dont have the knowledge to resolve it in code - searching the web appears like many other people get the same issue, and killing process's every time or rebooting to fix it just isnt viable

IMHO the circuit python way is either rubbish, buggy or not properly documented, (probably all 3), so i did the fix above and used the old library which works.

WinBase
  • 31
  • 1
  • 3
0
  • Rasperry pi 4 model b
  • dht11
import time
import Adafruit_DHT  #Adafruit_DHT , adafruit_dht both are needed
import adafruit_dht
from board import *


DHT_PIN = D17       # GPIO17
dht11 = adafruit_dht.DHT11(DHT_PIN, False)

while True:
    try:
        dht11.measure()
        temp = dht11.temperature
        humid = dht11.humidity

        if humid is not None and temp is not None:
            print(f"temp= {temp:.2f}°C")
            print(f"humid= {humid:.2f}")
        else:
            print("failed to get reading from the sensor.Try again!")
        
    except RuntimeError as error:
        print("runtime error: "+str(error.args[0]))
    time.sleep(1.0)

output:

humid= 26.00
runtime error: Checksum did not validate. Try again.
temp= 22.00°C
humid= 26.00
Zrn-dev
  • 99
  • 5