DHT22 Sensor import Adafruit_DHT error

喜欢而已 提交于 2019-12-05 09:02:16
ndb23

Easy fix:

cd Adafruit_Python_DHT

sudo apt-get update

sudo apt-get install build-essential python-dev python-openssl

sudo python setup.py install

Try to run sudo ./AdafruitDHT.py ## ## ... file again

You may have forgot to run the setup properly.

PS1

It seems that your script cannot find the "Adafruit_DHT" module. There are two ways.

  1. Run the file in the terminal as "Python Adafruit_Python_DHT"

  2. Add the following code at the first line of your script. Should I put #! (shebang) in Python scripts, and what form should it take?

Ok,try running this script with "sudo".

import sys
import Adafruit_DHT

def main():
    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] GPIOpin#'
        print 'example: sudo ./Adafruit_DHT.py 2302 4 - Read from an AM2302 connected to GPIO #4'
        sys.exit(1)

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

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!'

if __name__ == '__main__':
   main()
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!