반응형
앞서 DHT22 온습도 센서 관련하여 파이선 프로그래밍과 거의 동일합니다. 라즈베피 파이 DHT22 온도 습도 센서로부터 데이터를 가져오는 포스팅을 참고하세요.
DHT11 연결도는 아레와 같습니다.
파이선 코드를 입력하기 전에 Raspberry Pi에 DHT 라이브러리를 다운로드하여 설치해야합니다. 터미널 창을 열고 다음 사항을 하나씩 입력합니다. 아래 명령어를 하나씩 실행하는 과정과 출력을 아래에 표시합니다. 라즈베리파이 환경과 설정에 따라 다르게 나올 수도 있지만 유념치 마시고 결과 위주로 확인하십시오. 다 설치가 되었으면 재부팅이 필요할 수 있습니다. 우선 아래 명령을 순차적으로 입력합니다. 입력하여 실행되는 출력문을 아래에 포함하였습니다.
- git clone https://github.com/adafruit/Adafruit_Python_DHT.git
- cd Adafruit_Python_DHT
- sudo apt-get update
- sudo apt-get install build-essential python-dev
- sudo python setup.py install
pi@raspberrypi:~ $ git clone https://github.com/adafruit/Adafruit_Python_DHT.git
Cloning into 'Adafruit_Python_DHT'...
remote: Enumerating objects: 325, done.
remote: Total 325 (delta 0), reused 0 (delta 0), pack-reused 325
Receiving objects: 100% (325/325), 98.35 KiB | 0 bytes/s, done.
Resolving deltas: 100% (176/176), done.
pi@raspberrypi:~ $
pi@raspberrypi:~ $ cd Adafruit_Python_DHT
pi@raspberrypi:~/Adafruit_Python_DHT $ sudo apt-get update
Get:1 http://archive.raspberrypi.org/debian stretch InRelease [25.4 kB]
Get:2 http://raspbian.raspberrypi.org/raspbian stretch InRelease [15.0 kB]
Get:3 http://raspbian.raspberrypi.org/raspbian stretch/main armhf Packages [11.7 MB]
Get:4 http://raspbian.raspberrypi.org/raspbian stretch/contrib armhf Packages [56.9 kB]
Get:5 http://raspbian.raspberrypi.org/raspbian stretch/non-free armhf Packages [98.9 kB]
Get:6 http://raspbian.raspberrypi.org/raspbian stretch/rpi armhf Packages [1,360 B]
Fetched 11.9 MB in 27s (426 kB/s)
Reading package lists... Done
pi@raspberrypi:~/Adafruit_Python_DHT $ sudo apt-get install build-essential python-dev
Reading package lists... Done
Building dependency tree
Reading state information... Done
build-essential is already the newest version (12.3).
python-dev is already the newest version (2.7.13-2).
python-dev set to manually installed.
The following packages were automatically installed and are no longer required:
lxkeymap python-cairo python-gobject python-gobject-2 python-gtk2 python-xklavier realpath
Use 'sudo apt autoremove' to remove them.
0 upgraded, 0 newly installed, 0 to remove and 9 not upgraded.
pi@raspberrypi:~/Adafruit_Python_DHT $
pi@raspberrypi:~/Adafruit_Python_DHT $ sudo python setup.py install
running install
running bdist_egg
running egg_info
creating Adafruit_DHT.egg-info
writing Adafruit_DHT.egg-info/PKG-INFO
writing top-level names to Adafruit_DHT.egg-info/top_level.txt
writing dependency_links to Adafruit_DHT.egg-info/dependency_links.txt
writing manifest file 'Adafruit_DHT.egg-info/SOURCES.txt'
reading manifest file 'Adafruit_DHT.egg-info/SOURCES.txt'
reading manifest template 'MANIFEST.in'
writing manifest file 'Adafruit_DHT.egg-info/SOURCES.txt'
installing library code to build/bdist.linux-armv7l/egg
running install_lib
running build_py
creating build
creating build/lib.linux-armv7l-2.7
creating build/lib.linux-armv7l-2.7/Adafruit_DHT
copying Adafruit_DHT/Raspberry_Pi.py -> build/lib.linux-armv7l-2.7/Adafruit_DHT
copying Adafruit_DHT/Beaglebone_Black.py -> build/lib.linux-armv7l-2.7/Adafruit_DHT
copying Adafruit_DHT/platform_detect.py -> build/lib.linux-armv7l-2.7/Adafruit_DHT
copying Adafruit_DHT/Raspberry_Pi_2.py -> build/lib.linux-armv7l-2.7/Adafruit_DHT
copying Adafruit_DHT/__init__.py -> build/lib.linux-armv7l-2.7/Adafruit_DHT
copying Adafruit_DHT/Test.py -> build/lib.linux-armv7l-2.7/Adafruit_DHT
copying Adafruit_DHT/common.py -> build/lib.linux-armv7l-2.7/Adafruit_DHT
running build_ext
building 'Adafruit_DHT.Raspberry_Pi_2_Driver' extension
creating build/temp.linux-armv7l-2.7
creating build/temp.linux-armv7l-2.7/source
creating build/temp.linux-armv7l-2.7/source/Raspberry_Pi_2
arm-linux-gnueabihf-gcc -pthread -DNDEBUG -g -fwrapv -O2 -Wall -Wstrict-prototypes -fno-strict-aliasing -Wdate-time -D_FORTIFY_SOURCE=2 -g -fdebug-prefix-map=/build/python2.7-ajc2xi/python2.7-2.7.13=. -fstack-protector-strong -Wformat -Werror=format-security -fPIC -I/usr/include/python2.7 -c source/_Raspberry_Pi_2_Driver.c -o build/temp.linux-armv7l-2.7/source/_Raspberry_Pi_2_Driver.o -std=gnu99
arm-linux-gnueabihf-gcc -pthread -DNDEBUG -g -fwrapv -O2 -Wall -Wstrict-prototypes -fno-strict-aliasing -Wdate-time -D_FORTIFY_SOURCE=2 -g -fdebug-prefix-map=/build/python2.7-ajc2xi/python2.7-2.7.13=. -fstack-protector-strong -Wformat -Werror=format-security -fPIC -I/usr/include/python2.7 -c source/common_dht_read.c -o build/temp.linux-armv7l-2.7/source/common_dht_read.o -std=gnu99
arm-linux-gnueabihf-gcc -pthread -DNDEBUG -g -fwrapv -O2 -Wall -Wstrict-prototypes -fno-strict-aliasing -Wdate-time -D_FORTIFY_SOURCE=2 -g -fdebug-prefix-map=/build/python2.7-ajc2xi/python2.7-2.7.13=. -fstack-protector-strong -Wformat -Werror=format-security -fPIC -I/usr/include/python2.7 -c source/Raspberry_Pi_2/pi_2_dht_read.c -o build/temp.linux-armv7l-2.7/source/Raspberry_Pi_2/pi_2_dht_read.o -std=gnu99
arm-linux-gnueabihf-gcc -pthread -DNDEBUG -g -fwrapv -O2 -Wall -Wstrict-prototypes -fno-strict-aliasing -Wdate-time -D_FORTIFY_SOURCE=2 -g -fdebug-prefix-map=/build/python2.7-ajc2xi/python2.7-2.7.13=. -fstack-protector-strong -Wformat -Werror=format-security -fPIC -I/usr/include/python2.7 -c source/Raspberry_Pi_2/pi_2_mmio.c -o build/temp.linux-armv7l-2.7/source/Raspberry_Pi_2/pi_2_mmio.o -std=gnu99
arm-linux-gnueabihf-gcc -pthread -shared -Wl,-O1 -Wl,-Bsymbolic-functions -Wl,-z,relro -fno-strict-aliasing -DNDEBUG -g -fwrapv -O2 -Wall -Wstrict-prototypes -Wdate-time -D_FORTIFY_SOURCE=2 -g -fdebug-prefix-map=/build/python2.7-ajc2xi/python2.7-2.7.13=. -fstack-protector-strong -Wformat -Werror=format-security -Wl,-z,relro -Wdate-time -D_FORTIFY_SOURCE=2 -g -fdebug-prefix-map=/build/python2.7-ajc2xi/python2.7-2.7.13=. -fstack-protector-strong -Wformat -Werror=format-security build/temp.linux-armv7l-2.7/source/_Raspberry_Pi_2_Driver.o build/temp.linux-armv7l-2.7/source/common_dht_read.o build/temp.linux-armv7l-2.7/source/Raspberry_Pi_2/pi_2_dht_read.o build/temp.linux-armv7l-2.7/source/Raspberry_Pi_2/pi_2_mmio.o -lrt -o build/lib.linux-armv7l-2.7/Adafruit_DHT/Raspberry_Pi_2_Driver.so
creating build/bdist.linux-armv7l
creating build/bdist.linux-armv7l/egg
creating build/bdist.linux-armv7l/egg/Adafruit_DHT
copying build/lib.linux-armv7l-2.7/Adafruit_DHT/Raspberry_Pi.py -> build/bdist.linux-armv7l/egg/Adafruit_DHT
copying build/lib.linux-armv7l-2.7/Adafruit_DHT/Beaglebone_Black.py -> build/bdist.linux-armv7l/egg/Adafruit_DHT
copying build/lib.linux-armv7l-2.7/Adafruit_DHT/platform_detect.py -> build/bdist.linux-armv7l/egg/Adafruit_DHT
copying build/lib.linux-armv7l-2.7/Adafruit_DHT/Raspberry_Pi_2.py -> build/bdist.linux-armv7l/egg/Adafruit_DHT
copying build/lib.linux-armv7l-2.7/Adafruit_DHT/Raspberry_Pi_2_Driver.so -> build/bdist.linux-armv7l/egg/Adafruit_DHT
copying build/lib.linux-armv7l-2.7/Adafruit_DHT/__init__.py -> build/bdist.linux-armv7l/egg/Adafruit_DHT
copying build/lib.linux-armv7l-2.7/Adafruit_DHT/Test.py -> build/bdist.linux-armv7l/egg/Adafruit_DHT
copying build/lib.linux-armv7l-2.7/Adafruit_DHT/common.py -> build/bdist.linux-armv7l/egg/Adafruit_DHT
byte-compiling build/bdist.linux-armv7l/egg/Adafruit_DHT/Raspberry_Pi.py to Raspberry_Pi.pyc
byte-compiling build/bdist.linux-armv7l/egg/Adafruit_DHT/Beaglebone_Black.py to Beaglebone_Black.pyc
byte-compiling build/bdist.linux-armv7l/egg/Adafruit_DHT/platform_detect.py to platform_detect.pyc
byte-compiling build/bdist.linux-armv7l/egg/Adafruit_DHT/Raspberry_Pi_2.py to Raspberry_Pi_2.pyc
byte-compiling build/bdist.linux-armv7l/egg/Adafruit_DHT/__init__.py to __init__.pyc
byte-compiling build/bdist.linux-armv7l/egg/Adafruit_DHT/Test.py to Test.pyc
byte-compiling build/bdist.linux-armv7l/egg/Adafruit_DHT/common.py to common.pyc
creating stub loader for Adafruit_DHT/Raspberry_Pi_2_Driver.so
byte-compiling build/bdist.linux-armv7l/egg/Adafruit_DHT/Raspberry_Pi_2_Driver.py to Raspberry_Pi_2_Driver.pyc
creating build/bdist.linux-armv7l/egg/EGG-INFO
copying Adafruit_DHT.egg-info/PKG-INFO -> build/bdist.linux-armv7l/egg/EGG-INFO
copying Adafruit_DHT.egg-info/SOURCES.txt -> build/bdist.linux-armv7l/egg/EGG-INFO
copying Adafruit_DHT.egg-info/dependency_links.txt -> build/bdist.linux-armv7l/egg/EGG-INFO
copying Adafruit_DHT.egg-info/top_level.txt -> build/bdist.linux-armv7l/egg/EGG-INFO
writing build/bdist.linux-armv7l/egg/EGG-INFO/native_libs.txt
zip_safe flag not set; analyzing archive contents...
creating dist
creating 'dist/Adafruit_DHT-1.4.0-py2.7-linux-armv7l.egg' and adding 'build/bdist.linux-armv7l/egg' to it
removing 'build/bdist.linux-armv7l/egg' (and everything under it)
Processing Adafruit_DHT-1.4.0-py2.7-linux-armv7l.egg
Copying Adafruit_DHT-1.4.0-py2.7-linux-armv7l.egg to /usr/local/lib/python2.7/dist-packages
Adding Adafruit-DHT 1.4.0 to easy-install.pth file
Installed /usr/local/lib/python2.7/dist-packages/Adafruit_DHT-1.4.0-py2.7-linux-armv7l.egg
Processing dependencies for Adafruit-DHT==1.4.0
Finished processing dependencies for Adafruit-DHT==1.4.0
pi@raspberrypi:~/Adafruit_Python_DHT $
아래 두 가지 동착코드는 동일한 결과를 보여줍니다. 참고하십시요. 프로그램을 중단할 때는 CTRL-C 키를 누르시면 됩니다.
import time
import Adafruit_DHT
sensor = Adafruit_DHT.DHT11
pin = 4
while True :
h, t = Adafruit_DHT.read_retry(sensor, pin)
if h is not None and t is not None :
print("Temperature = {0:0.1f}*C Humidity = {1:0.1f}%".format(t, h))
else :
print('Read error')
time.sleep(2)
import time
import Adafruit_DHT
sensor = Adafruit_DHT.DHT11
pin = 4
try:
while True :
h, t = Adafruit_DHT.read_retry(sensor, pin)
if h is not None and t is not None :
print("Temperature = {0:0.1f}*C Humidity = {1:0.1f}%".format(t, h))
else :
print('Read error')
time.sleep(1)
except KeyboardInterrupt:
print("Terminated by Keyboard")
finally:
print("End of Program")
결과는 아래와 같습니다.
pi@raspberrypi:~ $ python dht11.py
Temperature = 24.0*C Humidity = 46.0%
Temperature = 24.0*C Humidity = 43.0%
Temperature = 24.0*C Humidity = 49.0%
Temperature = 24.0*C Humidity = 49.0%
Temperature = 24.0*C Humidity = 49.0%
Temperature = 24.0*C Humidity = 49.0%
반응형
'개발자 > 라즈베리파이4' 카테고리의 다른 글
Raspberry Pi와 인터페이싱하는 GPS 모듈 (4) | 2020.11.24 |
---|---|
PCB 제작할 때 정해야 할 부분 RaspberryPi IO Interface Module (0) | 2020.11.20 |
라즈베리파이 OS 정보와 CPU 정보 확인 (0) | 2020.11.18 |
라즈베리파이 i2c 사용하기 ssd1306 OLED와 함께 (0) | 2020.11.18 |
RaspberryPi AM2301 온도 습도 센서 python 코드 using the DHT22 (4) | 2020.11.06 |
Raspberry Pi로 Ubidots에 데이터 보내고 받기 2 (0) | 2020.11.04 |
라즈베리파이4 기반 AI IoT 가이드 7강 Maria DB 기본 (0) | 2020.07.31 |
라즈베리파이4 기반 AI IoT 가이드 1강 시작하기 (0) | 2020.07.30 |
더욱 좋은 정보를 제공하겠습니다.~ ^^