///
Search
💻

11주차 (보충) RaspberryPI GPIO와 DHT 22,DHT11예제 실습하기

라즈베리파이 GPIO핀 리스트(매우중요)

센서는 DHT22로 실습하였다.

라즈베리파이랑 센서는 다음과 같이 연결을 하였다.
케이블 색상 : 라즈베리파이 DHT22
형광 : 7 (GPIO4) 2
흰색 : 17 (3v) 1
검정 : 06 (GND) 4
연결 화면 캡처
라즈베리파이터미널 sudo apt-get install git 해준다음(혹시몰라서 git 설치)
그다음은 아래 레포지토리를 가져온다. (명령어: git clone https://github.com/adafruit/Adafruit_Python_DHT.git)
pi@raspberrypi:/ $ sudo apt-get install git Reading package lists... Done Building dependency tree Reading state information... Done git is already the newest version (1:2.20.1-2+deb10u3). 0 upgraded, 0 newly installed, 0 to remove and 0 not upgraded. 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 | 1.02 MiB/s, done. Resolving deltas: 100% (176/176), done. pi@raspberrypi:~ $
JavaScript
복사
지금 설치된 디렉토리가 pi폴더안인데 이 안에는 Desktop폴더랑 Download폴더랑 같은 위치 안에 있다.
같은 디렉토리에 있는지 꼭 확인해보고 만약에 틀리다면 cd명령어를 활용해서 pi폴더 안으로 들어가도록 한다.
그리고 Adafruit_Python_DHT 폴더 안에 들어가는 작업을 하고 그 다음은 update를 한번 해준다.
명령어 리스트
1.
cd Adafruit_Python_DHT
2.
sudo apt-get update
예시화면
pi@raspberrypi:~ $ cd Adafruit_Python_DHT/ pi@raspberrypi:~/Adafruit_Python_DHT $ sudo apt-get update Get:1 http://raspbian.raspberrypi.org/raspbian buster InRelease [15.0 kB] Get:2 http://archive.raspberrypi.org/debian buster InRelease [32.9 kB] Get:3 http://raspbian.raspberrypi.org/raspbian buster/main armhf Packages [13.0 MB] Get:4 http://archive.raspberrypi.org/debian buster/main armhf Packages [375 kB] Fetched 13.4 MB in 23s (585 kB/s) Reading package lists... Done pi@raspberrypi:~/Adafruit_Python_DHT $
JavaScript
복사
그 다음은 다음과 같이 명령어를 작성한다,.
1.
sudo apt-get install build-essential python-dev python-openssl
2.
sudo python setup.py install
예시화면
pi@raspberrypi:~/Adafruit_Python_DHT $ sudo apt-get install build-essential python-dev python-openssl Reading package lists... Done Building dependency tree Reading state information... Done build-essential is already the newest version (12.6). python-dev is already the newest version (2.7.16-1). python-dev set to manually installed. python-openssl is already the newest version (19.0.0-1). python-openssl set to manually installed. 0 upgraded, 0 newly installed, 0 to remove and 105 not upgraded. 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_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/platform_detect.py -> build/lib.linux-armv7l-2.7/Adafruit_DHT copying Adafruit_DHT/common.py -> 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 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-InigCj/python2.7-2.7.16=. -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-InigCj/python2.7-2.7.16=. -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-InigCj/python2.7-2.7.16=. -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-InigCj/python2.7-2.7.16=. -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-InigCj/python2.7-2.7.16=. -fstack-protector-strong -Wformat -Werror=format-security -Wl,-z,relro -Wdate-time -D_FORTIFY_SOURCE=2 -g -fdebug-prefix-map=/build/python2.7-InigCj/python2.7-2.7.16=. -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_2.py -> 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/Raspberry_Pi_2_Driver.so -> 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/common.py -> 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 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/platform_detect.py to platform_detect.pyc byte-compiling build/bdist.linux-armv7l/egg/Adafruit_DHT/common.py to common.pyc 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 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 $
JavaScript
복사
여기까지 오면 예시를 실행시키기 위해 다음 Adafruit_Python_DHT 폴더 안에 있는 examples 폴더 안으로 들어간다.
그리고 AdafruitDHT.py를 실행시켜서 정상적으로 돌아가는지 확인한다.
명령어
1.
cd examples
2.
sudo ./AdafruitDHT.py 2302 4
실행화면
pi@raspberrypi:~/Adafruit_Python_DHT $ cd examples pi@raspberrypi:~/Adafruit_Python_DHT/examples $ sudo ./AdafruitDHT.py 2302 4 Temp=27.3* Humidity=48.5% pi@raspberrypi:~/Adafruit_Python_DHT/examples $ sudo ./AdafruitDHT.py 2302 4 Temp=25.9* Humidity=43.7% pi@raspberrypi:~/Adafruit_Python_DHT/examples $ sudo ./AdafruitDHT.py 2302 4 Temp=25.9* Humidity=43.6% pi@raspberrypi:~/Adafruit_Python_DHT/examples $
JavaScript
복사

DHT11와 라즈베리파이랑 연결하기

회로 구성도
케이블 색상 : 라즈베리파이 DHT11
형광 : 7 (GPIO4) S
흰색 : 17 (3v) 'S랑 -랑 표시가 되어있는 중간'
검정 : 34 (GND) -
연결 예시
진짜 다 똑같고 마지막에 우리가 sudo ./AdafruitDHT.py 2302 4 를 입력해서 실행을 시켰는데, 이렇게 말고 다음처럼 실행을 시키면 된다.
sudo ./AdafruitDHT.py 11 4
pi@raspberrypi:~/Adafruit_Python_DHT/examples $ sudo ./AdafruitDHT.py 11 4 Temp=26.0* Humidity=43.0% pi@raspberrypi:~/Adafruit_Python_DHT/examples $
JavaScript
복사
이유는 AdafruitDHT.py 코드를 보면 이해가 되는데 다음처럼 적혀 있다.
#!/usr/bin/python # Copyright (c) 2014 Adafruit Industries # Author: Tony DiCola # Permission is hereby granted, free of charge, to any person obtaining a copy # of this software and associated documentation files (the "Software"), to deal # in the Software without restriction, including without limitation the rights # to use, copy, modify, merge, publish, distribute, sublicense, and/or sell # copies of the Software, and to permit persons to whom the Software is # furnished to do so, subject to the following conditions: # The above copyright notice and this permission notice shall be included in all # copies or substantial portions of the Software. # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR # IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, # FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE # AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER # LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE # SOFTWARE. import sys import Adafruit_DHT # Parse command line parameters. sensor_args = { '11': Adafruit_DHT.DHT11, #DHT11를 선택할 때 '22': Adafruit_DHT.DHT22, #DHT22를 선택할 때 '2302': Adafruit_DHT.AM2302 } if len(sys.argv) == 3 and sys.argv[1] in sensor_args: sensor = sensor_args[sys.argv[1]] pin = sys.argv[2] else: print('Usage: sudo ./Adafruit_DHT.py [11|22|2302] <GPIO pin number>') print('Example: sudo ./Adafruit_DHT.py 2302 4 - Read from an AM2302 connected to GPIO pin #4') sys.exit(1) # 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) # Un-comment the line below to convert the temperature to Fahrenheit. # temperature = temperature * 9/5.0 + 32 # 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}* Humidity={1:0.1f}%'.format(temperature, humidity)) else: print('Failed to get reading. Try again!') sys.exit(1)
Python
복사
중간에 한글로 주석을 달았는데 저 부분에서 Adafruit_DHT.DHT11라는 함수를 선택하는 부분이 있는데 이쪽에서 다른 py파일에서 계산식을 불러오는 작업을 수행하는 것 같다.
끝!
소스코드는 아래의 링크에 올려놓았습니다.