esphome / issues

Issue Tracker for ESPHome
https://esphome.io/
291 stars 34 forks source link

pi pico w doesn't read DHT22 #4615

Open SDMatt2020 opened 1 year ago

SDMatt2020 commented 1 year ago

The problem

My pi pico w isn't getting data from a DHT22. If I put micropython on the pi pico w, it reads the sensor as expected.

This is on ESPHome 2023.6.2

Which version of ESPHome has the issue?

2023.6.2

What type of installation are you using?

Home Assistant Add-on

Which version of Home Assistant has the issue?

2023.6.3

What platform are you using?

RP2040

Board

pi pico w

Component causing the issue

dht sensor

Example YAML snippet

sensor:
  - platform: dht
    pin: 5
    temperature:
      name: "DEV - Living Room Temperature"
    humidity:
      name: "DEV - Living Room Humidity"
    model: DHT22
    update_interval: 10s

Anything in the logs that might be useful for us?

INFO ESPHome 2023.6.2
INFO Reading configuration /config/esphome/pico-dev.yaml...
INFO Generating C++ source...
INFO Compiling app...
Processing pico-dev (board: rpipicow; framework: arduino; platform: https://github.com/maxgerhardt/platform-raspberrypi.git)
--------------------------------------------------------------------------------
HARDWARE: RP2040 133MHz, 264KB RAM, 2MB Flash
 - framework-arduinopico @ 1.20604.0 (2.6.4) 
 - tool-rp2040tools @ 1.0.2 
Flash size: 2.00MB
Sketch size: 1.00MB
Filesystem size: 1.00MB
Maximium Sketch size: 1044480 EEPROM start: 0x101ff000 Filesystem start: 0x100ff000 Filesystem end: 0x101ff000
Dependency Graph
|-- WiFi @ 1.0.0
|-- LEAmDNS @ 1.2
|-- Updater @ 1.0
|-- noise-c @ 0.1.4
|-- MD5Builder @ 1.0.0
Compiling /data/pico-dev/.pioenvs/pico-dev/src/esphome/components/api/api_connection.cpp.o
Compiling /data/pico-dev/.pioenvs/pico-dev/src/esphome/components/api/api_frame_helper.cpp.o
Compiling /data/pico-dev/.pioenvs/pico-dev/src/esphome/components/api/api_pb2.cpp.o
Compiling /data/pico-dev/.pioenvs/pico-dev/src/esphome/components/api/api_pb2_service.cpp.o
Compiling /data/pico-dev/.pioenvs/pico-dev/src/esphome/components/api/api_server.cpp.o
Compiling /data/pico-dev/.pioenvs/pico-dev/src/esphome/components/api/list_entities.cpp.o
In file included from /data/cache/platformio/packages/toolchain-rp2040-earlephilhower/arm-none-eabi/include/c++/10.3.0/vector:72,
                 from /data/cache/platformio/packages/toolchain-rp2040-earlephilhower/arm-none-eabi/include/c++/10.3.0/functional:62,
                 from src/esphome/core/component.h:4,
                 from src/esphome/components/api/proto.h:3,
                 from src/esphome/components/api/api_pb2.h:5,
                 from src/esphome/components/api/api_pb2.cpp:3:
/data/cache/platformio/packages/toolchain-rp2040-earlephilhower/arm-none-eabi/include/c++/10.3.0/bits/vector.tcc: In member function 'void std::vector<_Tp, _Alloc>::_M_realloc_insert(std::vector<_Tp, _Alloc>::iterator, _Args&& ...) [with _Args = {esphome::api::BluetoothLERawAdvertisement}; _Tp = esphome::api::BluetoothLERawAdvertisement; _Alloc = std::allocator<esphome::api::BluetoothLERawAdvertisement>]':
/data/cache/platformio/packages/toolchain-rp2040-earlephilhower/arm-none-eabi/include/c++/10.3.0/bits/vector.tcc:426:7: note: parameter passing for argument of type 'std::vector<esphome::api::BluetoothLERawAdvertisement>::iterator' changed in GCC 7.1
  426 |       vector<_Tp, _Alloc>::
      |       ^~~~~~~~~~~~~~~~~~~
/data/cache/platformio/packages/toolchain-rp2040-earlephilhower/arm-none-eabi/include/c++/10.3.0/bits/vector.tcc: In member function 'virtual bool esphome::api::BluetoothLERawAdvertisementsResponse::decode_length(uint32_t, esphome::api::ProtoLengthDelimited)':
/data/cache/platformio/packages/toolchain-rp2040-earlephilhower/arm-none-eabi/include/c++/10.3.0/bits/vector.tcc:121:21: note: parameter passing for argument of type '__gnu_cxx::__normal_iterator<esphome::api::BluetoothLERawAdvertisement*, std::vector<esphome::api::BluetoothLERawAdvertisement> >' changed in GCC 7.1
  121 |    _M_realloc_insert(end(), std::forward<_Args>(__args)...);
      |    ~~~~~~~~~~~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
/data/cache/platformio/packages/toolchain-rp2040-earlephilhower/arm-none-eabi/include/c++/10.3.0/bits/vector.tcc: In member function 'void std::vector<_Tp, _Alloc>::_M_realloc_insert(std::vector<_Tp, _Alloc>::iterator, _Args&& ...) [with _Args = {long long unsigned int}; _Tp = long long unsigned int; _Alloc = std::allocator<long long unsigned int>]':
/data/cache/platformio/packages/toolchain-rp2040-earlephilhower/arm-none-eabi/include/c++/10.3.0/bits/vector.tcc:426:7: note: parameter passing for argument of type 'std::vector<long long unsigned int>::iterator' changed in GCC 7.1
  426 |       vector<_Tp, _Alloc>::
      |       ^~~~~~~~~~~~~~~~~~~
/data/cache/platformio/packages/toolchain-rp2040-earlephilhower/arm-none-eabi/include/c++/10.3.0/bits/vector.tcc: In member function 'std::vector<_Tp, _Alloc>::reference std::vector<_Tp, _Alloc>::emplace_back(_Args&& ...) [with _Args = {long long unsigned int}; _Tp = long long unsigned int; _Alloc = std::allocator<long long unsigned int>]':
/data/cache/platformio/packages/toolchain-rp2040-earlephilhower/arm-none-eabi/include/c++/10.3.0/bits/vector.tcc:121:21: note: parameter passing for argument of type '__gnu_cxx::__normal_iterator<long long unsigned int*, std::vector<long long unsigned int> >' changed in GCC 7.1
  121 |    _M_realloc_insert(end(), std::forward<_Args>(__args)...);
      |    ~~~~~~~~~~~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Compiling /data/pico-dev/.pioenvs/pico-dev/src/esphome/components/api/proto.cpp.o
Compiling /data/pico-dev/.pioenvs/pico-dev/src/esphome/components/api/subscribe_state.cpp.o
Compiling /data/pico-dev/.pioenvs/pico-dev/src/esphome/components/api/user_services.cpp.o
Compiling /data/pico-dev/.pioenvs/pico-dev/src/esphome/components/dht/dht.cpp.o
Compiling /data/pico-dev/.pioenvs/pico-dev/src/esphome/components/logger/logger.cpp.o
Compiling /data/pico-dev/.pioenvs/pico-dev/src/esphome/components/md5/md5.cpp.o
Compiling /data/pico-dev/.pioenvs/pico-dev/src/esphome/components/mdns/mdns_component.cpp.o
Compiling /data/pico-dev/.pioenvs/pico-dev/src/esphome/components/mdns/mdns_esp32.cpp.o
Compiling /data/pico-dev/.pioenvs/pico-dev/src/esphome/components/mdns/mdns_esp8266.cpp.o
Compiling /data/pico-dev/.pioenvs/pico-dev/src/esphome/components/mdns/mdns_host.cpp.o
Compiling /data/pico-dev/.pioenvs/pico-dev/src/esphome/components/mdns/mdns_rp2040.cpp.o
Compiling /data/pico-dev/.pioenvs/pico-dev/src/esphome/components/network/util.cpp.o
Compiling /data/pico-dev/.pioenvs/pico-dev/src/esphome/components/ota/ota_backend_arduino_esp32.cpp.o
Compiling /data/pico-dev/.pioenvs/pico-dev/src/esphome/components/ota/ota_backend_arduino_esp8266.cpp.o
Compiling /data/pico-dev/.pioenvs/pico-dev/src/esphome/components/ota/ota_backend_arduino_rp2040.cpp.o
Compiling /data/pico-dev/.pioenvs/pico-dev/src/esphome/components/ota/ota_backend_esp_idf.cpp.o
Compiling /data/pico-dev/.pioenvs/pico-dev/src/esphome/components/ota/ota_component.cpp.o
Compiling /data/pico-dev/.pioenvs/pico-dev/src/esphome/components/rp2040/gpio.cpp.o
Compiling /data/pico-dev/.pioenvs/pico-dev/src/esphome/components/rp2040/preferences.cpp.o
Compiling /data/pico-dev/.pioenvs/pico-dev/src/esphome/components/sensor/automation.cpp.o
Compiling /data/pico-dev/.pioenvs/pico-dev/src/esphome/components/sensor/filter.cpp.o
Compiling /data/pico-dev/.pioenvs/pico-dev/src/esphome/components/sensor/sensor.cpp.o
Compiling /data/pico-dev/.pioenvs/pico-dev/src/esphome/components/socket/bsd_sockets_impl.cpp.o
Compiling /data/pico-dev/.pioenvs/pico-dev/src/esphome/components/socket/lwip_raw_tcp_impl.cpp.o
Compiling /data/pico-dev/.pioenvs/pico-dev/src/esphome/components/socket/socket.cpp.o
Compiling /data/pico-dev/.pioenvs/pico-dev/src/esphome/components/wifi/wifi_component.cpp.o
Compiling /data/pico-dev/.pioenvs/pico-dev/src/esphome/components/wifi/wifi_component_esp32_arduino.cpp.o
Compiling /data/pico-dev/.pioenvs/pico-dev/src/esphome/components/wifi/wifi_component_esp8266.cpp.o
Compiling /data/pico-dev/.pioenvs/pico-dev/src/esphome/components/wifi/wifi_component_esp_idf.cpp.o
Compiling /data/pico-dev/.pioenvs/pico-dev/src/esphome/components/wifi/wifi_component_pico_w.cpp.o
Compiling /data/pico-dev/.pioenvs/pico-dev/src/esphome/core/application.cpp.o
Compiling /data/pico-dev/.pioenvs/pico-dev/src/esphome/core/component.cpp.o
Compiling /data/pico-dev/.pioenvs/pico-dev/src/esphome/core/component_iterator.cpp.o
Compiling /data/pico-dev/.pioenvs/pico-dev/src/esphome/core/controller.cpp.o
Compiling /data/pico-dev/.pioenvs/pico-dev/src/esphome/core/entity_base.cpp.o
Compiling /data/pico-dev/.pioenvs/pico-dev/src/esphome/core/helpers.cpp.o
Compiling /data/pico-dev/.pioenvs/pico-dev/src/esphome/core/log.cpp.o
Compiling /data/pico-dev/.pioenvs/pico-dev/src/esphome/core/scheduler.cpp.o
Compiling /data/pico-dev/.pioenvs/pico-dev/src/esphome/core/string_ref.cpp.o
Compiling /data/pico-dev/.pioenvs/pico-dev/src/esphome/core/util.cpp.o
Compiling /data/pico-dev/.pioenvs/pico-dev/src/main.cpp.o
Linking /data/pico-dev/.pioenvs/pico-dev/firmware.elf
Generating UF2 image
elf2uf2 "/data/pico-dev/.pioenvs/pico-dev/firmware.elf" "/data/pico-dev/.pioenvs/pico-dev/firmware.uf2"
Flash size: 2.00MB
Sketch size: 1.00MB
Filesystem size: 1.00MB
Maximium Sketch size: 1044480 EEPROM start: 0x101ff000 Filesystem start: 0x100ff000 Filesystem end: 0x101ff000
RAM:   [===       ]  26.4% (used 69252 bytes from 262144 bytes)
Flash: [=====     ]  48.5% (used 506544 bytes from 1044480 bytes)
Building /data/pico-dev/.pioenvs/pico-dev/firmware.bin
Building /data/pico-dev/.pioenvs/pico-dev/firmware.bin.signed
========================= [SUCCESS] Took 38.47 seconds =========================
INFO Successfully compiled program.
INFO Resolving IP address of pico-dev.local
INFO  -> 192.168.1.9
INFO Uploading /data/pico-dev/.pioenvs/pico-dev/firmware.bin (518576 bytes)
Uploading: [============================================================] 100% Done...

INFO Waiting for result...
INFO OTA successful
INFO Successfully uploaded program.
INFO Starting log output from pico-dev.local using esphome API
WARNING Can't connect to ESPHome API for pico-dev.local: Error resolving IP address: [Errno -5] No address associated with hostname
INFO Trying to connect to pico-dev.local in the background
INFO Successfully connected to pico-dev.local
[16:10:41][I][app:102]: ESPHome version 2023.6.2 compiled on Jun 24 2023, 16:10:15
[16:10:41][C][wifi:543]: WiFi:
[16:10:41][C][wifi:379]:   Local MAC: XXXXX
[16:10:41][C][wifi:380]:   SSID: 'XXXXX'[redacted]
[16:10:41][C][wifi:381]:   IP Address: 192.168.1.9
[16:10:41][C][wifi:382]:   BSSID: XXXXXX[redacted]
[16:10:41][C][wifi:384]:   Hostname: 'pico-dev'
[16:10:41][C][wifi:386]:   Signal strength: -47 dB ▂▄▆█
[16:10:41][C][wifi:390]:   Channel: 6
[16:10:41][C][wifi:391]:   Subnet: 255.255.255.0
[16:10:41][C][wifi:392]:   Gateway: 192.168.1.1
[16:10:41][C][wifi:393]:   DNS1: 208.67.222.222
[16:10:41][C][wifi:394]:   DNS2: 208.67.220.220
[16:10:41][C][logger:301]: Logger:
[16:10:41][C][logger:302]:   Level: DEBUG
[16:10:41][C][logger:303]:   Log Baud Rate: 115200
[16:10:41][C][logger:305]:   Hardware UART: USB_CDC
[16:10:41][C][dht:017]: DHT:
[16:10:41][C][dht:018]:   Pin: GPIO5
[16:10:41][C][dht:024]:   Model: DHT22 (or equivalent)
[16:10:41][C][dht:027]:   Update Interval: 10.0s
[16:10:41][C][dht:029]:   Temperature 'DEV - Living Room Temperature'
[16:10:41][C][dht:029]:     Device Class: 'temperature'
[16:10:41][C][dht:029]:     State Class: 'measurement'
[16:10:41][C][dht:029]:     Unit of Measurement: '°C'
[16:10:41][C][dht:029]:     Accuracy Decimals: 1
[16:10:41][C][dht:030]:   Humidity 'DEV - Living Room Humidity'
[16:10:41][C][dht:030]:     Device Class: 'humidity'
[16:10:41][C][dht:030]:     State Class: 'measurement'
[16:10:41][C][dht:030]:     Unit of Measurement: '%'
[16:10:41][C][dht:030]:     Accuracy Decimals: 0
[16:10:41][C][mdns:108]: mDNS:
[16:10:41][C][mdns:109]:   Hostname: pico-dev
[16:10:41][C][ota:093]: Over-The-Air Updates:
[16:10:41][C][ota:094]:   Address: pico-dev.local:2040
[16:10:41][C][ota:097]:   Using Password.
[16:10:41][C][api:138]: API Server:
[16:10:41][C][api:139]:   Address: pico-dev.local:6053
[16:10:41][C][api:141]:   Using noise encryption: YES
[16:10:50][W][dht:169]: Requesting data from DHT failed!
[16:10:50][W][dht:060]: Invalid readings! Please check your wiring (pull-up resistor, pin number).
[16:10:50][D][sensor:093]: 'DEV - Living Room Temperature': Sending state inf °C with 1 decimals of accuracy
[16:10:50][D][sensor:093]: 'DEV - Living Room Humidity': Sending state inf % with 0 decimals of accuracy
[16:11:00][W][dht:169]: Requesting data from DHT failed!
[16:11:00][W][dht:060]: Invalid readings! Please check your wiring (pull-up resistor, pin number).
[16:11:00][D][sensor:093]: 'DEV - Living Room Temperature': Sending state inf °C with 1 decimals of accuracy
[16:11:00][D][sensor:093]: 'DEV - Living Room Humidity': Sending state inf % with 0 decimals of accuracy

Additional information

here's the micropython code I used to check the functionality of the setup

from machine import Pin from time import sleep import dht

sensor = dht.DHT22(Pin(5))

while True: try: sleep(1) sensor.measure()

Retrieves measurements from the sensor

    print(f"Temperature : {sensor.temperature():.1f}")
    print(f"Humidity    : {sensor.humidity():.1f}")
    # Transmits the temperature to the terminal
except OSError as e:
    print('Failed reception')
    # If the pico does not receive the measurements from the sensor
SDMatt2020 commented 1 year ago

I forgot to mention. I've tried this with no resister, a 5K resister, and a 10K resistor (as the pull up). I've also tried running this at 3.3 and 5 volts, as I came across a post somewhere that suggested the DHT22 might run better at 5v.

Mat931 commented 1 year ago

Looks like the delayMicroseconds function is the problem.

https://forum.seeedstudio.com/t/ds18b20-temperature-sensor-issue/263525

Try adding this to your config:

external_components:
  - source: github://Mat931/esphome@rp2040-dht-fix
    components: [ dht ]
    refresh: always
SDMatt2020 commented 1 year ago

Looks like the delayMicroseconds function is the problem.

https://forum.seeedstudio.com/t/ds18b20-temperature-sensor-issue/263525

Try adding this to your config:

external_components:
  - source: github://Mat931/esphome@rp2040-dht-fix
    components: [ dht ]
    refresh: always

Thanks, but that didn't help.

Mat931 commented 1 year ago

Thanks for testing. I'm still waiting for my Pico W. Once it arrives I can take a look with the logic analyzer

Mat931 commented 1 year ago

My Pico W just arrived and it can read the DHT22 (AM2302) sensor just fine, both with and without the "fix". I'm using a 10k pull-up resistor.

SDMatt2020 commented 1 year ago

I wonder if there's some manufacturing variance in the DHT22s, and perhaps the micropython library is more forgiving. I'll also put a meter on the resister to verify its resistance, but I doubt the resistor is the issue here.

Where did you get your DHT22? If it's a generic Amazon/AliExpress part can you post a link (unless that's against some Github policy)?

Mat931 commented 1 year ago

Sorry, my sensor is at least 5 years old and I have no clue where I bought it. I can do some more testing with MicroPython vs. ESPHome later. By the way I saw the same error message for a while and thought I reproduced the issue but turns out the sensor didn't make good contact to the wires.

Mat931 commented 1 year ago

MicroPython: (18ms delay) micropython

ESPHome: (0.8ms delay) esphome

In MicroPython there's a longer delay before the data is transmitted

Mat931 commented 1 year ago

@SDMatt2020 Can you try configuring other DHT models in yaml? Available models are: DHT11, DHT22, DHT22_TYPE2, AM2302, RHT03, SI7021

SDMatt2020 commented 1 year ago

I just tried all those models without success.

SDMatt2020 commented 1 year ago

At this point I'm planning on buying some more sensors. I'm betting there's something weird about these DHT22s that I have. I have an application that should work fine with the cheaper DHT11s, so I plan on buying some from a reputable seller in hopes they'll work with ESPHome.

The current DHT22s I have were sold under the DWEII brand name. They are 3 pin sensors, rather than the 4 pin that you commonly see from sellers like adafruit.

Fixitman333 commented 1 year ago

I'm thinking this is related to manufacturing differences vs. delay before data transmission. Some sensors "wake up" quickly, while others are slow.

I just tested my DHT11 with micropython, and it wouldn't work. I added a 2 second delay after creating the sensor object, and it worked normally. I haven't found a way to add a delay using esphome, so my testing has come to a halt.

github-actions[bot] commented 8 months ago

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.

kdpuvvadi commented 2 weeks ago

Seems like it's still issue. Works file with micropython library but but throwing error on esphome. though.