文章目录

  • 一、开篇简单介绍
  • 二、在树莓派4B上导入Adafruit_DHT的库
  • 三、使用Adafruit_DHT的库获取温湿度数据
  • 四、应用到项目中去
  • 五、写在最后


2021年10月28日更新了博客,补充了一些细节,欢迎评论区交流!🙌


一、开篇简单介绍

使用设备情况: Raspberry Pi 4 Model B 4g版

系统为:官方 Raspberry Pi OS 2021.01.11版

使用元器件:DHT22,3根杜邦线,vcc接3.3v & out 接gpio 4 (参考下图,忽略标出来的TX和RX,没找其他图,(~ ̄▽ ̄)~) & GND 接ground

将温湿度传感器的值传到云平台 温湿度传感器数据读取_树莓派

将温湿度传感器的值传到云平台 温湿度传感器数据读取_将温湿度传感器的值传到云平台_02

树莓派读取温湿度传感器,有很多大佬都写过,但是博文都有时效性,有很多都没法有很好的效果了,这里我和大家分享一下,我目前使用的比较好的方法。


将温湿度传感器的值传到云平台 温湿度传感器数据读取_python_03

DHT22也称AM2302,是一款含有已校准数字信号输出的温湿度复合传感器,湿度量程范围0~99.9%RH,精度±2%RH,而温度量程范围是-40℃80℃,精度±0.5℃。

对DHT22 工作原理感兴趣,可以去自行搜索一下。

我在这里只介绍在树莓派上如何使用 Python 调用 Adafruit_DHT 的库来简单方便使用温湿度传感器DHT22

二、在树莓派4B上导入Adafruit_DHT的库

从GitHub获取Adafruit库

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

我这里已经导入过了。

将温湿度传感器的值传到云平台 温湿度传感器数据读取_传感器_04


安装完成后pi文件夹下新增Adafruit_Python_DHT文件夹,进入该文件夹安装该库。

cd Adafruit_Python_DHT
sudo python3 setup.py install

将温湿度传感器的值传到云平台 温湿度传感器数据读取_raspberry pi_05


打开Adafruit_DHT目录下的platform_detect.py,进行如下修改

将温湿度传感器的值传到云平台 温湿度传感器数据读取_raspberry pi_06

三、使用Adafruit_DHT的库获取温湿度数据

安装完成后进入examples文件夹运行AdfruitDHT.py可以获得结果

将温湿度传感器的值传到云平台 温湿度传感器数据读取_传感器_07

python3 AdafruitDHT.py 22 4

将温湿度传感器的值传到云平台 温湿度传感器数据读取_传感器_08


四、应用到项目中去

但是!!!我们怎么用到项目中去呢?

对python有些简单了解的朋友可能已经发现了,下面这句命令不就是python中常用的将变量传递给脚本的方法吗!

python3 AdafruitDHT.py 22 4

这个AdafruitDHT.py中一定是使用了 from sys import argv, 导入了sys模块

我们打开AdafruitDHT.py看看

#!/usr/bin/python
# Copyright (c) 2014 Adafruit Industries
# Author: Tony DiCola

# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:

# The above copyright notice and this permission notice shall be included in all
# copies or substantial portions of the Software.

# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
# SOFTWARE.
import sys

import Adafruit_DHT


# Parse command line parameters.
sensor_args = { '11': Adafruit_DHT.DHT11,
                '22': Adafruit_DHT.DHT22,
                '2302': Adafruit_DHT.AM2302 }
if len(sys.argv) == 3 and sys.argv[1] in sensor_args:
    sensor = sensor_args[sys.argv[1]]
    pin = sys.argv[2]
else:
    print('Usage: sudo ./Adafruit_DHT.py [11|22|2302] <GPIO pin number>')
    print('Example: sudo ./Adafruit_DHT.py 2302 4 - Read from an AM2302 connected to GPIO pin #4')
    sys.exit(1)

# 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)

# Un-comment the line below to convert the temperature to Fahrenheit.
# temperature = temperature * 9/5.0 + 32

# 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}*  Humidity={1:0.1f}%'.format(temperature, humidity))
else:
    print('Failed to get reading. Try again!')
    sys.exit(1)

好,现在结构已经很简单了,我现在需要设计一个每隔5秒就print一次温湿度,

代码在下面提供给大家参考。

(脚本我是放在Adafruit_DHT文件夹里面执行的,否则会缺乏依赖)

import Adafruit_DHT
import time

sensor = Adafruit_DHT.DHT22
pin = 4

humidity, temperature = Adafruit_DHT.read_retry(sensor,pin)

while(1):
    if humidity is not None and temperature is not None:
        print('Temp={0:0.1f}*  Humidity={1:0.1f}%'.format(temperature, humidity))
    else:
        print('Failed to get reading. Try again!')
    time.sleep(5)

效果图如下:

每隔5分钟执行一次,把上面的代码放到你项目脚本的合适位置就可以了。

将温湿度传感器的值传到云平台 温湿度传感器数据读取_python_09