DHT22 shield on Wemos D1 mini not providing values after deep sleep

I have a demos D1 mini with a DHT22 shield which i plan to use for measuring temperature and humidity in my bathroom, since this sensor will be running on a battery i want it to go into deep sleep between sending values.

Initially i had issues in getting the Demos D1 to work with the shield, after reading this blog post i changed the DHT22 shield to use pin D2 instead of D4. Now the Demos D1 could read the sensor ok.

Now i headed over to ESPhome to set up the sensor and to have it sending values over mqtt and then going into deep sleep for 60 seconds using the yaml code below. To have the Demos D1 mini to wake up i also had to connect the RST and D0 pins.

esphome:
  name: old_bath_1
  platform: ESP8266
  board: d1_mini

wifi:
  ssid: <removed>
  password: <removed>
  manual_ip:
    static_ip: 192.168.1.250
    gateway: 192.168.1.1
    subnet: 255.255.255.0

# Enable logging
logger:

mqtt:
  broker: '192.168.1.187'
  username: <removed>
  password: <removed>
  discovery: True
  discovery_retain: False
  birth_message:
  will_message:

sensor:
  - platform: dht
    pin: D2
    model: DHT22
    temperature:
      name: "old_bath_temp"
    humidity:
      name: "old_bath_hum"
    update_interval: 6s
    
deep_sleep:
  run_duration: 10s
  sleep_duration: 60s

After compiling and uploading the code to the Wemos D1 it transmits the values and goes into deep sleep but when waking up after 60 seconds it cannot read the DHT22 anymore. Disconnecting the Wemos D1 from the USB cable, removing the DHT22 shield from the Wemos D1, putting the DHT shield back on the Demos D1 and reconnecting the USB cable makes it work again, once. After waking up from deep sleep it cannot read the DHT22 any longer.

There is a description of a similar case in this forum which suggests setting DHTPIN to output and LOW before going into deep sleep, example code below.

pinMode(DHTPIN, OUTPUT);
digitalWrite(DHTPIN, LOW);
ESP.deepSleep(0.1 * 60000000); // Send to sleep for 35 minutes

I dont know how to achieve this in ESPhome, maybe someone can guide?

This is the log output i receive from ESPhome (yes, i have added a voltage and wifi signal strength sensor as well to verify that i get some measurements)

[17:22:41][D][dht:048]: Got Temperature=24.9°C Humidity=55.0%
[17:22:41][D][sensor:092]: 'old_bath_temp': Sending state 24.90000 °C with 1 decimals of accuracy
[17:22:41][D][sensor:092]: 'old_bath_hum': Sending state 55.00000 % with 0 decimals of accuracy
[17:22:42][D][adc:056]: 'voltage': Got voltage=2.93V
[17:22:42][D][sensor:092]: 'voltage': Sending state 2.92969 V with 2 decimals of accuracy
[17:22:43][D][sensor:092]: 'wifi_signal': Sending state -46.00000 dB with 0 decimals of accuracy
[17:22:47][D][dht:048]: Got Temperature=25.0°C Humidity=52.2%
[17:22:47][D][sensor:092]: 'old_bath_temp': Sending state 25.00000 °C with 1 decimals of accuracy
[17:22:47][D][sensor:092]: 'old_bath_hum': Sending state 52.20000 % with 0 decimals of accuracy
[17:22:48][I][deep_sleep:067]: Beginning Deep Sleep
[17:22:48][W][wifi_esp8266:354]: Event: Disconnected ssid='TN_24GHz_777A3C' bssid=A4:91:B1:77:7A:3C reason='Association Leave'
[17:22:48][W][wifi:089]: WiFi Connection lost... Reconnecting...
[17:22:48][W][wifi:431]: Restarting WiFi adapter...
[17:22:58]rll��|�l�|�l�b|����{�#�#��og�dno���cp��l{d;lp�o��l��cn�|l��c��g'�$��l �\033g'$`a's���gc�das��oc�al�bcc���d`��g�[I][logger:116]: Log initialized
[17:22:58][I][app:028]: Running through setup()...
[17:22:58][C][dht:011]: Setting up DHT...
[17:22:58][C][adc:018]: Setting up ADC 'voltage'...
[17:22:58][C][wifi:029]: Setting up WiFi...
[17:22:58][D][wifi:272]: Starting scan...
[17:22:58][D][adc:056]: 'voltage': Got voltage=2.93V
[17:22:58][D][sensor:092]: 'voltage': Sending state 2.92773 V with 2 decimals of accuracy
[17:23:02][D][adc:056]: 'voltage': Got voltage=2.93V
[17:23:02][D][sensor:092]: 'voltage': Sending state 2.93066 V with 2 decimals of accuracy
[17:23:03][E][dht:184]: DHT reports invalid data. Is the update interval too high or the sensor damaged?
[17:23:03][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.
[17:23:04][D][wifi:287]: Found networks:
[17:23:04][I][wifi:322]: - 'TN_24GHz_777A3C' (A4:91:B1:77:7A:3C) [redacted]▂▄▆█
[17:23:04][D][wifi:323]:     Channel: 11
[17:23:04][D][wifi:324]:     RSSI: -40 dB
[17:23:04][D][wifi:326]: - 'DontEvenTry'[redacted] (00:1F:33:34:C9:D6) [redacted]▂▄▆█
[17:23:04][I][wifi:164]: WiFi Connecting to 'TN_24GHz_777A3C'...
[17:23:05][I][wifi:380]: WiFi connected!
[17:23:05][C][wifi:254]:   SSID: 'TN_24GHz_777A3C'[redacted]
[17:23:05][C][wifi:255]:   IP Address: 192.168.1.250
[17:23:05][C][wifi:257]:   BSSID: A4:91:B1:77:7A:3C[redacted]
[17:23:05][C][wifi:258]:   Hostname: 'old_bath_1'
[17:23:05][C][wifi:262]:   Signal strength: -46 dB ▂▄▆█
[17:23:05][C][wifi:263]:   Channel: 11
[17:23:05][C][wifi:264]:   Subnet: 255.255.255.0
[17:23:05][C][wifi:265]:   Gateway: 192.168.1.1
[17:23:05][C][wifi:266]:   DNS1: 0.0.0.0
[17:23:05][C][wifi:267]:   DNS2: 0.0.0.0
[17:23:05][C][mqtt:025]: Setting up MQTT...
[17:23:05][I][mqtt:162]: Connecting to MQTT...
[17:23:05][I][mqtt:202]: MQTT Connected!
[17:23:05][C][deep_sleep:013]: Setting up Deep Sleep...
[17:23:05][I][app:060]: setup() finished successfully!
[17:23:05][D][sensor:092]: 'wifi_signal': Sending state -50.00000 dB with 0 decimals of accuracy
[17:23:05][I][app:096]: esphome version 1.13.6 compiled on Oct 23 2019, 14:36:37
[17:23:05][C][wifi:372]: WiFi:
[17:23:05][C][wifi:254]:   SSID: 'TN_24GHz_777A3C'[redacted]
[17:23:05][C][wifi:255]:   IP Address: 192.168.1.250
[17:23:05][C][wifi:257]:   BSSID: A4:91:B1:77:7A:3C[redacted]
[17:23:05][C][wifi:258]:   Hostname: 'old_bath_1'
[17:23:05][C][wifi:262]:   Signal strength: -50 dB ▂▄▆█
[17:23:05][C][wifi:263]:   Channel: 11
[17:23:05][C][wifi:264]:   Subnet: 255.255.255.0
[17:23:05][C][wifi:265]:   Gateway: 192.168.1.1
[17:23:05][C][wifi:266]:   DNS1: 0.0.0.0
[17:23:06][C][wifi:267]:   DNS2: 0.0.0.0
[17:23:06][C][logger:137]: Logger:
[17:23:06][C][logger:138]:   Level: DEBUG
[17:23:06][C][logger:139]:   Log Baud Rate: 115200
[17:23:06][C][logger:140]:   Hardware UART: UART0
[17:23:06][C][dht:017]: DHT:
[17:23:06][C][dht:018]:   Pin: GPIO4 (Mode: INPUT)
[17:23:06][C][dht:020]:   Auto-detected model: DHT11
[17:23:06][C][dht:027]:   Update Interval: 6.0s
[17:23:06][C][dht:029]:   Temperature 'old_bath_temp'
[17:23:06][C][dht:029]:     Unit of Measurement: '°C'
[17:23:06][C][dht:029]:     Accuracy Decimals: 1
[17:23:06][C][dht:029]:     Icon: 'mdi:thermometer'
[17:23:06][C][dht:030]:   Humidity 'old_bath_hum'
[17:23:06][C][dht:030]:     Unit of Measurement: '%'
[17:23:06][C][dht:030]:     Accuracy Decimals: 0
[17:23:06][C][dht:030]:     Icon: 'mdi:water-percent'
[17:23:06][C][adc:026]: ADC Sensor 'voltage'
[17:23:06][C][adc:026]:   Unit of Measurement: 'V'
[17:23:06][C][adc:026]:   Accuracy Decimals: 2
[17:23:06][C][adc:026]:   Icon: 'mdi:flash'
[17:23:06][C][adc:029]:   Pin: VCC
[17:23:06][C][adc:051]:   Update Interval: 6.0s
[17:23:06][C][mqtt:051]: MQTT:
[17:23:06][C][mqtt:053]:   Server Address: 192.168.1.187:1883 (192.168.1.187)
[17:23:06][C][mqtt:054]:   Username: 'mosquito'[redacted]
[17:23:06][C][mqtt:055]:   Client ID: 'old_bath_1-84f3eb0c9e3b'[redacted]
[17:23:06][C][mqtt:057]:   Discovery prefix: 'homeassistant'
[17:23:06][C][mqtt:058]:   Discovery retain: NO
[17:23:06][C][mqtt:060]:   Topic Prefix: 'old_bath_1'
[17:23:06][C][mqtt:062]:   Log Topic: 'old_bath_1/debug'
[17:23:06][C][wifi_signal.sensor:009]: WiFi Signal 'wifi_signal'
[17:23:06][C][wifi_signal.sensor:009]:   Unit of Measurement: 'dB'
[17:23:06][C][wifi_signal.sensor:009]:   Accuracy Decimals: 0
[17:23:06][C][wifi_signal.sensor:009]:   Icon: 'mdi:wifi'
[17:23:06][C][mqtt.sensor:024]: MQTT Sensor 'voltage':
[17:23:06][C][mqtt.sensor:028]:   State Topic: 'old_bath_1/sensor/voltage/state'
[17:23:06][C][mqtt.sensor:024]: MQTT Sensor 'wifi_signal':
[17:23:06][C][mqtt.sensor:028]:   State Topic: 'old_bath_1/sensor/wifi_signal/state'
[17:23:06][C][mqtt.sensor:024]: MQTT Sensor 'old_bath_temp':
[17:23:06][C][mqtt.sensor:028]:   State Topic: 'old_bath_1/sensor/old_bath_temp/state'
[17:23:06][C][mqtt.sensor:024]: MQTT Sensor 'old_bath_hum':
[17:23:06][C][mqtt.sensor:028]:   State Topic: 'old_bath_1/sensor/old_bath_hum/state'
[17:23:06][C][deep_sleep:020]: Setting up Deep Sleep...
[17:23:06][C][deep_sleep:023]:   Sleep Duration: 10000 ms
[17:23:06][C][deep_sleep:026]:   Run Duration: 10000 ms
[17:23:08][D][adc:056]: 'voltage': Got voltage=2.93V
[17:23:08][D][sensor:092]: 'voltage': Sending state 2.93164 V with 2 decimals of accuracy
[17:23:09][E][dht:184]: DHT reports invalid data. Is the update interval too high or the sensor damaged?
[17:23:09][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.
[17:23:11][D][sensor:092]: 'wifi_signal': Sending state -47.00000 dB with 0 decimals of accuracy
[17:23:14][D][adc:056]: 'voltage': Got voltage=2.93V
[17:23:14][D][sensor:092]: 'voltage': Sending state 2.93164 V with 2 decimals of accuracy
[17:23:15][E][dht:184]: DHT reports invalid data. Is the update interval too high or the sensor damaged?
[17:23:15][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.
[17:23:15][I][deep_sleep:067]: Beginning Deep Sleep
[17:23:15][W][wifi_esp8266:354]: Event: Disconnected ssid='TN_24GHz_777A3C' bssid=A4:91:B1:77:7A:3C reason='Association Leave'
[17:23:15][W][wifi:089]: WiFi Connection lost... Reconnecting...
[17:23:15][W][wifi:431]: Restarting WiFi adapter...

I have also tried the following sketch, which works well:


#include "DHT.h"
#define DHTPIN D2
#define DHTTYPE DHT22
DHT dht(DHTPIN, DHTTYPE);

// sleep for this many seconds
const int sleepSeconds = 10;

float humidity, temperature, heatIndex;
char str_humidity[10], str_temperature[10], str_heatIndex[10];

void setup() {
  Serial.begin(9600);
  Serial.println("\n\nWake up");

  // Connect D0 to RST to wake up
  pinMode(D0, WAKEUP_PULLUP);

  Serial.println("Initialize DHT sensor");
  dht.begin();
  delay(2000);

  Serial.println("Read DHT sensor");
  float humidity = dht.readHumidity();
  float temperature = dht.readTemperature();
  if (isnan(humidity) || isnan(temperature)) {
    Serial.println("Failed to read from DHT sensor!");
  }
  float heatIndex = dht.computeHeatIndex(temperature, humidity, false);

  // Convert the floats to strings and round to 2 decimal places
  dtostrf(humidity, 1, 2, str_humidity);
  dtostrf(temperature, 1, 2, str_temperature);
  dtostrf(heatIndex, 1, 2, str_heatIndex);

  Serial.printf("Humidity:    %s %%\nTemperature: %s *C\nHeat index:  %s *C\n", str_humidity, str_temperature, str_heatIndex);
  Serial.printf("Sleep for %d seconds\n\n", sleepSeconds);

  // convert to microseconds
  ESP.deepSleep(sleepSeconds * 1000000);
}

void loop() {
}