树莓派 4b python3 温湿度传感器 dht不断获取温度湿度

sudo apt-get update
sudo apt-get install build-essential python-dev
sudo wget https://github.com/adafruit/Adafruit_Python_DHT.git

cd Adafruit_Python_DHT

sudo python3 setup.py install  python3安装Adafruit_Python_DHT库

cd examples

python3 AdafruitDHT.py 11 04  11代表传感器型号  04 是代表gpio 脚

在这里插入图片描述
这样显得不太方便 ,写一个python文件代码循环读取

#!/usr/bin/python
# encoding:utf-8
import time
import Adafruit_DHT


humidity, temperature = Adafruit_DHT.read_retry(11, 4)
while(1):
    if humidity is not None and temperature is not None:
        print('Temp={0:0.2f}度  Humidity={1:0.2f}%'.format(temperature, humidity))
        time.sleep(1)
    else:
        print('读写数据失败')

你可能感兴趣的:(树莓派传感器实验)