Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

DHT22 / AM2302 does not read sensor data on Pi 4 (likely DHT11 as well) #37382

Closed
nchorekchyan opened this issue Jul 2, 2020 · 18 comments · Fixed by #41525
Closed

DHT22 / AM2302 does not read sensor data on Pi 4 (likely DHT11 as well) #37382

nchorekchyan opened this issue Jul 2, 2020 · 18 comments · Fixed by #41525

Comments

@nchorekchyan
Copy link

nchorekchyan commented Jul 2, 2020

The problem

DHT Sensor does not appear to work for AM2302 on PI4. The exact same config / pinout /sensor works fine on a Pi3. The issue appears to be the depricated Adafruit_DHT library which has been replace by adafruit-circuitpython-dht

New Library:
https://github.com/adafruit/Adafruit_CircuitPython_DHT

Deprecated Library
https://github.com/adafruit/Adafruit_Python_DHT

Don't be fooled by the "Include Raspberry Pi 4" description on the folder for the deprecated library, though the device name is the same, it does not work.

Testing Done:
This was confirmed by running the AdafruitDHT.py manually FROM THE HOST (not within the container) and not getting any reading info.

pi@Pi4:~/Adafruit_Python_DHT/examples $ sudo ./AdafruitDHT.py 2302 4
Failed to get reading. Try again!

However, after installing the Adafruit_CircuitPython_DHT library and running the example provided in the git from the host (again, not the container):

pi@Pi4:~ $ python3 dht_simpletest.py
Temp: 82.6 F / 28.1 C Humidity: 58.0%
Temp: 82.6 F / 28.1 C Humidity: 58.1%

These findings seem to correlate with the error provided in the homeassistant logs, where HA is using the deprecated library:

Environment

  • Home Assistant Core release with the issue: Home Assistant 0.112.0
  • Last working Home Assistant Core release (if known): Pi3
  • Operating environment (OS/Container/Supervised/Core): Docker Container
  • Integration causing this issue: DHT
  • Link to integration documentation on our website: https://www.home-assistant.io/integrations/dht/

Problem-relevant configuration.yaml

  • platform: dht
    sensor: AM2302
    pin: 4
    temperature_offset: 0
    humidity_offset: 0
    monitored_conditions:
    • 'temperature'
    • 'humidity'

Traceback/Error logs

2020-07-02 17:33:39 ERROR (MainThread) [homeassistant.components.sensor] dht: Error on device update!
Traceback (most recent call last):
File "/usr/src/homeassistant/homeassistant/helpers/entity_platform.py", line 319, in _async_add_entity
await entity.async_device_update(warning=False)
File "/usr/src/homeassistant/homeassistant/helpers/entity.py", line 466, in async_device_update
self.update # type: ignore
File "/usr/local/lib/python3.7/concurrent/futures/thread.py", line 57, in run
result = self.fn(*self.args, **self.kwargs)
File "/usr/src/homeassistant/homeassistant/components/dht/sensor.py", line 137, in update
self.dht_client.update()
File "/usr/src/homeassistant/homeassistant/util/init.py", line 239, in wrapper
result = method(*args, **kwargs)
File "/usr/src/homeassistant/homeassistant/components/dht/sensor.py", line 173, in update
humidity, temperature = self.adafruit_dht.read_retry(self.sensor, self.pin)
File "/usr/local/lib/python3.7/site-packages/Adafruit_DHT/common.py", line 94, in read_retry
humidity, temperature = read(sensor, pin, platform)
File "/usr/local/lib/python3.7/site-packages/Adafruit_DHT/common.py", line 80, in read
platform = get_platform()
File "/usr/local/lib/python3.7/site-packages/Adafruit_DHT/common.py", line 63, in get_platform
raise RuntimeError('Unknown platform.')
RuntimeError: Unknown platform.
2020-07-02 17:33:39 ERROR (MainThread) [homeassistant.components.sensor] dht: Error on device update!
Traceback (most recent call last):
File "/usr/src/homeassistant/homeassistant/helpers/entity_platform.py", line 319, in _async_add_entity
await entity.async_device_update(warning=False)
File "/usr/src/homeassistant/homeassistant/helpers/entity.py", line 466, in async_device_update
self.update # type: ignore
File "/usr/local/lib/python3.7/concurrent/futures/thread.py", line 57, in run
result = self.fn(*self.args, **self.kwargs)
File "/usr/src/homeassistant/homeassistant/components/dht/sensor.py", line 137, in update
self.dht_client.update()
File "/usr/src/homeassistant/homeassistant/util/init.py", line 239, in wrapper
result = method(*args, **kwargs)
File "/usr/src/homeassistant/homeassistant/components/dht/sensor.py", line 173, in update
humidity, temperature = self.adafruit_dht.read_retry(self.sensor, self.pin)
File "/usr/local/lib/python3.7/site-packages/Adafruit_DHT/common.py", line 94, in read_retry
humidity, temperature = read(sensor, pin, platform)
File "/usr/local/lib/python3.7/site-packages/Adafruit_DHT/common.py", line 80, in read
platform = get_platform()
File "/usr/local/lib/python3.7/site-packages/Adafruit_DHT/common.py", line 63, in get_platform
raise RuntimeError('Unknown platform.')

Additional information

uname -a: Linux Pi4 5.4.50-v7l+ #1324 SMP Wed Jul 1 17:06:14 BST 2020 armv7l GNU/Linux

@probot-home-assistant
Copy link

dht documentation
dht source
(message by IssueLinks)

@edofullin
Copy link

Can confirm it's not working.

I'm trying to solve the issue but it seems that the new library does not have support for AM2302 (but i don't have one so I can't try)

How do we address that?

@Gordontgopher
Copy link

Just passing by, but there are a few issues (e.g. adafruit/Adafruit_CircuitPython_DHT#33) that might point to it being an issue with the library? (AIUI AM2302 is simply a wired and packaged DHT22)

@nchorekchyan
Copy link
Author

nchorekchyan commented Jul 27, 2020

Hey @edofullin
The tests I did in my original post was with a AM2302. Its the same device as the DHT22, so I can confirm the edit:NEW library does in fact work, at least from a raspbian perspective.

@edofullin
Copy link

edofullin commented Jul 27, 2020

Hey @edofullin
The tests I did in my original post was with a AM2302. Its the same device as the DHT22, so I can confirm the edit:NEW library does in fact work, at least from a raspbian perspective.

Ok, thanks for the info, I'll try to address the problem

@edofullin
Copy link

I see you had problems with 0.112.0, I remember 0.112.4 working fine for a while for me, that's odd.

@ilvalle
Copy link

ilvalle commented Sep 19, 2020

Hi there, I've a rpi4 with a AM2302 sensor too, my experience is that after a while the sensor holds, I've tried with different setup with both 3.3V and 5V, different resistors and a capacitor as stated in the manual.
In order to avoid other confusion it would be helpful to update home assistant with the latest library for such sensors which is Adafruit_CircuitPython_DHT.

@sebastianeferrer
Copy link

Hi there! I have done an ugly workarround to make it work on RPi4. It works while you wait until somebody updates Adafruit_DHT to latest version. If nobody does, when I have some time and get to know how to do it, I will do it in the proper way.

You have to attach to your docker container running homeassistant. First run:

$ docker ps
6ae78f22c7ac        homeassistant/armv7-hassio-observer:3              "/init"                  2 weeks ago         Up 15 hours         0.0.0.0:4357->80/tcp   hassio_observer
241ee026029b        homeassistant/raspberrypi4-homeassistant:0.109.5   "/init"                  4 weeks ago         Up 24 minutes                              homeassistant

Then you run (replace 241ee026029b with your corresponding container id) :
docker exec -it 241ee026029b bash
Inside the container you have to edit the following file (if you don't know how to use vi, see this link:
vi /usr/local/lib/python3.7/site-packages/Adafruit_DHT/common.py
comment line 63 and add the following two lines:

        from . import Raspberry_Pi_2                                          
        return Raspberry_Pi_2

get_platform() function should look like this:

def get_platform():                                                                                                                                                                                               
    """Return a DHT platform interface for the currently detected platform."""                                                                                                                                    
    plat = platform_detect.platform_detect()                                                                                                                                                                      
    if plat == platform_detect.RASPBERRY_PI:                                                                                                                                                                      
        # Check for version 1 or 2 of the pi.                                                                                                                                                                     
        version = platform_detect.pi_version()                                                                                                                                                                    
        if version == 1:                                                                                                                                                                                          
            from . import Raspberry_Pi                                                                                                                                                                            
            return Raspberry_Pi                                                                                                                                                                                   
        elif version == 2:                                                                                                                                                                                        
            from . import Raspberry_Pi_2                                                                                                                                                                          
            return Raspberry_Pi_2                                                                                                                                                                                 
        elif version == 3:                                                                                                                                                                                        
            """Use Pi 2 driver even though running on Pi 3"""                                                                                                                                                     
            from . import Raspberry_Pi_2                                                                                                                                                                          
            return Raspberry_Pi_2                                                                                                                                                                                 
        else:                                                                                                                                                                                                     
            raise RuntimeError('No driver for detected Raspberry Pi version available!')                                                                                                                          
    elif plat == platform_detect.BEAGLEBONE_BLACK:                                                                                                                                                                
        from . import Beaglebone_Black                                                                                                                                                                            
        return Beaglebone_Black                                                                                                                                                                                   
    else:                                                                                                                                                                                                         
#        raise RuntimeError('Unknown platform.')                                                                                                                                                                  
        from . import Raspberry_Pi_2                                                                                                                                                                              
        return Raspberry_Pi_2 

To exit the container run:
$ exit

That's all, hope it helps you!

@smoth90
Copy link

smoth90 commented Oct 3, 2020

Hi there! i did a fork of homeassistant in order to try and implement the Adafruit_CircuitPython_DHT since the Adafruit_Python_DHT has been deprecated and to make it work on my Pi 4 without any workaround.
i've done the python coding part and it should work, but i'm unable to run it inside hass docker container to test it properly due to the missing libgpiod2 library (as required system library) as you can see here.
i've tried to add it manually since in the package manager (apk) is not present, but i have errors:

$ apk add libgpiod-dev g++ make
$ git clone https://github.com/adafruit/libgpiod_pulsein.git
$ cd libgpiod_pulsein/src
$ make
gcc -c -O3 -o libgpiod_pulsein.o libgpiod_pulsein.c -I. -lgpiod -pthread -Wall
In file included from libgpiod_pulsein.c:23:
libgpiod_pulsein.h:1:10: fatal error: gpiod.h: No such file or directory
    1 | #include <gpiod.h>
      |          ^~~~~~~~~
compilation terminated.
make: *** [Makefile:7: libgpiod_pulsein.o] Error 1

anyone can help me ?
Then also i will have to address how to change correctly hass released container ( maybe editing the dockerfile ? )

@themultiplexer
Copy link

Had the same dht: Error on device update! and Unknown platform. problem on dockered HA 0.116.4 on a RPi 4 with a DHT22. It had worked for a long time before...

https://stackoverflow.com/a/63311551/9839378 pointed out that it has to to with a raspbian update.

So I did this:

docker exec -it 16e9878eaaa0 bash
-> vi /usr/local/lib/python3.8/site-packages/Adafruit_DHT/platform_detect.py
-> Added changes mentioned in StackOverflow answer
-> exit
Restart homeassistant and everthing works again.

This clearly is only a workaround but I will be looking for updates from Adafruit or a better workaround 😄

@stasukas
Copy link

stasukas commented Nov 11, 2020

For me DHT22 is working fine with version 0.114. It is very stable. After upgrade to 0.115 and newer versions the DHT22 sensor stop working after several hours. I see no data for this sensor. Some times reboot helps to get the sensor to send data again. But sometimes reboots doesn't help.
After downgrade from 0.117 to the latest 0.115 the sensor sometimes still stop working. After downgrade to 0.114.4 the sensor is working fine.
My configuration is RPi3 + ubuntu 18.04 + Supervised HA (in docker)

Update: I'm using DHT22 and relay on my RPi3. May be there is some conflicts with simultaneous request to DHT22 and changing of relay state.

@HedgehogCode
Copy link

Note that there is a PR for this which is not linked: #41525.

@Conobi
Copy link

Conobi commented Feb 2, 2021

This issue is pretty annoying since more and more people are getting a Raspi 4, and the documentation page of DHT sensor should point out this bug until it is fixed.

@SJang1
Copy link

SJang1 commented Feb 19, 2021

Is there any way to use those sensor on RPI4(b) for now before PR merge?

@Madhuraank
Copy link

Madhuraank commented Mar 11, 2021

from pigpio_dht import DHT11, DHT22

gpio = 4 # BCM Numbering

sensor = DHT11(gpio)
#sensor = DHT22(gpio)

result = sensor.read()
print(result)

Even I had the same problem..This code worked for me you can also try once. Before running the code enter the commands on the terminal

sudo pigpiod #Start daemon

pigs pud 4 u # Set internal pull up

If pigpio-dht is not installed enter pip3 install pigpio-dht and run the above program

@mgutierrezp
Copy link

from pigpio_dht import DHT11, DHT22

gpio = 4 # BCM Numbering

sensor = DHT11(gpio) #sensor = DHT22(gpio)

result = sensor.read() print(result)

Even I had the same problem..This code worked for me you can also try once. Before running the code enter the commands on the terminal

sudo pigpiod #Start daemon

pigs pud 4 u # Set internal pull up

If pigpio-dht is not installed enter pip3 install pigpio-dht and run the above program

this worked perfectly for me, thanks!

@jokertaiz
Copy link

I am using DHT22 with Raspberry Pi 4 in Proteus 8.13 for my simulation.

I am still facing "No module named 'adafruit_max7219' [U1] ".

Anyone can help ?

@VictorEsteban
Copy link

VictorEsteban commented Dec 23, 2022

@Madhuraank

Esta es la respuesta! gracias amigo, te amo jajaja (estuve 7 horas buscando una solución y llegue a tu certero comentario <3 )

@home-assistant home-assistant locked as resolved and limited conversation to collaborators Dec 23, 2022
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

Successfully merging a pull request may close this issue.