DHT22 Sensor import Adafruit_DHT error

寵の児 提交于 2020-01-02 04:50:48

问题


So I've properly attached DHT22 Humidity Sensor to my BeagleBone Black Rev C. I'm running OS Mavericks on my MacBook Pro and I followed the directions provided by Adafruit on how to use my DHT22

The website I used was pretty clear: https://learn.adafruit.com/dht-humidity-sensing-on-raspberry-pi-with-gdocs-logging/software-install-updated

Also here is the github files I cloned: https://github.com/adafruit/Adafruit_Python_DHT

I put in these lines:

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

cd Adafruit_Python_DHT

sudo apt-get upgrade

sudo apt-get install build-essential python-dev

sudo python setup.py install

cd examples

sudo ./AdafruitDHT.py 22 P8_11

I am successful until that last line. Once I enter that last line (sudo ./AdafruitDHT.py 22 P8_11), I get the following error message:

Traceback (most recent call last):
  File "./AdafruitDHT.py", line 23, in <module>
    import Adafruit_DHT
ImportError: No module named Adafruit_DHT

I know there is an Adafruit_DHT file somewhere because when I ls in the Adafruit_Python_DHT directory, I get this:

root@beaglebone:~/Adafruit_Python_DHT# ls
Adafruit_DHT  examples  ez_setup.py  ez_setup.pyc  LICENSE  README.md  setup.py  source

I've tried reinstalling the setup.py, but the outcome is still the same.

I've followed all the directions Adafruit provided, but I just can't seem to get past this. Any idea on what is going on? It seems like a simple problem, but it's proving to be one major obstacle in getting readings from my DHT22. If there is more information needed to help answer this problem please let me know.


回答1:


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.




回答2:


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?




回答3:


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()


来源:https://stackoverflow.com/questions/26246239/dht22-sensor-import-adafruit-dht-error

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