【问题标题】:Can the Adafruit DHT22 library be modified to support Raspberry Pi 4 Model B (BCM2711)?是否可以修改 Adafruit DHT22 库以支持 Raspberry Pi 4 Model B (BCM2711)?
【发布时间】:2020-07-22 08:49:33
【问题描述】:

我找到了唯一一个用于数字输出相对湿度和温度传感器/模块 DHT22 的 Python 库:https://github.com/adafruit/DHT-sensor-library

但考虑到 /usr/local/lib/python3.7/dist-packages/Adafruit_DHT/platform_detect.py 中的函数,它不支持最新的 Raspberry 处理器 (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

强制此函数返回“Pi 3 (Model B+) / BCM2837”是否安全?

因为否则我无法导入库:

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)

主持人: 树莓派 4 B 型 2Gb 内存, Raspbian GNU/Linux 10(破坏者), Python 3.7

所有代的 Raspberry 板都(主要)兼容 GPIO,所以修复这个库可能没什么大不了的?

【问题讨论】:

    标签: python raspberry-pi iot


    【解决方案1】:

    我敢于改变上面的功能,它的工作原理!但是,如果您对不同的板版本使用不同的输出,请小心。我用的是 GPIO4。

    ...
    elif match.group(1) == 'BCM2837':
        # Pi 3b+
        return 3
    else:
        # Something else, PI 4 MODEL B
        return 3
    

    【讨论】:

    • 非常有用的答案。当我调用脚本从传感器检索数据时,这使我能够通过 RPi4 的设备检测来解决这个问题:python AdafruitDHT.py stackoverflow.com/questions/64093805/…
    【解决方案2】:

    @tconsta Raspberry pi 4b 使用 'BCM 2711' 所以我认为我们可以添加以下代码

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-04-20
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多