树莓派之温湿度模块dht11

2017-12-25 Frank 服务器

连线

连接正负极及gpio引脚

安装模块

sudo apt-get update
sudo apt-get install build-essential python-dev
git clone git@github.com:adafruit/Adafruit_Python_DHT.git
sudo python setup.py install

脚本

vim /home/pi/pydht11.py

import Adafruit_DHT as dht
import sys
pin = int(sys.argv[1])
sensor = dht.DHT11
humidity, temperature = dht.read_retry(sensor, pin)
if humidity is not None and temperature is not None:
    print '{0:d} {1:0.1f} c {2:0.1f} %'.format(0,temperature, humidity)
else:
    print '{0:d} {1:d} c {2:d} %'.format(3,0,0)

运行

$ python /home/pi/pydht11.py 26

成功打印0 20.0 c 46.0 %
运行正常,然后就可以通过homebridge显示在Home 应用上。

参考
Adafruit_Python_DHT

标签: 树莓派

发表评论 登录

Top