树莓派4 DHT11 求教

lang 10cm2020-12-211321 次点击
使用CircuitPython库编译后 第一次实验成功了 可以显示温湿度,第二次实验时,发生错误,出现Unable to set line 18 to input
收藏 ♥ 感谢
Spoony 小组长 2020-12-22 
详细说一下使用的工具、源码、命令和操作步骤看看
lang 10cm 2020-12-22 
一开始我用的这个库来测DHT11温湿度
import Adafruit_DHT
import time

makerobo_pin = 17 # DHT11 温湿度传感器管脚定义

# GPIO口定义
def makerobo_setup():
global sensor
sensor = Adafruit_DHT.DHT11

# 循环函数
def loop():
humidity, temperature = Adafruit_DHT.read_retry(sensor, makerobo_pin)
while True:
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!')
time.sleep(1) # 延时1s

def destroy():
GPIO.cleanup() # 释放资源

# 程序入口
if __name__ == '__main__':
makerobo_setup()
try:
loop()
except KeyboardInterrupt: # 当按下Ctrl+C时,将执行destroy()子程序。
destroy()
但是试了几天 一直不行,出现
Traceback (most recent call last):
File "/home/pi/CLBDEMO/27_humiture/python/27_humiture.py", line 28, in <module>
loop()
File "/home/pi/CLBDEMO/27_humiture/python/27_humiture.py", line 13, in loop
humidity, temperature = Adafruit_DHT.read_retry(sensor, makerobo_pin)
File "/usr/local/lib/python3.7/dist-packages/Adafruit_DHT-1.4.0-py3.7-linux-armv7l.egg/Adafruit_DHT/common.py";, line 94, in read_retry
humidity, temperature = read(sensor, pin, platform)
File "/usr/local/lib/python3.7/dist-packages/Adafruit_DHT-1.4.0-py3.7-linux-armv7l.egg/Adafruit_DHT/common.py";, line 80, in read
platform = get_platform()
File "/usr/local/lib/python3.7/dist-packages/Adafruit_DHT-1.4.0-py3.7-linux-armv7l.egg/Adafruit_DHT/common.py";, line 60, in get_platform
from . import Beaglebone_Black
File "/usr/local/lib/python3.7/dist-packages/Adafruit_DHT-1.4.0-py3.7-linux-armv7l.egg/Adafruit_DHT/Beaglebone_Black.py";, line 24, in <module>
from . import Beaglebone_Black_Driver as driver
ImportError: cannot import name 'Beaglebone_Black_Driver' from 'Adafruit_DHT' (/usr/local/lib/python3.7/dist-packages/Adafruit_DHT-1.4.0-py3.7-linux-armv7l.egg/Adafruit_DHT/__init__.py)

最后在网上了解到CircuitPython库也可以测DHT11温湿度,但出现一点小问题,这个文件每次开机运行的第一次能运行成功,但是停止再运行时就会出现Unable to set line 18 to input 的错误;
参照https://learn.adafruit.com/dht-humidity-sensing-on-raspberry-pi-with-gdocs-logging/python-setup
代码:
import time
import board
import adafruit_dht

# Initial the dht device, with data pin connected to:
dhtDevice = adafruit_dht.DHT11(board.D18)

# you can pass DHT22 use_pulseio=False if you wouldn't like to use pulseio.
# This may be necessary on a Linux single board computer like the Raspberry Pi,
# but it will not work in CircuitPython.
# dhtDevice = adafruit_dht.DHT22(board.D18, use_pulseio=False)

while True:
try:
# Print the values to the serial port
temperature_c = dhtDevice.temperature
temperature_f = temperature_c * (9 / 5) + 32
humidity = dhtDevice.humidity
print(
"Temp: {‌:.1f} F / {‌:.1f} C Humidity: {‌}% ".format(
temperature_f, temperature_c, humidity
)
)

except RuntimeError as error:
# Errors happen fairly often, DHT's are hard to read, just keep going
print(error.args[0])
time.sleep(2.0)
continue
except Exception as error:
dhtDevice.exit()
raise error

time.sleep(2.0)
Spoony 小组长 2020-12-22 
前面的问题看错误信息和 Beaglebone_Black_Driver 有关
下面这个你试试换一个引脚,如果可以用BCM 编码就尽量用BCM编码,修改下这里
dhtDevice = adafruit_dht.DHT11(board.D18)


因为时序的问题,DHT 11 的读取有一定的失败几率的
lang 10cm 2020-12-22 
还有其他稳定的方法 用DHT11测 温湿度的吗??? 求教
Spoony 小组长 2020-12-22 
@lang 这个我测试下来可以
https://talk.quwj.com/topic/1005
lang 10cm 2020-12-23 
python版本的有吗??
Spoony 小组长 2020-12-23 
@lang 有但不准
树莓派实验室搜索 dht11

登录注册 后可回复。

这里由以下店铺的技术人员在此免费提供能力范围内的技术支持。

NXEZ 创客商店(淘宝)

排障问题发帖注意事项:
1、请尽可能将故障说明清晰,列明例如使用环境和做了哪些操作和配置等。
2、排障中已经做了哪些尝试,如果能在问题中进行描述将极大提高解决问题的效率。
3、对于需要自身完成知识积累才能解决的问题,请自行通过搜索引擎寻找资料学习。