'Adafruit_DHT' has no attribute 'DHT22'

Moderators: adafruit_support_bill, adafruit

Forum rules
Talk about Adafruit Raspberry Pi® accessories! Please do not ask for Linux support, this is for Adafruit products only! For Raspberry Pi help please visit: http://www.raspberrypi.org/phpBB3/
Locked
User avatar
Mr_Mack
 
Posts: 4
Joined: Thu Feb 18, 2021 3:41 pm

'Adafruit_DHT' has no attribute 'DHT22'

Post by Mr_Mack »

I was setting up two DHT22 sensors using python 3 on a Raspberry Pi Zero W following the directions here https://pimylifeup.com/raspberry-pi-hum ... sor-dht22/

I had to alter the code but I was ultimately able to get everything working as I wanted (which is impressive considering I don't know how to code). But after an unrelated mistake I had to re-image the microSD and reinstall everything. After that the program stopped working. Now when I run it I get:

Code: Select all

AttributeError: module 'Adafruit_DHT' has no attribute 'DHT22'
I can't find much online and most of what I found didn't help. Updating and reinstalling the library doesn't seem to make a difference. Using "sudo" also doesn't help.

I'll post the code below. I'm not a programmer, but it seemed to be working before I had to start over. And I'm running it on Raspberry Pi Lite OS. Any help would be greatly appreciated.

Code: Select all

import os
import time
import Adafruit_DHT
import datetime
import board

DHT_SENSOR1 = Adafruit_DHT.DHT22
DHT_PIN1 = 4

DHT_SENSOR2 = Adafruit_DHT.DHT22
DHT_PIN2 = 11

try:	
    f = open('/mnt/usb/temp.csv', 'a+')	
    if os.stat('/mnt/usb/temp.csv').st_size == 0:		
        f.write('Date, Time, Temp1, Humidity1, Temp2, Humidity2\r\n')

except:	
    pass

while True:	
    humidity, temperature = Adafruit_DHT.read_retry(DHT_SENSOR1, DHT_PIN1)
    temperature = temperature * 9/5.0 + 32	
    f = open('/mnt/usb/temp.csv', 'a+')	

    if humidity is not None and temperature is not None:
        f.write('\r\n{0}, {1}, {2:0.1f}*F, {3:0.1f}%'.format(time.strftime('%y/%m/%d'), time.strftime('%H:%M:%S'), temperature, humidity))
        now = datetime.datetime.now()
        print (now.strftime("%y/%m/%d %H:%M:%S"))
        print("Sensor1 Temp={0:0.1f}*F Humidity={1:0.1f}%".format(temperature, humidity))

    else:
        print("failed to retrieve data from humidity sensor")

    humidity, temperature = Adafruit_DHT.read_retry(DHT_SENSOR2, DHT_PIN2)
    temperature = temperature * 9/5.0 + 32
    f = open('/mnt/usb/temp.csv', 'a+')

    if humidity is not None and temperature is not None:
        f.write(' ,{2:0.1f}*F, {3:0.1f}%'.format(time.strftime('%y/%m/%d'), time.strftime('%H:%M:%S'), temperature, humidity))
        print("Sensor2 Temp={0:0.1f}*F Humidity={1:0.1f}%".format(temperature, humidity))
        time.sleep(300)	

else:
        print("failed to retrieve data from humidity sensor")

User avatar
dastels
 
Posts: 15653
Joined: Tue Oct 20, 2015 3:22 pm

Re: 'Adafruit_DHT' has no attribute 'DHT22'

Post by dastels »

Odd. Can you get into the REPL and type

Code: Select all

import Adafruit_DHT
dir(Adafruit_DHT)
Dave

User avatar
Mr_Mack
 
Posts: 4
Joined: Thu Feb 18, 2021 3:41 pm

Re: 'Adafruit_DHT' has no attribute 'DHT22'

Post by Mr_Mack »

Thanks for the fast reply! I'll do that as soon as I get home. Even though it's a work project I'm not actually working on it at work.

For what am I looking? If something's missing, could I find it on GitHub?

User avatar
dastels
 
Posts: 15653
Joined: Tue Oct 20, 2015 3:22 pm

Re: 'Adafruit_DHT' has no attribute 'DHT22'

Post by dastels »

Well, it should have DHT22 in the list as well as DHT11 and some other things, so I'm curious what it's finding.

Dave

User avatar
Mr_Mack
 
Posts: 4
Joined: Thu Feb 18, 2021 3:41 pm

Re: 'Adafruit_DHT' has no attribute 'DHT22'

Post by Mr_Mack »

I misread your post. I don't actually know what REPL is and I'm still new to all of this. So when I looked it up I couldn't figure out how to use it.

I ended up re-imaging the microSD card and starting over. That seemed to fix the module problem, but a different problem popped up. Then I dropped the breadboard and the wires came loose. Things are going well.

I guess a different module I had installed was causing a conflict? I can't recall why "import board" is in the code, but I didn't reinstall that module and now it works? The library, at least, if not the code as a whole.

Thank you for your help.

User avatar
dastels
 
Posts: 15653
Joined: Tue Oct 20, 2015 3:22 pm

Re: 'Adafruit_DHT' has no attribute 'DHT22'

Post by dastels »

It's common/recommended to use the board module to access pins rather than use numbers like you tend to in Arduino. So you'd use things like board.D4 rather than 4. Board defines/provides what is available on the board.

The REPL is invaluable for trying things out as well as debugging. See
https://learn.adafruit.com/welcome-to-c ... al-console
https://learn.adafruit.com/welcome-to-c ... al-console
https://learn.adafruit.com/welcome-to-c ... n/the-repl.

There are several modules that are part of CircuitPython, either implemented in C++ or "baked in". It varies based on the board, and you can see what internal modules are present per board at https://circuitpython.readthedocs.io/en ... atrix.html.

Dave

User avatar
Mr_Mack
 
Posts: 4
Joined: Thu Feb 18, 2021 3:41 pm

Re: 'Adafruit_DHT' has no attribute 'DHT22'

Post by Mr_Mack »

I'm using Python 3, not Circuit Python. But that's just because I found an in depth tutorial for it. I don't have any technical reason for doing so.

Should I be using Circuit Python? I'm running on a Raspberry Pi Zero W. At the moment I'm more concerned with getting the final product than with educating myself.

Locked
Forum rules
Talk about Adafruit Raspberry Pi® accessories! Please do not ask for Linux support, this is for Adafruit products only! For Raspberry Pi help please visit: http://www.raspberrypi.org/phpBB3/

Return to “Adafruit Raspberry Pi® accessories”