프로그래밍/Python

[Python] 라즈베리4 Adafruit DHT11/21/22(온습도센서)

Beginner:) 2021. 11. 8.
320x100

환경은 라즈베리4, 라즈비안os, 파이썬 3.7이다.

 

각각 정확한 버전은 잘 모르겠다.

 

환경설정은 준비되었다 하고 

 

아래를 참고하여 온습도센서 라이브러리를 다운받을 수 있다.

 

https://github.com/adafruit/Adafruit_Python_DHT

 

GitHub - adafruit/Adafruit_Python_DHT: Python library to read the DHT series of humidity and temperature sensors on a Raspberry

Python library to read the DHT series of humidity and temperature sensors on a Raspberry Pi or Beaglebone Black. - GitHub - adafruit/Adafruit_Python_DHT: Python library to read the DHT series of hu...

github.com

 

 

여기서는 그냥 pip를 통해 설치한다.

(다시 해보니 오류가 뜬다. 가상환경관련인거같은데 나는 가상환경을 사용하지 않을 것이다.

아래의 레퍼지토리방식으로 사용하거나 오류를 해결하고 pip로 설치하자)

sudo pip3 install Adafruit_DHT

 

만약 레퍼지토리로 다운받는다면 아래의 명령어로 설치도 완료해야한다. (본인의 경로 설정!)

https://github.com/adafruit/Adafruit_Python_DHT

git clone https://github.com/adafruit/Adafruit_Python_DHT.git
cd Adafruit_Python_DHT
sudo python3 setup.py install

 

cannot import name 'Beaglebone_Black_Driver' from 'Adafruit_DHT' 에러가 뜬다면

 

아마 라즈베리4일 것이다. 

 

아래는 라즈베리4가 나오면 Adafruit측에서도 라즈베리4에 대한 업데이트를 해줘야하는데 

 

작성일 기준 아직 업데이트가 안되었다. 

 

각 라즈베리마다 칩셋명이 있는데 라즈베리4의 칩셋명이 BCM2711이다. 

 

해당 칩셋명을 추가하기 위해 아래와 같이 vim 또는 nano를 통해 해당 라이브러리를 접근한뒤 

sudo vim /usr/local/lib/python3.7/dist-packages/Adafruit_DHT/platform_detect.py
반응형

 

111번째 줄에 아래와 같이 라즈베리4에 대한 칩셋 코드를 추가한다.

 

그 후 Adafruit_DHT에 있는 예제파일의 pin을 맞게 설정한 후 실행한다.

 

import Adafruit_DHT

# Sensor should be set to Adafruit_DHT.DHT11,
# Adafruit_DHT.DHT22, or Adafruit_DHT.AM2302.
sensor = Adafruit_DHT.DHT22

# Example using a Raspberry Pi with DHT sensor
# connected to GPIO23.
pin = 23

# Try to grab a sensor reading.  Use the read_retry method which will retry up
# to 15 times to get a sensor reading (waiting 2 seconds between each retry).
humidity, temperature = Adafruit_DHT.read_retry(sensor, pin)

# Note that sometimes you won't get a reading and
# the results will be null (because Linux can't
# guarantee the timing of calls to read the sensor).
# If this happens try again!
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!')

 

참고로 DHT21은  DHT22로 설정하여 실행하면 된다.

 

https://github.com/adafruit/Adafruit_Python_DHT/blob/master/examples/simpletest.py

 

GitHub - adafruit/Adafruit_Python_DHT: Python library to read the DHT series of humidity and temperature sensors on a Raspberry

Python library to read the DHT series of humidity and temperature sensors on a Raspberry Pi or Beaglebone Black. - GitHub - adafruit/Adafruit_Python_DHT: Python library to read the DHT series of hu...

github.com

 

반응형

댓글