DHT22 Sensor import Adafruit_DHT error
Asked Answered
S

3

6

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.

Substantive answered 7/10, 2014 at 22:20 Comment(0)
C
3

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.

Chagrin answered 30/5, 2016 at 15:8 Comment(0)
B
0

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?

Byler answered 8/10, 2014 at 7:30 Comment(3)
I appreciate the help so much. I did what you said and by running the file in the terminal as "python Adafruit_Python_DHT" , I get: {/usr/bin/python: can't find '_____main_____' module in Adafruit_Python_DHT} I have successfully run the BMP180 sensor on my beagleboneblack with Adafruit's code and I noticed itdoesn't have a _____main_____ module.Adafruit did warn me that "This tutorial is a first attempt to develop a DHT interface driver. It is not guaranteed to work, and is for experimentation and hacking!" so I guess this is the reason it doesn't work. A lot of things in general are missing.Substantive
Ok,can you try and add if name == 'main': main() the two lines in that Adafruti's code, "main" / main() could be in other names.Byler
I don't understand how you want me to add this. Add it before import Adafruit_DHT? what two lines are you talking about? As in: if name == 'main': main() import Adafruit_DHTSubstantive
B
0

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()
Byler answered 10/10, 2014 at 5:15 Comment(6)
root@beaglebone:~# cd Adafruit_Python_DHT root@beaglebone:~/Adafruit_Python_DHT# cd examples root@beaglebone:~/Adafruit_Python_DHT/examples# nano hope.py root@beaglebone:~/Adafruit_Python_DHT/examples# sudo python hope.py Traceback (most recent call last): File "hope.py", line 2, in <module> import Adafruit_DHT ImportError: No module named Adafruit_DHT same problem :/ i don't know why I can't import Adafruit_DHT when Adafruit_BMP works just fine for their BMP180 sensor. the folder layout is very similiar. must be something missing in the coding .again i truly appreciate the effortSubstantive
btw i copied your script into hope.pySubstantive
What if you re-install the drivers again? Maybe that would work.Byler
I re-installed with no success :/Substantive
this fixed it: sudo python setup.py install --forceSubstantive
Nice job,man. In the future if someone gets the same problem. I'll redirect them to this link. Cheers.Byler

© 2022 - 2024 — McMap. All rights reserved.