[ Raspberrypi ] – 溫溼度感應器_DHT11_DHT22 - [ 727 ]

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%
//結果顯示

!議建法想的你下留是或分享加讚按我幫請,話的您到助幫有章文的我果如 如果我的文章有幫助到您的話,請幫我按讚加分享或是留下你的想法建議!

發佈留言

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