esphome / issues

Issue Tracker for ESPHome
https://esphome.io/
290 stars 36 forks source link

ESP8266 DHT22 connection failing (Tried all listed solutions) #792

Closed WillT93 closed 4 years ago

WillT93 commented 4 years ago

name: Bug report about: Create a report to help ESPHome improve


Operating environment/Installation (Hass.io/Docker/pip/etc.):

Windows 10 running ESPHome 1.13.6 Macbook Air running ESPHome 1.13.6

ESP (ESP32/ESP8266, Board/Sonoff):

ESP8266 (ESP01_1M) ESP8266 (Sparkfun Thing Dev)

Affected component:

https://esphome.io/components/sensor/dht.html

Description of problem:

Problem:

Regardless of what I try, I continuously am only able to get the:

[22:35:01][W][dht:120]: Requesting data from DHT failed!
[22:35:01][W][dht:058]: Invalid readings! Please check your wiring (pull-up resistor, pin number) and consider manually specifying the DHT model using the model option.

error. I have tried every fix I can find online for this to no avail.

Problem-relevant YAML-configuration entries:

esphome:
  name: totallyrandom
  platform: ESP8266
  board: esp01_1m

wifi:
  ssid: "[Redacted]"
  password: "[Redacted]"

logger:

api:

ota:

sensor:
  - platform: dht
    pin: 2
    temperature:
      name: "Living Room Temperature"
    humidity:
      name: "Living Room Humidity"
    update_interval: 10s

Logs (if applicable):

INFO Connecting to totallyrandom.local:6053 (xxx.xxx.xxx.229)
INFO Successfully connected to totallyrandom.local
[22:29:50][I][app:096]: esphome version 1.13.6 compiled on Nov  1 2019, 22:27:01
[22:29:50][C][wifi:372]: WiFi:
[22:29:50][C][wifi:254]:   SSID: '[Redacted]'
[22:29:50][C][wifi:255]:   IP Address: xx.xx.xx.229
[22:29:50][C][wifi:257]:   BSSID: xx:xx:xx:xx:xx:xx
[22:29:50][C][wifi:258]:   Hostname: 'totallyrandom'
[22:29:50][C][wifi:262]:   Signal strength: -71 dB ▂▄▆█
[22:29:50][C][wifi:263]:   Channel: 11
[22:29:50][C][wifi:264]:   Subnet: 255.255.255.0
[22:29:50][C][wifi:265]:   Gateway: xxx.xxx.xxx.231
[22:29:50][C][wifi:266]:   DNS1: xxx.xxx.xxx.231
[22:29:50][C][wifi:267]:   DNS2: 0.0.0.0
[22:29:50][C][logger:137]: Logger:
[22:29:50][C][logger:138]:   Level: DEBUG
[22:29:50][C][logger:139]:   Log Baud Rate: 115200
[22:29:50][C][logger:140]:   Hardware UART: UART0
[22:29:50][C][dht:017]: DHT:
[22:29:50][C][dht:018]:   Pin: GPIO2 (Mode: INPUT)
[22:29:50][C][dht:020]:   Auto-detected model: DHT22
[22:29:50][C][dht:027]:   Update Interval: 10.0s
[22:29:50][C][dht:029]:   Temperature 'Living Room Temperature'
[22:29:50][C][dht:029]:     Unit of Measurement: '°C'
[22:29:50][C][dht:029]:     Accuracy Decimals: 1
[22:29:50][C][dht:029]:     Icon: 'mdi:thermometer'
[22:29:50][C][dht:030]:   Humidity 'Living Room Humidity'
[22:29:50][C][dht:030]:     Unit of Measurement: '%'
[22:29:50][C][dht:030]:     Accuracy Decimals: 0
[22:29:50][C][dht:030]:     Icon: 'mdi:water-percent'
[22:29:50][C][ota:029]: Over-The-Air Updates:
[22:29:50][C][ota:030]:   Address: totallyrandom.local:8266
[22:29:50][C][api:103]: API Server:
[22:29:50][C][api:104]:   Address: totallyrandom.local:6053
[22:29:51][W][dht:120]: Requesting data from DHT failed!
[22:29:51][W][dht:058]: Invalid readings! Please check your wiring (pull-up resistor, pin number) and consider manually specifying the DHT model using the model option.
[22:30:01][W][dht:120]: Requesting data from DHT failed!
[22:30:01][W][dht:058]: Invalid readings! Please check your wiring (pull-up resistor, pin number) and consider manually specifying the DHT model using the model option.
[22:30:11][W][dht:120]: Requesting data from DHT failed!
[22:30:11][W][dht:058]: Invalid readings! Please check your wiring (pull-up resistor, pin number) and consider manually specifying the DHT model using the model option.
[22:30:14][D][api:573]: Client 'Home Assistant 0.99.3 (10.11.12.11)' connected successfully!
[22:30:21][W][dht:120]: Requesting data from DHT failed!
[22:30:21][W][dht:058]: Invalid readings! Please check your wiring (pull-up resistor, pin number) and consider manually specifying the DHT model using the model option.
[22:30:31][W][dht:120]: Requesting data from DHT failed!

Additional information and things you've tried:

Hardware Link:

The exact sensor board and ESP I have been using are linked here: https://www.ebay.com/itm/AM2302-DHT22-ESP8266-ESP-01-01S-Temperature-Humidity-Sensor-Wifi-Wireless-Module/192879041070?ssPageName=STRK%3AMEBIDX%3AIT&var=493026967410&_trksid=p2060353.m2749.l2649

Similar Cases:

Seems oddly similar to the second person listed here, however it doesn't seem they ever found a solution. If so, they didn't post it: https://community.home-assistant.io/t/debug-esphome-sensors/134688

Attempted Resolutions:

The most interesting part of it all though, is that after I upload the following bog-standard Arduino code to the EXACT same device, it spits out the temperature and humidity levels to the console window without a single problem in the world. The only issue seems to be when it's running the code compiled up for it by ESPHome.

Arduino code that seems to work fine, printing to the console.

// Example testing sketch for various DHT humidity/temperature sensors written by ladyada
// REQUIRES the following Arduino libraries:
// - DHT Sensor Library: https://github.com/adafruit/DHT-sensor-library
// - Adafruit Unified Sensor Lib: https://github.com/adafruit/Adafruit_Sensor

#include "DHT.h"

#define DHTPIN 2     // Digital pin connected to the DHT sensor
// Feather HUZZAH ESP8266 note: use pins 3, 4, 5, 12, 13 or 14 --
// Pin 15 can work but DHT must be disconnected during program upload.

// Uncomment whatever type you're using!
//#define DHTTYPE DHT11   // DHT 11
#define DHTTYPE DHT22   // DHT 22  (AM2302), AM2321
//#define DHTTYPE DHT21   // DHT 21 (AM2301)

// Connect pin 1 (on the left) of the sensor to +5V
// NOTE: If using a board with 3.3V logic like an Arduino Due connect pin 1
// to 3.3V instead of 5V!
// Connect pin 2 of the sensor to whatever your DHTPIN is
// Connect pin 4 (on the right) of the sensor to GROUND
// Connect a 10K resistor from pin 2 (data) to pin 1 (power) of the sensor

// Initialize DHT sensor.
// Note that older versions of this library took an optional third parameter to
// tweak the timings for faster processors.  This parameter is no longer needed
// as the current DHT reading algorithm adjusts itself to work on faster procs.
DHT dht(DHTPIN, DHTTYPE);

void setup() {
  Serial.begin(9600);
  Serial.println(F("DHTxx test!"));

  dht.begin();
}

void loop() {
  // Wait a few seconds between measurements.
  delay(2000);

  // Reading temperature or humidity takes about 250 milliseconds!
  // Sensor readings may also be up to 2 seconds 'old' (its a very slow sensor)
  float h = dht.readHumidity();
  // Read temperature as Celsius (the default)
  float t = dht.readTemperature();
  // Read temperature as Fahrenheit (isFahrenheit = true)
  float f = dht.readTemperature(true);

  // Check if any reads failed and exit early (to try again).
  if (isnan(h) || isnan(t) || isnan(f)) {
    Serial.println(F("Failed to read from DHT sensor!"));
    return;
  }

  // Compute heat index in Fahrenheit (the default)
  float hif = dht.computeHeatIndex(f, h);
  // Compute heat index in Celsius (isFahreheit = false)
  float hic = dht.computeHeatIndex(t, h, false);

  Serial.print(F("Humidity: "));
  Serial.print(h);
  Serial.print(F("%  Temperature: "));
  Serial.print(t);
  Serial.print(F("°C "));
  Serial.print(f);
  Serial.print(F("°F  Heat index: "));
  Serial.print(hic);
  Serial.print(F("°C "));
  Serial.print(hif);
  Serial.println(F("°F"));
}

Apologies for the information overload, I figure if there's any chance of solving this, it's with all the info I can provide, I'm happy to test anything out that you guys suggest, post any code, anything. If I sound desperate, it's cause I am. I've been at this for what feels like forever.

Thank you in advance, William

nbergont commented 4 years ago

I have notice that DHT22 sensor need power reboot (remove power supply) after flashing or OTA for working properly.

grischard commented 4 years ago

I'm seeing the same behaviour even after a power reboot.

grischard commented 4 years ago

Might be related to #239

WillT93 commented 4 years ago

I have notice that DHT22 sensor need power reboot (remove power supply) after flashing or OTA for working properly.

Hi, thanks for the suggestion unfortunately in this case that doesn't seem to be helping much :/ have power cycled on several occasions while switching between different power supplies to no avail.

WillT93 commented 4 years ago

I'm seeing the same behaviour even after a power reboot.

Interesting to see someone else having the same issue, to verify we are in the same boat, have you tried uploading a simple Arduino sketch to the device which reads from the DHT and prints the value to serial? That's what I did to verify that it wasn't an issue with the board or the wiring. Can send you the sketch if you like.

grischard commented 4 years ago

I haven't - the Sonoff is near the ceiling and I don't have the hardware to flash it over serial here.

CheongKoo commented 4 years ago

Hello, My DHT sensor works. I found that I had to specify the model of the sensor in order for it to work. See below. Hopefully this will help.

sensor:
  platform: dht
  pin: D4
  model: AM2302
  temperature:
     name: "Study temp"
  humidity:
     name: "Study humidity"
  update_interval: 20s
WillT93 commented 4 years ago

Hello, My DHT sensor works. I found that I had to specify the model of the sensor in order for it to work. See below. Hopefully this will help.

sensor:
  platform: dht
  pin: D4
  model: AM2302
  temperature:
     name: "Study temp"
  humidity:
     name: "Study humidity"
  update_interval: 20s

CheongKoo, Thanks for your reply and I'm glad your having success, unfortunately I've already tried the solution that worked for you (per the attempted resolutions section of my initial post) with no luck.

Nonetheless, I've since moved to my own custom Arduino code with reporting of temperatures using MQTT and have been running with complete success with that for the past few months so I'm happy with this matter being closed. If anyone else has the same issue and wants to give my solution a go, I'm happy to share the code.

Thanks all, William