# 取得溫濕度

[DHT22(AOSONG AM2302)](https://www.adafruit.com/product/385) 溫濕度元件在GitHub中有提供Raspberry Pi的python的程式讀取溫度與濕度

## 更新系統套件

執行指令：

```
sudo apt-get update
sudo apt-get upgrade
```

## 接著要安裝所需要的相關套件

執行指令：

```
sudo apt-get install git build-essential python-dev
```

執行結果：

```
pi@raspberrypi:~ $ sudo apt-get install git build-essential python-dev
Reading package lists... Done
Building dependency tree
Reading state information... Done
build-essential is already the newest version (12.3).
The following additional packages will be installed:
  git-man liberror-perl libexpat1-dev libpython-dev libpython2.7 libpython2.7-dev python2.7-dev
Suggested packages:
  git-daemon-run | git-daemon-sysvinit git-doc git-el git-email git-gui gitk gitweb git-arch git-cvs git-mediawiki git-svn
The following NEW packages will be installed:
  git git-man liberror-perl libexpat1-dev libpython-dev libpython2.7 libpython2.7-dev python-dev python2.7-dev
0 upgraded, 9 newly installed, 0 to remove and 0 not upgraded.
Need to get 33.7 MB of archives.
After this operation, 67.0 MB of additional disk space will be used.
Do you want to continue? [Y/n] y
Get:1 http://mirror.ossplanet.net/raspbian/raspbian stretch/main armhf liberror-perl all 0.17024-1 [26.9 kB]
Get:2 http://mirror.ossplanet.net/raspbian/raspbian stretch/main armhf git-man all 1:2.11.0-3+deb9u2 [1,432 kB]
Get:9 http://raspbian.raspberrypi.org/raspbian stretch/main armhf python-dev armhf 2.7.13-2 [1,130 B]
Get:3 http://mirror.ossplanet.net/raspbian/raspbian stretch/main armhf git armhf 1:2.11.0-3+deb9u2 [3,380 kB]
Get:4 http://mirror.ossplanet.net/raspbian/raspbian stretch/main armhf libexpat1-dev armhf 2.2.0-2+deb9u1 [116 kB]
Get:5 http://mirror.ossplanet.net/raspbian/raspbian stretch/main armhf libpython2.7 armhf 2.7.13-2+deb9u2 [914 kB]
Get:6 http://mirror.ossplanet.net/raspbian/raspbian stretch/main armhf libpython2.7-dev armhf 2.7.13-2+deb9u2 [27.6 MB]
Get:7 http://mirror.ossplanet.net/raspbian/raspbian stretch/main armhf libpython-dev armhf 2.7.13-2 [20.0 kB]
Get:8 http://mirror.ossplanet.net/raspbian/raspbian stretch/main armhf python2.7-dev armhf 2.7.13-2+deb9u2 [286 kB]
Fetched 33.7 MB in 37s (911 kB/s)
Selecting previously unselected package liberror-perl.
(Reading database ... 34374 files and directories currently installed.)
Preparing to unpack .../0-liberror-perl_0.17024-1_all.deb ...
Unpacking liberror-perl (0.17024-1) ...
Selecting previously unselected package git-man.
Preparing to unpack .../1-git-man_1%3a2.11.0-3+deb9u2_all.deb ...
Unpacking git-man (1:2.11.0-3+deb9u2) ...
Selecting previously unselected package git.
Preparing to unpack .../2-git_1%3a2.11.0-3+deb9u2_armhf.deb ...
Unpacking git (1:2.11.0-3+deb9u2) ...
Selecting previously unselected package libexpat1-dev:armhf.
Preparing to unpack .../3-libexpat1-dev_2.2.0-2+deb9u1_armhf.deb ...
Unpacking libexpat1-dev:armhf (2.2.0-2+deb9u1) ...
Selecting previously unselected package libpython2.7:armhf.
Preparing to unpack .../4-libpython2.7_2.7.13-2+deb9u2_armhf.deb ...
Unpacking libpython2.7:armhf (2.7.13-2+deb9u2) ...
Selecting previously unselected package libpython2.7-dev:armhf.
Preparing to unpack .../5-libpython2.7-dev_2.7.13-2+deb9u2_armhf.deb ...
Unpacking libpython2.7-dev:armhf (2.7.13-2+deb9u2) ...
Selecting previously unselected package libpython-dev:armhf.
Preparing to unpack .../6-libpython-dev_2.7.13-2_armhf.deb ...
Unpacking libpython-dev:armhf (2.7.13-2) ...
Selecting previously unselected package python2.7-dev.
Preparing to unpack .../7-python2.7-dev_2.7.13-2+deb9u2_armhf.deb ...
Unpacking python2.7-dev (2.7.13-2+deb9u2) ...
Selecting previously unselected package python-dev.
Preparing to unpack .../8-python-dev_2.7.13-2_armhf.deb ...
Unpacking python-dev (2.7.13-2) ...
Setting up git-man (1:2.11.0-3+deb9u2) ...
Setting up liberror-perl (0.17024-1) ...
Processing triggers for libc-bin (2.24-11+deb9u3) ...
Setting up libpython2.7:armhf (2.7.13-2+deb9u2) ...
Setting up libexpat1-dev:armhf (2.2.0-2+deb9u1) ...
Processing triggers for man-db (2.7.6.1-2) ...
Setting up libpython2.7-dev:armhf (2.7.13-2+deb9u2) ...
Setting up python2.7-dev (2.7.13-2+deb9u2) ...
Setting up git (1:2.11.0-3+deb9u2) ...
Setting up libpython-dev:armhf (2.7.13-2) ...
Setting up python-dev (2.7.13-2) ...
Processing triggers for libc-bin (2.24-11+deb9u3) ...
pi@raspberrypi:~ $
```

## 下載程式碼

以上所需要的套件安裝完成後，透過git指令把放在github上的程式clone到Raspberry Pi中

執行指令：

```
cd ~
git clone https://github.com/adafruit/Adafruit_Python_DHT.git
```

執行結果：

```
pi@raspberrypi:~ $ git clone https://github.com/adafruit/Adafruit_Python_DHT.git
Cloning into 'Adafruit_Python_DHT'...
remote: Counting objects: 249, done.
remote: Total 249 (delta 0), reused 0 (delta 0), pack-reused 249
Receiving objects: 100% (249/249), 77.01 KiB | 0 bytes/s, done.
Resolving deltas: 100% (142/142), done.
```

## 安裝Adafruit\_Python\_DHT

執行指令：

```
cd Adafruit_Python_DHT
sudo python setup.py install
```

執行結果：

```
pi@raspberrypi:~/Adafruit_Python_DHT $ sudo python setup.py install
Extracting in /tmp/tmphYQXtG
Now working in /tmp/tmphYQXtG/setuptools-4.0.1
Building a Setuptools egg in /home/pi/Adafruit_Python_DHT
/home/pi/Adafruit_Python_DHT/setuptools-4.0.1-py2.7.egg
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'
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/Beaglebone_Black.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/Raspberry_Pi_2.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/Test.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/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-kKRR4y/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-kKRR4y/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-kKRR4y/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-kKRR4y/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-kKRR4y/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-kKRR4y/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/Beaglebone_Black.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/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/platform_detect.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/__init__.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/Beaglebone_Black.py to Beaglebone_Black.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/Raspberry_Pi_2.py to Raspberry_Pi_2.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/Test.py to Test.pyc
byte-compiling build/bdist.linux-armv7l/egg/Adafruit_DHT/__init__.py to __init__.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.3.2-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 setuptools-4.0.1-py2.7.egg
Copying setuptools-4.0.1-py2.7.egg to /usr/local/lib/python2.7/dist-packages
Adding setuptools 4.0.1 to easy-install.pth file
Installing easy_install script to /usr/local/bin
Installing easy_install-2.7 script to /usr/local/bin

Installed /usr/local/lib/python2.7/dist-packages/setuptools-4.0.1-py2.7.egg
Processing dependencies for setuptools==4.0.1
Finished processing dependencies for setuptools==4.0.1
Processing Adafruit_DHT-1.3.2-py2.7-linux-armv7l.egg
Copying Adafruit_DHT-1.3.2-py2.7-linux-armv7l.egg to /usr/local/lib/python2.7/dist-packages
Adding Adafruit-DHT 1.3.2 to easy-install.pth file

Installed /usr/local/lib/python2.7/dist-packages/Adafruit_DHT-1.3.2-py2.7-linux-armv7l.egg
Processing dependencies for Adafruit-DHT==1.3.2
Finished processing dependencies for Adafruit-DHT==1.3.2
```

## 取得溫濕度

執行指令：

```
cd examples
sudo ./AdafruitDHT.py 2302 4
```

執行結果：

```
pi@raspberrypi:~/Adafruit_Python_DHT $ cd examples/
pi@raspberrypi:~/Adafruit_Python_DHT/examples $ sudo ./AdafruitDHT.py 2302 4
Temp=26.6*  Humidity=85.2%
```

所顯示的Temp就是目前所讀取到的溫度，而Humidity就是濕度


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://gxiangco.gitbook.io/raspberry-pi-temperature/chapter2.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
