1. Home
  2. /
  3. IOT 物聯網
  4. /
  5. Raspberrypi
  6. /
  7. [ Raspberrypi ] –...

[ Raspberrypi ] – 溫溼度感應器_DHT11_DHT22

評等結果
點擊便能為這篇文章進行評等!
[評等總次數: 0,平均評等: 0]

sudo apt-get update
//更新安裝套件清單
sudo apt-get install build-essential python-dev
//安裝 編譯器
git clone https://github.com/adafruit/Adafruit_Python_DHT.git
//從git複製資料
cd Adafruit_Python_DHT
將目前位置移到 Adafruit_Python_DHT 資料夾
python setup.py install
//執行安裝
cd examples
//移到範例檔
nano simpletest.py
//編輯範例檔
[javascript]
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 Beaglebone Black with DHT sensor
# connected to pin P8_11.
pin = ‘P8_11’
#選擇接收資料的腳位
# 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!’)
#無法取得感應器資料 有可能是腳位插錯 或是供電不足
[/javascript]
樹莓派3 腳位參考圖 https://www.element14.com/community/servlet/JiveServlet/previewBody/73950-102-10-339300/pi3_gpio.png
python simpletest.py
//執行範例檔
Temp=25.0*C Humidity=21.0%
//結果顯示

評等結果
點擊便能為這篇文章進行評等!
[評等總次數: 0,平均評等: 0]

發佈留言

發佈留言必須填寫的電子郵件地址不會公開。 必填欄位標示為 *