Adafruit_DHT不适用于python 3 Raspberry Pi 3 B.

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Adafruit_DHT不适用于python 3 Raspberry Pi 3 B.相关的知识,希望对你有一定的参考价值。

这就是我做的:

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

cd Adafruit_Python_DHT

sudo apt-get install build-essential python-dev

sudo python setup.py install

这个^是在github链接本身中给出的。我这样做了,我的代码与python 2.x中的DHT11传感器完美配合但是它与python 3失败了。我得到的错误是:

RuntimeError: Error accessing GPTO. Make sure program is run as root with sudo!

我的代码是:

import Adafruit_DHT
import time

while True:
    time.sleep(1)
    humidity, temperature = Adafruit_DHT.read_retry(Adafruit_DHT.DHT11,4)
    print(temperature)
    print(humidity)

与python 2完美配合,问题在于python 3.我使用Raspberry Pi3 B作为GPIO接口。

编辑:我尝试了sudo python temper.py,它再次工作,但sudo python3 temper.py仍然不起作用,一个小的改变,它不会给出任何错误,但现在输出是

None
None
None
None

基本上,“无”代替温度和湿度的传感器值。

答案

请问,因为您使用的是Python 3,所以使用Python 3安装库:

sudo python3 setup.py install
另一答案

对于Python3,您需要在打印字后使用括号。

print('Temp: {0:0.1f} C  Humidity: {1:0.1f} %'.format(temperature, humidity))

以上是关于Adafruit_DHT不适用于python 3 Raspberry Pi 3 B.的主要内容,如果未能解决你的问题,请参考以下文章