RobTillaart / DHTNew

Arduino library for DHT11 and DHT22 with automatic sensor recognition
MIT License
96 stars 15 forks source link

DHT22 registers as type 70 in 0.4.14 #81

Closed simonbogh closed 1 year ago

simonbogh commented 1 year ago

DHT22 registers as type 70 in 0.4.14. Works as intended in 0.4.13 where it registers as type 22.

RobTillaart commented 1 year ago

Thanks for this issue, I will look into it asap.

What processor do you use? does the DHT22 give correct readings?

simonbogh commented 1 year ago

Thanks.

I use an esp-wroom-32, the board is a Wemos Lolin32 ESP32 OLED. The readings are correct.

RobTillaart commented 1 year ago

OK, so the problem is the automatic recognition of the type.

Ran DHT_endless.ino to see if I can recreate (on UNO)

DHT_endless.ino
LIBRARY VERSION: 0.4.14

0   1   SNR,    -999.0, -999.0, 37216,  0
1075    2   OK, 70.8,   19.3,   5016,   22
2081    3   WFR,    70.8,   19.3,   8,  22
3083    4   OK, 70.8,   19.3,   5008,   22
4089    5   WFR,    70.8,   19.3,   8,  22
5090    6   OK, 70.7,   19.3,   5012,   22
6096    7   WFR,    70.7,   19.3,   8,  22
7098    8   OK, 70.6,   19.3,   4960,   22
8104    9   WFR,    70.6,   19.3,   4,  22

So not able to recreate, looking further

simonbogh commented 1 year ago

Looks like it. If I use mySensor.setType(22); in 0.4.14 it gives correct readings and type.

RobTillaart commented 1 year ago

DHTNEW.cpp

int DHTNEW::read()
{
  ...
  //  AUTODETECT

  _type = 22;
  _wakeupDelay = DHTLIB_DHT_WAKEUP;
  int rv = _read();
  if (rv == DHTLIB_OK) return rv;

  _type = 11;
  _wakeupDelay = DHTLIB_DHT11_WAKEUP;
  rv = _read();
  if (rv == DHTLIB_OK) return rv;

  //  experimental 0.4.14
  _type = 70;
  _wakeupDelay = DHTLIB_SI7021_WAKEUP;
  rv = _read();
  if (rv == DHTLIB_OK) return rv;

  _type = 0; // retry next time
  return rv;
}

The code tries the DHT22 before the Si7021, so the detection order is OK too.

RobTillaart commented 1 year ago

@simonbogh

Can you in your code before the very first call to DHT.read() add a delay of 2 seconds?

Assumption is that the sensor needs time to wake up and that it just has not had enough time for it. Thus failing type == 22 , tries type == 11 - which fails too and finally succeeds in being a type 70.

Assumption is based on my output above that states 0 1 SNR, -999.0, -999.0, 37216, 0 SNR == Sensor not ready.

RobTillaart commented 1 year ago

Tried adding a delay(1000) in the auto detect an the SNR error disappeared in endless.ino.

So I assume this (first) error already existed in the 0.4.13 and before. Lets test.


And yes,

DHT_endless.ino
LIBRARY VERSION: 0.4.13

0   1   SNR,    -999.0, -999.0, 37160,  0
1074    2   OK, 70.3,   19.2,   5068,   22
2080    3   WFR,    70.3,   19.2,   8,  22
3082    4   OK, 70.4,   19.2,   4832,   22
4088    5   WFR,    70.4,   19.2,   8,  22

so the bug already existed, but is made more explicit by the addition of the Si7021 support.

I will create a 0.4.15 with a solution.

simonbogh commented 1 year ago

That does the trick. Even a small delay of 500 ms in void setup() works on my end 👍

RobTillaart commented 1 year ago

Created a develop branch, added a delay of max 1 second in first call

~line 123 dhtnew.cpp

added

  //  AUTODETECT
  //  make sure sensor had time to wake up.
  while (millis() < 1000);
RobTillaart commented 1 year ago

Going to merge the fix.

RobTillaart commented 1 year ago

I keep the wake up delay to the first 1000 millis() to cope with differences between individual sensors.

0.4.15 released

RobTillaart commented 1 year ago

@simonbogh Thanks!