回复了 lang 创建的主题 树莓派 / 树莓派4 PCF8591 光敏电阻传感器 求教
2021-01-02

你好,还是不行,始终有问题,,,想请教一下你们这边的代码可以实现吗?

回复了 lang 创建的主题 树莓派 / 树莓派4 PCF8591 光敏电阻传感器 求教
2020-12-31

重启还是这样,,每次都是只能输出一会 然后就报错了

回复了 lang 创建的主题 树莓派 / 树莓派4 PCF8591 光敏电阻传感器 求教
2020-12-30

你好 我用了你推荐的传感器 也遇到一点小问题
它能够测出来,但是过了一会儿就报错了 这是什么问题呢?
光照強度: 61.67lx
光照強度: 61.67lx
光照強度: 61.67lx
光照強度: 61.67lx
光照強度: 61.67lx
光照強度: 61.67lx
Traceback (most recent call last):
File "/home/pi/CLBDEMO/code/illuminance.py", line 45, in <module>
print(getIlluminance())
File "/home/pi/CLBDEMO/code/illuminance.py", line 34, in getIlluminance
res=bus.read_word_data(__DEV_ADDR,0)
OSError: [Errno 121] Remote I/O error


源代码:
import smbus
import time

#BH1750地址
__DEV_ADDR=0x23

#控制字
__CMD_PWR_OFF=0x00 #關機
__CMD_PWR_ON=0x01 #開機
__CMD_RESET=0x07 #重置
__CMD_CHRES=0x10 #持續高分辨率檢測
__CMD_CHRES2=0x11 #持續高分辨率模式2檢測
__CMD_CLHRES=0x13 #持續低分辨率檢測
__CMD_THRES=0x20 #一次高分辨率
__CMD_THRES2=0x21 #一次高分辨率模式2
__CMD_TLRES=0x23 #一次分辨率
__CMD_SEN100H=0x42 #靈敏度100%,高位
__CMD_SEN100L=0X65 #靈敏度100%,低位
__CMD_SEN50H=0x44 #50%
__CMD_SEN50L=0x6A #50%
__CMD_SEN200H=0x41 #200%
__CMD_SEN200L=0x73 #200%

bus=smbus.SMBus(1)
bus.write_byte(__DEV_ADDR,__CMD_PWR_ON)
bus.write_byte(__DEV_ADDR,__CMD_RESET)
bus.write_byte(__DEV_ADDR,__CMD_SEN100H)
bus.write_byte(__DEV_ADDR,__CMD_SEN100L)
bus.write_byte(__DEV_ADDR,__CMD_PWR_OFF)
def getIlluminance():
bus.write_byte(__DEV_ADDR,__CMD_PWR_ON)
bus.write_byte(__DEV_ADDR,__CMD_THRES2)
time.sleep(0.2)
res=bus.read_word_data(__DEV_ADDR,0)
#read_word_data
res=((res>>8)&0xff)|(res<<8)&0xff00
res=round(res/(2*1.2),2)
result="光照強度: "+str(res)+"lx"
return result


if __name__ == '__main__':
while True:
try:
print(getIlluminance())

except KeyboardInterrupt:
pass

回复了 lang 创建的主题 技术支持 / 树莓派4 DHT11 求教
2020-12-23

python版本的有吗??

回复了 lang 创建的主题 技术支持 / 树莓派4 DHT11 求教
2020-12-22

还有其他稳定的方法 用DHT11测 温湿度的吗??? 求教

回复了 lang 创建的主题 技术支持 / 树莓派4 DHT11 求教
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)