DHT22 sensor not showing

I have a ESP8266 IR gateway. I’ve added a DHT22 sensor and used the D1 pin for the data pin (like mentioned in the docs). However I don’t see any readings being publish on the mqtt server. I’ve attached the board to my pc to read the serial monitor. Also there nothing shows that the DHT22 is connected. Any ideas?

Hi @FerronN,

how did you define your environment for the IR ESP8266 gateway to include the DHT22?

@DigiH I didn’t. All sensor are not by default setup in all gateway firmwares?

No, as the connection to D1 could also be a BH1750 SCL, HTU21 SCL or several other sensors. So you need to specify which sensor you want to use with your set up, by also defining

${libraries.dht}

in the lib_deps

and

'-DZsensorDHT="DHT"'

in the build_flags of your IR environment to also use the DHT22 along with it.

Let us know how you get on and maybe show us how your final environment looks like.

Thank you!

I’ve added this build configuration for myself:

[env:nodemcuv2-ir-dht]
platform = ${com.esp8266_platform}
board = nodemcuv2
lib_deps =
  ${com-esp.lib_deps}
  ${libraries.wifimanager8266}
  ${libraries.irremoteesp}
  ${libraries.esp8266_mdns}
  ${libraries.dht}
build_flags =
  ${com-esp.build_flags}
  '-DZgatewayIR="IR"'
  '-DZsensorDHT="DHT"'
  '-DGateway_Name="OpenMQTTGateway_ESP8266_IR"'
board_build.flash_mode = dout

Only the sensor says it’s 9.9 degrees celcius and it should be around 19.0. Sometimes the sensor return -10 degrees celcius. Is this a known problem in some situation?

Not that I know of, but I am not using any connected DHTs myself, so cannot

Also note that the it only reads the temp/hum every 30 seconds, as defined in config_DHT.h.

Does it possibly need a pull up resistor for clear data?? Maybe someone with DHT experience can answer this better.

Do you have a 5-10k resistor lying around to try out? :wink:

Have a look at the wiring instructions on this page, and see if all your connections are the same - without the 5k resistor currently. If they are and you are still seeing the temperature variations then you might need to add a resistor as shown in the diagram.

Hope this helps.

I will take a look and yes I have a resistor, but I have this complete module:

Yes, this module looks like it already has a resistor on it - hmm

I had multiple of these modules, but another has the same result. So I think it has to do something with code/library. I will look if I can find something.

The library has had some updates since the OMG included version 1.4.2.; it’s now 1.4.4.
Although I cannot see any major changes, or changes which should be relevant for the problems you are seeing it might be worth you trying it out with the latest version, by changing line 122 in patformio.ini to

dht = DHT sensor library@1.4.4

I know I should have an old non-board DHT22 in one of my drawers somewhere - if I can find it I can also have a try with manually adding a 5k resistor to see what results I’m getting :wink:

Also going through the forum here there there seem to have been similar issues

so whatever the older “real” DHT22s and new DHT22s are (no board/board with resistor??) there might be some library adjustment needed for some.

@christian-0s - any hints at what timing changes in the library got you proper readings from the ‘new’ DHT22s?

Thanks

I just used the library with the adafruit example DHTTester that’s included with the library. This test sketch seem to work fine on a ESP32. My openmqttgateway is running on a ESP8266. Library 1.4.4 doesn’t make a difference.

// Example testing sketch for various DHT humidity/temperature sensors
// Written by ladyada, public domain

// 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 3 (on the right) of the sensor to GROUND (if your sensor has 3 pins)
// Connect pin 4 (on the right) of the sensor to GROUND and leave the pin 3 EMPTY (if your sensor has 4 pins)
// 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"));
}

Which version of ESP8266 do you have? Here a very similar setup to yours works fine with the user still adding an additional BH1750 light sensor - all with a NodeMCU v3.

While he needed to change the DHT_RECEIVER_GPIO to 14 (D5) to free D1 for the BH1750 this might also be worth trying out in your case - also as commented in config_DHT.h.

I have this one: DEBO JT ESP8266: Developer boards - NodeMCU ESP8266 Wi-Fi module at reichelt elektronik. I just tried PIN 14 (D15), but the reading is the same.

Looks like a v2 to me at first glance, of which I also have some lying around here. So if I find my old style DHT22 I’ll give it a try in the next few days.

One last idea - which pin do you use to supply power to the DHT22, the 3.3V, which should be fine spec wise, or the VIN, which could (on the NodeMCUv2??) have 5V - looking at the diode on your version I think it will, and you’re using the USB port for supplying power - and should also be fine for the DHT22?

Maybe swapping the power might give you different results - best to check for the 5V with a multimeter first - and slowly grasping at straws here :stuck_out_tongue_winking_eye:

I use the VIN pin. It had 4.6v according to my multimeter. Thank you for all the help so far!

Then maybe swap it around and try the 3.3V pin to supply power to the DHT22 :slight_smile: it’s input range should be from 3.0-5.5V

3.3v same result unfortunately :neutral_face:

While it’s always a good idea to use a dedicated regulated power pin to supply power to sensors - VIN only being for input on some models, on others also carrying around 5V from the USB supply - unfortunately it didn’t help here.

I am out of ideas what else it could be, but will keep you posted on my tests if and when I find my old style DHT22.

Or someone else might have further suggestions.

All the best.

1 Like

Hi again @FerronN,

having dug out my DHT22 - plain version not on a board with included resistor like yours - I tested it and found exactly the same issues you were having. Enabling DHT debugging it also showed that a lot of times there were checksum errors when reading from the DHT and when the checksum was fine the hex bytes corresponded with the erroneous temperature and humidity values :frowning:

Not knowing exactly where the issue lies I went back to a previous OpenMQTTGateway version, before changes were made on the OMG side, but also before substantial changes to the included DHT/unified sensor libraries.

Using OpenMQTTGateway 0.9.8 I got constant good readings from my DHT22.

Using the same older library versions from then in OMG 0.9.16 this is also working nicely for me, please change the library links in platformio.ini to:

dht = DHT sensor library@1.3.0
unifiedsensor = Adafruit Unified Sensor@1.0.2

Also an additional lib_deps needs to be added, in your case the environment should be

[env:nodemcuv2-ir-dht]
platform = ${com.esp8266_platform}
board = nodemcuv2
lib_deps =
  ${com-esp.lib_deps}
  ${libraries.wifimanager8266}
  ${libraries.irremoteesp}
  ${libraries.esp8266_mdns}
  ${libraries.unifiedsensor}
  ${libraries.dht}
build_flags =
  ${com-esp.build_flags}
  '-DZgatewayIR="IR"'
  '-DZsensorDHT="DHT"'
  '-DGateway_Name="OpenMQTTGateway_ESP8266_IR"'
board_build.flash_mode = dout

Please let us know if these older library versions fix your issues as well.

1 Like

The older library fixes the issue for me :slight_smile: I think it has something to do with OMG. I also tried my ESP32 with OMG and I got the same wrong readings (dht library 1.4.4 and unified sensor 1.1.6). Using the same libraries (and versions) and the sketch I referred to earlier I got the correct values.