Raspberry Pi 4B +Python3 的 AdafruitDHT庫 修改


樹莓派 通過DHT11模塊采集環境溫濕度:

方法:1、Python + RPi.GPIO 直接使用GPIO庫來控制實現,其中在樹莓派實驗室(網站)上有實現,很多博客也有相關實現,,但是目前個人測試,都沒有成功。(猜測:python直接控制gpio的延時不好控制,設備有限,放棄此方法);

方法:2、純C語言實現,非常好,不需要辣么多累贅的東西。弊端,由於本人還需要用到python的http協議庫,所以不想去用C自己去實現。所以此方法沒有完全執行。

方法:3、Python+wiringpi2庫,仍然是在Python 里面,各種問題都被解決掉,比較好。

方法:4、C語言WiringPi.h,這個還可以,樹莓派實驗中有兩種實現方法,都測試過,基本上實現沒有障礙,主要在於程序有沒有優化(封裝函數+校驗和)。

方法:5、Python +AdafruitDHT庫,這個Adafruit庫是封裝好的,地址http://github.com/adafruit/Adafruit_Python_DHT,確實挺好的,但是(2020/10/31)截至目前,這個庫最少4年沒有更新了,里面還是樹莓派3B的適配。

本人對其進行了舔磚加瓦;

git下載下后,

修改1、setup.py

elif pi_version == 4:
        extensions.append(Extension("Adafruit_DHT.Raspberry_Pi_2_Driver",
                                    ["source/_Raspberry_Pi_2_Driver.c", "source/common_dht_read.c", "source/Raspberry_Pi_2/pi_2_dht_read.c", "source/Raspberry_Pi_2/pi_2_mmio.c"],
                                    libraries=['rt'],
                                    extra_compile_args=['-std=gnu99']))

修改2、Adafruit_DHT/platform_detect.py

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+), Raspberry Pi 4
    or not a Raspberry Pi.
    """
    # Check /proc/cpuinfo for the Hardware field value.
    # 2708 is pi 1
    # 2709 is pi 2
    # 2835 is pi 3 
    # 2837 is pi 3b+
    # 2711 is pi 4b
    # 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
    elif match.group(1) == 'BCM2711':
        # Pi 4b
        return 4
    else:
        # Something else, not a pi.
        return None

修改3、Adafruit_DHT/common.py

def get_platform():
    """Return a DHT platform interface for the currently detected platform."""
    plat = platform_detect.platform_detect()
    if plat == platform_detect.RASPBERRY_PI:
        # Check for version 1 or 2 of the pi.
        version = platform_detect.pi_version()
        if version == 1:
            from . import Raspberry_Pi
            return Raspberry_Pi
        elif version == 2:
            from . import Raspberry_Pi_2
            return Raspberry_Pi_2
        elif version == 3:
            """Use Pi 2 driver even though running on Pi 3"""
            from . import Raspberry_Pi_2
            return Raspberry_Pi_2
        elif version == 4:
            from . import Raspberry_Pi_2
            return Raspberry_Pi_2
        else:
            raise RuntimeError('No driver for detected Raspberry Pi version available!')
    elif plat == platform_detect.BEAGLEBONE_BLACK:
        from . import Beaglebone_Black
        return Beaglebone_Black
    else:
        raise RuntimeError('Unknown platform.')

修改4、source/_Raspberry_Pi_2_Driver.c

static PyObject* Raspberry_Pi_2_Driver_read(PyObject *self, PyObject *args)
{
    // Parse sensor and pin integer arguments.
    int sensor, pin;
    if (!PyArg_ParseTuple(args, "ii", &sensor, &pin)) {
        return NULL;
    }
    // Call dht_read and return result code, humidity, and temperature.
    float humidity = 0, temperature = 0;
    int result = 0;
    do{
        result = pi_2_dht_read(sensor, pin, &humidity, &temperature);
    }while(result != 0);   //主要修改邏輯,讓C語言庫專注讀取數據,避免因為Python的延時(不讓python參與底層數據的獲取工作),直接返回結果 return Py_BuildValue("iff", result, humidity, temperature);
}

 

可以返回Adafruit_Python_DHT目錄

sudo python3 setup.py install

安裝OKO  (會在Adafruit_Python_DHT\build\lib.linux-armv7l-3.7\Adafruit_DHT\Raspberry_Pi_2_Driver.cpython-37m-arm-linux-gnueabihf.so,生成動態鏈接庫,會自動拷貝到/usr/local/lib/python3.7/dist-packages/Adafruit_DHT-1.4.0-py3.7-linux-armv7l.egg/Adafruit_DHT/目錄)

進入examples目錄

測試 python3 AdafruitDHT.py  11 4  

11為傳感器類型DHT11

4為 樹莓派BCM編碼引腳號

 

可以正常獲取數據ok

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM