ラズベリーパイとDHT11温度センサー


1.はじめに

  Raspberry PiのGPIO操作に慣れるために、小さなケースを見つけましたが、最近解決されたばかりの問題が発生したので、それを記録しました。

2.ハードウェアの準備

1.DHT11温度および湿度センサー

DHT11
現在市販されているDHT11ピンは3ピンと4ピンになっていますが、1ピンが空なのですべて同じように使用しています。3ピン購入しました。
ピンダイアグラム

2.ラズベリーパイ4B

ラズベリーパイ4B
拡張ポート:
Raspberry Pi4B拡張インターフェース

ピン配線:(現在の配線は任意です。DATA位置と正極と負極が逆に接続されていないことに注意してください)

  • VCC-ピン1
  • 日付-ピン3
  • GND-ピン6

3、ソフトウェアの準備

トニー

システムに付属のPythonIDEを使用する
ここに画像の説明を挿入

4、プログラム設計

1.Adafruitを使用してDHT11温度および湿度センサーを読み取ります

①、ソフトウェアのインストール

開始する前に、パッケージを更新する必要があります。

sudo apt-get update
sudo apt-get install build-essential python-dev

GitHubからAdafruitライブラリを入手します。

sudo git clone https://github.com/adafruit/Adafruit_Python_DHT.git
cd Adafruit_Python_DHT

Python2およびPython3のライブラリをインストールします。

sudo python setup.py install
sudo python3 setup.py install

②、サンプルプログラムを使用してください

サンプルプログラム
Adafruitはサンプルプログラムを提供しています。次のコマンドを実行してテストします。

cd ~
cd Adafruit_Python_DHT
cd examples
python AdafruitDHT.py 11 2

サンプルプログラム

③、Adafruitライブラリを使用したカスタムPythonプログラム

もちろん、このライブラリを独自のPythonプログラムで使用して、呼び出しを実装することもできます。

import Adafruit_DHT
  
# Set sensor type : Options are DHT11,DHT22 or AM2302
sensor=Adafruit_DHT.DHT11
  
# Set GPIO sensor is connected to
gpio=2
  
# Use read_retry method. This will retry up to 15 times to
# get a sensor reading (waiting 2 seconds between each retry).
humidity, temperature = Adafruit_DHT.read_retry(sensor, gpio)
  
# Reading the DHT11 is very sensitive to timings and occasionally
# the Pi might fail to get a valid reading. So check if readings are valid.
if humidity is not None and temperature is not None:
  print('Temp={0:0.1f}*C  Humidity={1:0.1f}%'.format(temperature, humidity))
else:
  print('Failed to get reading. Try again!')

ここに画像の説明を挿入

④温度と湿度をリアルタイムでサイクリング表示するためのカスタムPythonプログラム

#!/usr/bin/python
# *-*- coding:utf-8 -*-*
import Adafruit_DHT

class DHT11:
    def DHT11_Read(self):
        sensor = Adafruit_DHT.DHT11
        gpio = 2
        humidity, temperature = Adafruit_DHT.read_retry(sensor, gpio)
        return humidity,temperature

if __name__ == '__main__':
    float humid,temp
    dht11 = DHT11()
    while True:
        humid,temp=dht11.DHT11_Read()
        if humid is not None and temp is not None:
            print('Temp={0:0.1f}*C  Humidity={1:0.1f}%'.format(temp, humid))
        else:
            print('Failed to get reading. Try again!')

ここに画像の説明を挿入

2.GPIOを使用してDHT11温度および湿度センサーを読み取ります

import RPi.GPIO as GPIO
import time

channel =2 # 上文提到的GPIO编号 
data = []
j = 0

GPIO.setmode(GPIO.BCM)
time.sleep(1)
GPIO.setup(channel, GPIO.OUT)
GPIO.output(channel, GPIO.LOW)
time.sleep(0.02)
GPIO.output(channel, GPIO.HIGH)
GPIO.setup(channel, GPIO.IN)
while GPIO.input(channel) == GPIO.LOW:
    continue
while GPIO.input(channel) == GPIO.HIGH:
    continue

while j < 40:
    k = 0
    while GPIO.input(channel) == GPIO.LOW:
        continue
    while GPIO.input(channel) == GPIO.HIGH:
        k += 1
    if k > 100:
        break
    if k < 8:
        data.append(0)
    else:
        data.append(1)
    j += 1

print ("sensor is working.")

humidity_bit = data[0:8]
humidity_point_bit = data[8:16]
temperature_bit = data[16:24]
temperature_point_bit = data[24:32]
check_bit = data[32:40]

humidity = 0
humidity_point = 0
temperature = 0
temperature_point = 0
check = 0

for i in range(8):
    humidity += humidity_bit[i] * 2 ** (7-i)
    humidity_point += humidity_point_bit[i] * 2 ** (7-i)
    temperature += temperature_bit[i] * 2 ** (7-i)
    temperature_point += temperature_point_bit[i] * 2 ** (7-i)
    check += check_bit[i] * 2 ** (7-i)

tmp = humidity + humidity_point + temperature + temperature_point

if check == tmp:
    print ("temperature :", temperature, "*C, humidity :", humidity, "%")
    res='{value:%f}'% temperature
    import json
    with open('/home/pi/Desktop/data.txt', 'a') as outfile:
        json.dump(res, outfile)
    outest=open('/home/pi/Desktop/data.txt','a')
    outest.write(res)
    outest.close
    print(res)
else:
    print ("wrong")
GPIO.cleanup()

問題ないのは当然ですが、結果を表示できず、温度と湿度の両方が255で表示され、データビットが役に立たないので、答えに精通している友人を歓迎します。
ここに画像の説明を挿入

3.発生した問題

①、Adafruit_DHTを使用したRaspberry Pi4Bは温度を正しく表示できません

  これは、ダウンロードしたAdafruit_DHTドライバーが4bプロセッサーBCM2711をサポートしていないため/home/pi/Adafruit_Python_DHT/Adafruit_DHT/platform_detect.pyです。手動で開き、赤い円の中に次のコードを追加する必要があります。

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

ここに画像の説明を挿入
  次に、Adafruit_Python_DHTディレクトリに戻り、Adafruit Python DHTセンサー機能ライブラリを再インストールし、サンプルプログラムを再度実行して、正しい温度と湿度を表示します。

cd Adafruit_Python_DHT/
sudo python setup.py install

おすすめ

転載: blog.csdn.net/qq_41071754/article/details/114122576