1

I found the only one Python library for the Digital-output relative humidity & temperature sensor/module DHT22: https://github.com/adafruit/DHT-sensor-library

But considering a function in /usr/local/lib/python3.7/dist-packages/Adafruit_DHT/platform_detect.py it's not supporting the latest Raspberry processor (BCM2711):

def pi_version():
"""Detect the version of the Raspberry Pi.  Returns either 1, 2, 3 or
None depending on if it's a Raspberry Pi 1 (model A, B, A+, B+),
Raspberry Pi 2 (model B+), Raspberry Pi 3,Raspberry Pi 3 (model B+) or not $
"""
# Check /proc/cpuinfo for the Hardware field value.
# 2708 is pi 1
# 2709 is pi 2
# 2835 is pi 3
# 2837 is pi 3b+
# Anything else is not a pi.
with open('/proc/cpuinfo', 'r') as infile:
    cpuinfo = infile.read()
# Match a line like 'Hardware   : BCM2709'
match = re.search('^Hardware\s+:\s+(\w+)$', cpuinfo,
                  flags=re.MULTILINE | re.IGNORECASE)
if not match:
    # Couldn't find the hardware, assume it isn't a pi.
    return None
if match.group(1) == 'BCM2708':
    # Pi 1
    return 1
elif match.group(1) == 'BCM2709':
    # Pi 2
    return 2
elif match.group(1) == 'BCM2835':
    # Pi 3
    return 3
elif match.group(1) == 'BCM2837':
    # Pi 3b+
    return 3
else:
    # Something else, not a pi.
    return None

Is it safe to force this function to return "Pi 3 (Model B+) / BCM2837" ?

Because otherwise I cannot import the library:

Traceback:

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)

Host: Raspberry Pi 4 Model B 2Gb RAM, Raspbian GNU/Linux 10 (buster), Python 3.7

All generations of Raspberry boards are GPIO compatible (mainly), so maybe it's not a big deal to fix this library?

tconsta
  • 33
  • 2
  • 7

2 Answers2

2

I dared to change the above function and it works! But BE CAREFUL if you use outputs that are different for different board versions. I used GPIO4.

...
elif match.group(1) == 'BCM2837':
    # Pi 3b+
    return 3
else:
    # Something else, PI 4 MODEL B
    return 3
tconsta
  • 33
  • 2
  • 7
  • 1
    Very useful answer. This allowed me to solve this problem with device detection of my RPi4 when I call the script to retrieve data from my sensor: python AdafruitDHT.py https://stackoverflow.com/questions/64093805/adafruit-python-dht-raspberry-pi-in-get-platform-runtimeerror-unknown-platf/64254079#64254079 – Tms91 Oct 08 '20 at 07:59
0

@tconsta Raspberry pi 4b uses 'BCM 2711' so I think we can add the below code

...
elif match.group(1) == 'BCM2711':
    #Pi 4b
    return 3
else:
    return None