这篇文章来源于deviceplus.com英语网站的翻译稿。
目录
1. 简介
1.1 认识dht22:一种温湿度传感器
1.2 您将要学到的内容
2. 将您的raspberry pi与dht22传感器连接
2.1 dht22:基本电路
2.1.1 bom
2.1.2 启动!
2.2 dht22机房环境控制器
2.2.1 bom
2.2.2 启动!
exentia – stock.adobe.com
1. 简介
能够在任意环境中轻松感知环境湿度的能力非常重要,因为湿度不仅会影响人的舒适度,还会影响人类机体的运作。我们在本文中将要使用的双模传感器非常适用于创客项目,例如改善hvac、气象站和物联网恒温器,尤其是与仅在raspbian上可用的大量高质量软件包相结合之后。您可以在pi(或linux终端)上试试这个:
wget http://archive.raspbian.org/raspbian/dists/stable/main/binary-armhf/packages.xz -o - 2>/dev/null | xz -dfc | grep -c '^package: ' | cut -d ' ' -f 2
等待其运行完成。它将输出您可以使用的软件包数量。在撰写本文时,该存储库中有61.487个包。这个数量每天都在增加。
无论您需要什么功能,应该都可以通过这些软件包来实现。您只需要提供感知的环境数据,本文第二部分中的示例将会告诉您如何获取这些数据。
相对湿度(rh)的概念在维基百科中有很详尽的解释。请记住,相对湿度和实际湿度是两种不同的东西,因为相对湿度取决于温度。
我们普遍感兴趣的是湿度对于人类和电子设备的阈值。如果您在25c/77f左右的环境温度下运动,那么会在35-40% rh开始感到不适。您的电子设备,尤其是计算机系统,在45-55% rh,温度在20c/68f至 24c/75f之间的环境中更灵敏,且性能最佳。如果rh太低,esd就会成为问题。如果rh太高,有局部冷凝的风险。
在冷却理论中,有一个简单的事实经常被忽视,即水分子是完美的小热量桶,风冷系统在45-55% rh环境中的冷却效果会比在20% rh环境中好得多。湿度对此有所帮助。很多人没有发现这一点,而您现在已经获悉这个小技巧了。
了解了这些内容,现在我们继续讨论硬件部分。
1.1 认识dht22:一种温湿度传感器
该使用什么硬件?当然,sht85湿度传感器性能很好,防水等级为ip67,误差范围仅为±1.5% rh,但我想我们可以选择湿度传感器误差范围为±2-5% rh的设备。
dht22有一个板载模数转换器,因此湿度传感器和热敏电阻的信号清晰明了。如果没有该功能,校验和的读取会让您十分痛苦。温度感应只有±0.5c的误差,对创客来说性能已经算非常好了。
五年前我买了6个,每个都性能出色,即使是在户外(虽然屏蔽了来自阳光、雨水等的紫外线)。dht22是单总线,既不是i2c也不是spi,虽然电压范围为3.3至6伏,但是绝不能用大于3.3v的电压供电,因为这会导致其发热,并感应出错误的温度数据。过高的局部温度会蒸发水分,因此局部湿度也会下降。这一点是完全可以避免的:直接从raspberry pi的3v3引脚获取3.3v电压,该引脚的数字为1。
在没有运行时的电流消耗非常低(约40ua),因此不需要任何节能功能,但如果您想要设置该功能,比如在mcu睡眠状态期间将dht22关闭,那么在再次想要读取数据时,需要大概1秒钟的唤醒时间。当开始对周围环境进行测量时,dht22消耗1.5-2.5ma的电流,因此电流量不会达到使用3v3引脚时建议的最大电流量50ma。
最小轮询间隔被定义为两秒,不过实际并不是如此。我可以以大约350毫秒的间隔进行轮询,大约10秒后会出现升温问题—但是如果您出于某种原因需要快速读取10次数据(取中位数?),那么在3.5秒内就可以完成,非常好。
引脚从左到右分别是1:vcc(3v3),2:signal,3:nc(未连接),4:gnd。您还需要一个从引脚2到引脚1的10kω上拉电阻。
当且仅当您遇到抖动(通常发生在长线缆电路上)状况时,需要在vcc和gnd之间添加一个100nf电容。
1.2 您将要学到的内容
学习完本文内容后,您将能够轻松感知相对温度和湿度。我添加了一些辅助函数,使您无需谷歌工具就可以将完成“摄氏度”和“华氏度”之间的转换。每次都手动转换会比较麻烦。为了您的方便,我们添加两个小的辅助函数:
# celsius to fahrenheit: f = ( c * 1.8 ) + 32def celsius2fahrenheit( _celsius ): _fahrenheit = ( %.2f % (( _celsius * 1.8 ) + 32) ) return float( _fahrenheit )# fahrenheit to celsius: c = ( f - 32 ) * 5/9def fahrenheit2celsius( _fahrenheit ): _celsius = ( %.2f % (( _fahrenheit - 32 ) * 5/9 )) return float( _celsius )
在第二部分的dht22_simple.py和dht22_actionable.py中也有这两个函数。
在我们研究dht22的过程中,将会深入探索“dht22机房环境控制器”或dcrec的构建和操作,该器件能够对传感器数据进行响应。您可以将dht22_actionable.py作为湿度项目的模板(基于dht22),如果您具备了硬件和安装技能,就可以完成几乎所有的工作了。阅读完python脚本后,您也将会掌握gpio输出控制。
2. 将您的raspberry pi与dht22传感器连接
dht22非常易于使用。此处无需处理i2c总线,它是i/o的一个数据引脚,是一种半双工总线,也许您对这个术语更熟悉。
您可以从技术规格书 获取有关脉冲长度、间隔等信息,但如果您想马上开始,请首先按照此raspberry pi设置指南确保您的pi已准备就绪。
准备好后,将下面的脚本复制粘贴到您的raspberry pi gui编辑器中(听说pluma很好用),并将文件保存为“rpi_prepare.sh”。
[ begin rpi_prepare.sh ]
#! /usr/bin/env bashset -eu -o pipefailexport debian_frontend debian_priority debconf_nowarnings_pkg_list=pigpio wiringpi python-rpi.gpio python3-rpi.gpio rpi.gpio-common git python-gpiozero python-gpiozero-doc python3-gpiozero python-setuptools-git python3-setuptools-git python3-dev python3-pip # upgrade system and installed packages - uncomment where # relevantsudo apt update || ech o failed to update index list #sudo dpkg --configure -a || ech o failed to fix interrupted upgrades#sudo apt --fix-broken --fix-missing install || ech o failed to fix conflicts #sudo apt -y --allow-downgrades --fix-broken --fix-missing #dist-upgrade# install $_pkg_listsudo apt updatesudo apt-get -y install $_pkg_list# make 'pip3' bigly fresh? yes.sudo python3 -m pip --upgrade pip setuptools wheel# get adafruit_dht python librarysudo pip3 install adafruit_dhtread -p [?] reboot? y/n: _fooif [ x$_foo = xy -o x$_foo = xy ]then ech o [!] rebooting in 5 seconds, ctrl+c to abort ... for i in $( seq 1 5 ) ; do ech o -n . ; sleep 1 ; done ; ech o sudo rebootfi
[ end rpi_prepare.sh ]
该脚本用于安装和更新python3的“pip3”程序,以及安装一些有用的gpio软件。将其复制到pi后,使用以下命令运行:
bash rpi_prepare.sh
2.1.1 bom
raspberry pi 4 https://www.newark.com/raspberry-pi/rpi4-modbp-4gb/raspberry-pi-4-model-b-4gb-rohs/dp/02ah3164
dht22 传感器 https://www.newark.com/mcm/83-17985/dht22-temp-humidity-sensor/dp/32ac9951
10kω 电阻 https://www.newark.com/multicomp-pro/mccfr0w4j0103a50/carbon-film-resistor-10kohm-250mw/dp/58k5002
杜邦电线 https://www.newark.com/adafruit/824/wire-gauge-28awg/dp/88w2794
面包板 https://www.newark.com/mcm/21-19082/breadboardjumper-kit-with-binding/dp/79x3995
2.1.2 启动!
将它们全部连接起来,这一步很简单,请按照以下图示进行连接。
首先,将dht22放在面包板上,并在引脚1和引脚2之间添加10kω上拉电阻。然后将board1/3v3连接到面包板上的红色导轨,board6/gnd连接到黑色导轨,将bcm24/board18连接到dht22上的引脚2。现在,用一根导线从红色导轨连接到dht22的引脚1,用另一根导线从黑色导轨连接到引脚4。然后将以下脚本复制粘贴到您的pi终端:
#! /usr/bin/env python3# demonstrate reading dht22 sensor using the# adafruit_dht library# dht22 pinout (left to right): # 1: vcc(3.3-6v) # 2: signal # 3: unused # 4: gnd# notes:# - 10kohm pull-up resistor from sig to vcc.# - use 3v3 for vcc, or the dht22 will heat up.# - indentation: 2 whitespaces per level, no tabs.import adafruit_dhtfrom time import sleepp=print # alias # note: use >=2000ms polling intervals_poll_interval = 2 # seconds_dht_pin = 24 # bcm24/board18_dht = adafruit_dht.dht22def celsius2fahrenheit( _celsius ): _fahrenheit = ( %.1f % (( _celsius * 1.8 ) + 32) ) return float( _fahrenheit )def fahrenheit2celsius( _fahrenheit ): _celsius = ( %.1f % (( _fahrenheit - 32 ) * 5/9 )) return float( _celsius )if __name__ == '__main__': while true: ( _humidity, _celsius ) = adafruit_dht.read_retry( _dht, _dht_pin ) p( humidity => %.1f%% rh % _humidity ) p( temperature => %.2ff % celsius2fahrenheit( _celsius ), end='/' ) p( %.2fc % _celsius ) sleep( _poll_interval )
[ end dht22_simple.py ]
输出应如下所示。如果没有输出任何内容,您可能遇到了运行超时的问题。这种情况有时候会发生,但是是不应该会发生的。按下ctrl+c中断程序,并检查所有连接是否正确。
在截图的执行过程中,我将热风枪的喷嘴轻轻靠近dht22,同时用红外温度计测量dht22。这两个读数可以彼此验证,以保证实验的严谨性。
2.2 dht22 机房控制器
现在,我们将做一些比仅仅通过湿度传感器和热敏电阻感知环境更有趣的事情—制作dht22机房环境控制器。在阅读下面的dht22_actionable.py之前,需要对其操作进行简短的解释。我们设置两个raspberry pi gpio引脚作为输出(bcm25/board22和bcm23/board16),建立与dht22传感器的连接,然后我们循环几个条件语句来检查所处环境是否满足公认的“适于计算的最佳湿度和温度条件”。
每当湿度或温度超出范围时,_humidity_led或_temperature_led都会亮起。但它也可以用于驱动电路,例如irlz24n mosfet或继电器驱动电路。您可以利用自己现有的设备来实现所有这些功能。
准备好所有的元器件,如果有缺少的东西,请访问下文bom中的连接。
2.2.1 bom
raspberry pi 4 https://www.newark.com/raspberry-pi/rpi4-modbp-4gb/raspberry-pi-4-model-b-4gb-rohs/dp/02ah3164
dht22 传感器 https://www.newark.com/mcm/83-17985/dht22-temp-humidity-sensor/dp/32ac9951
2x rohm slr343bc4tt32 3mm led https://www.infinite-electronic.hk/product/lapis-semiconductor_slr343bc4tt32.aspx
杜邦电线 https://www.newark.com/adafruit/824/wire-gauge-28awg/dp/88w2794
面包板 https://www.newark.com/mcm/21-19082/breadboardjumper-kit-with-binding/dp/79x3995
10kω 电阻 https://www.newark.com/multicomp-pro/mccfr0w4j0103a50/carbon-film-resistor-10kohm-250mw/dp/58k5002
2x 330ω 电阻 https://www.newark.com/multicomp-pro/mccfr0w4j0331a50/carbon-film-resistor-330-ohm-250mw/dp/58k5042
2.2.2 启动!
如下图所示完成所有连接。保留上次raspberry pi gpio bcm25/board22和bcm23/board16的引脚连接,3v3/board1和gnd/board6的连接和上次相同。在您的pi准备好运行程序之前不要给电路通电,在此之前仔细检查所有连接。确认没有任何问题后,通电并执行下文中的dht22_actionable.py。
python3 dht22_actionable.py
[ begin dht22_actionable.py ]
#! /usr/bin/env python3# install prerequisite packages & adafruit_dht library#! /bin/sh_pkg_list='python3-setuptools python3-pip python3-dev python3-rpi.gpio'sudo apt-get -y install $_pkg_listsudo python3 -m pip --upgrade pip setuptools wheelsudo pip3 install adafruit_dht## ensure operating conditions in a # datacenter (your basement) are # within safe operating humidity- and # temperature thresholds for optimal # computing conditions.## will fire bcm25/board22 if humidity is # out of bounds, and bcm23/board16 if # temperature is out of bounds. # # uses rpi.gpio adafruit_dht library, # tested on raspbian, dec. 2019## dht22 pinout (left to right):# 1: vcc(3.3-6v)# 2: signal# 3: unused# 4: gnd# notes:# - 10kohm pull-up resistor from sig to vcc.# - 330ohm resistors in series with rpi # gpio pins and rohm slr343bc4tt32# 3mm leds.# - use 3v3 for vcc, or the dht22 will # heat up.# - indentation: 2 whitespaces per level, no # tabs.import adafruit_dhtfrom time import sleepimport rpi.gpio as gpioimport atexitgpio.setwarnings( true )# use bcm pin numbering instead of boardgpio.setmode( gpio.bcm ) _humidity_led = 25 # bcm25/board22_humidity_lower_threshold = 45.0_humidity_upper_threshold = 55.0_temperature_led = 23 # bcm23/board16_temperature_lower_threshold = 20.0 # 20c/68f_temperature_upper_threshold = 24.0 # 24c/75f_poll_interval = 2 # use intervals >=2secs_dht_pin = 24 # bcm24/board18_dht = adafruit_dht.dht22_debug = truep=print # aliasif _debug: p( [!] setting up pin bcm_%i as output for humidity led ... % _humidity_led ) p( [!] setting up pin bcm_%i as output for temperature led ... % _temperature_led )gpio.setup( _humidity_led, gpio.out )gpio.setup( _temperature_led, gpio.out )def exit_cleanly(): print( [!] cleaning up and exiting ... ) gpio.setwarnings( false ) gpio.cleanup() exit()def celsius2fahrenheit( _celsius ): _fahrenheit = ( %.1f % (( _celsius * 1.8 ) + 32) ) return float( _fahrenheit )def fahrenheit2celsius( _fahrenheit ): _celsius = ( %.1f % (( _fahrenheit - 32 ) * 5/9 )) return float( _celsius )# call exit_cleanly on normal exit and ctrl+c/keyboardinterrupt/fooatexit.register( exit_cleanly )if __name__ == '__main__': while true: ( _humidity, _celsius ) = adafruit_dht.read_retry( _dht, _dht_pin ) if _debug: p( [+] humidity => %.1f%% rh % _humidity ) p( [+] temperature => %.1fc % _celsius, end='/' ) p( %.1ff % celsius2fahrenheit( _celsius ) ) # let's be neat _humidity = float( %.1f % _humidity ) _celsius = float( %.1f % _celsius ) # humidity too high? if _humidity > _humidity_upper_threshold: p( [!] humidity %.1f%% rh exceeds upper threshold value of %.1f%% rh % ( _humidity, _humidity_upper_threshold ) ) # take decisive action! gpio.output( _humidity_led, 1 ) # humidity too low? elif _humidity _temperature_upper_threshold: p( [!] temperature %.1fc/%.1ff exceeds upper threshold value of %.1fc/%.1ff % ( _celsius, celsius2fahrenheit( _celsius ), _temperature_upper_threshold, celsius2fahrenheit( _temperature_upper_threshold ) ) ) # take decisive action! gpio.output( _temperature_led, 1 ) # temperature too low? elif _celsius < _temperature_lower_threshold: p( [!] temperature %1.fc/%.1ff is below lower threshold value of %.1fc/%.1ff % ( _celsius, celsius2fahrenheit( _celsius ), _temperature_lower_threshold, celsius2fahrenheit( _temperature_lower_threshold ) ) ) # take decisive action! gpio.output( _temperature_led, 1 ) # safe operating temperature? elif _celsius = _temperature_lower_threshold: gpio.output( _temperature_led, 0 ) # safe! sl eep( _poll_interval )
[ end dht22_actionable.py ]
您的输出不应和下图中我的终端输出内容相同。将450c/842f的热风枪吹过一个装满水的小金属碗,以验证dht22机房环境控制器(dcrec)已经功能正常。在您的应用中,请确保gpio引脚连接到了一些完好的应用模块,例如mosfet驱动继电器或双扩音器。现在开始行动吧!
您可以在下图中看到我的面包板。在右下角,dht22正在运行,它会以数字的形式向我的raspberry pi报告。在顶部中间,两个led亮起,警告我的桌面计算环境不在最佳操作范围内。
我对此感到非常意外和震惊。
lasse efrayim jespersen
lasse出生于以色列,后移居到纬度较冷的地区。出于个人天然的兴趣爱好,他一步步走近了直接的perl、高效的c/c++、以及优雅的micropython。他非常喜欢用esp8266/esp32/raspberry pi和arduino来构建机器。
减速电机使用前需要检验哪些性能?顺力电机
一文详解电源的分类
苹果2020年三季度的财报逊色 苹果手机的竞争力正在下降?
锤子360还有OPPO都相继公布了自家新品发布会的时间
1499元价位,小米5C和中兴 Blade V8详细对比
极客造物:简易Raspberry Pi温湿度传感器
浅谈焊锡膏焊接后出现锡须是什么原因?
示波器使用中的常见问题
二氧化碳传感器在温室农业和垂直农业的异同和作用
AUTOSAR架构MCAL、服务层、ECU抽象层介绍
农村配电网的防雷保护和过电压保护
东芝数码复印机复印过程概述
三分钟看懂雪崩光电二极管
三星正式推出了Serif TV支持识别18种方言
HDI板盲孔互联失效原因
坚果r1评测 号称史上最好的锤子手机
机器人操作系统ROS详细介绍
MR-16 LED驱动器和用于脉冲LED冷却器的5V辅助电源
巧制无感螺丝刀
5G加速融入实体经济