02_溫溼度傳感器

1. 溫溼度傳感器鏈接

溫溼度傳感器

2. 軟件安裝

sudo apt-get update
sudo apt-get install build-essential python-dev

3. 從GitHub獲取Adafruit庫

sudo git clone https://github.com/adafruit/Adafruit_Python_DHT.git
cd Adafruit_Python_DHT

4. 給Python2安裝該庫

sudo python setup.py install

5. 實例程序

cd ~
cd Adafruit_Python_DHT
cd examples
python AdafruitDHT.py 11 17#參數爲DHT11和數據引腳所接的樹莓派GPIO編號

6. python項目

cd
mkdir project
cd project
vim read_retry.py
#coding=utf-8
import Adafruit_DHT
import datetime
#打印當前時間
print datetime.datetime.now().strftime('%Y-%m-%d %H:%M:%S'),

#Set sensor type:Options are DHT11,DHT22 or AM2302
sensor = Adafruit_DHT.DHT11

#Set GPIO sensor is connected to
gpio=17

# Use read_retry method. This will retry up to 15 times to
# get a sensor reading (waiting 2 seconds between each retry).
humidity, temperature = Adafruit_DHT.read_retry(sensor, gpio)
  
# Reading the DHT11 is very sensitive to timings and occasionally
# the Pi might fail to get a valid reading. So check if readings are valid.
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!')

7.製做獲取溫溼度腳本

touch temp_humidity.txt
touch get_temp_humidity.sh
/usr/bin/python2.7 /home/pi/project/read_retry.py >> /home/pi/project/temp_humidity.txt

8. 定時啓動溫溼度腳本

sudo vim /etc/crontab
0 */2 * * * pi sh /home/pi/project/get_temp_humidity.sh
#參考資料:

1.自啓動和定時啓動:http://www.php.cn/python-tutorials-376149.htmlphp

2.溫溼度傳感器:http://shumeipai.nxez.com/2018/05/16/dht11-temperature-and-humidity-sensor-raspberry-pi.htmlhtml

相關文章
相關標籤/搜索