-->
Page 2 of 6

Re: No more DHT22 values after deep sleep

PostPosted: Fri Feb 26, 2016 1:12 am
by atideath
Hello. Did your problem fixed? I have this problem with my ESP8266,DHT22 with deepsleep and I can't fix it. Please tell me How to fix this bug. Thanks!

Re: No more DHT22 values after deep sleep

PostPosted: Fri Feb 26, 2016 7:29 am
by schufti
Hi,
I'm not using DHT22 but AM2321 with i2c wich is somehow related.
Maybe my sketch is of some help for using deep sleep ...
Code: Select all#include <SPI.h>
#include <Wire.h>

#define I2C_ADDR  0x5C
#define CMD_READ  0x03
#define REG_HUM   0x00
#define REG_MOD   0x08
#define REG_UID   0x0b
#define MAX_DAT   0x0f

int tmp = 0;
unsigned int devid = 0, hum = 0, ver = 0, mod = 0;
byte buf[MAX_DAT];

struct rst_info {
  uint32 reason;
  uint32 exccause;
  uint32 epc1;
  uint32 epc2;
  uint32 epc3;
  uint32 excvaddr;
  uint32 depc;
};

void setup() {
  rst_info *xyz;
  int c;

  xyz = ESP.getResetInfoPtr();
  c = (*xyz).reason;
  Serial.begin(74880);
  Serial.println();
  Serial.print("Start: ");
  Serial.println(c);

  Wire.begin(0, 2);
  Wire.setClock(800000);
  if (c == 5) {
     Serial.println("awake");
  }
  else {
    Serial.println("poweron");
  }
}

void loop() {
  if (readAC()) {
    Serial.print("Temp: ");
    Serial.print(tmp/10.0,1);
    Serial.print("\tHum: ");
    Serial.println(hum/10.0,1);
    delay(200);
  }
 
  if (readID()) {
    Serial.println(mod);
    Serial.println(ver);
    Serial.println(devid);
    delay(2000);
    }
  ESP.deepSleep(4800000ul, RF_DISABLED);
}

bool readRaw(byte what, byte len) {
  //wakeup
  Wire.beginTransmission(I2C_ADDR);
  delay(1);
  Wire.endTransmission();
  delay(1);
  //send read command
  Wire.beginTransmission(I2C_ADDR);
  Wire.write(byte(CMD_READ));
  Wire.write(what);
  Wire.write(len);
  Wire.endTransmission();
  // for safe
  delay(15);
  Wire.requestFrom(I2C_ADDR, 2 + len + 2); // COMMAND + REGCOUNT + DATA + CRCLSB + CRCMSB
  int i = 0;
  for (; i < 2 + len; ++i)
    buf[i] = Wire.read();

  unsigned short crc = 0;
  crc  = Wire.read();     //CRC LSB
  crc |= Wire.read() << 8;//CRC MSB

  if (crc != crc16(buf, i))
    return false;
  return true;
}

bool readID() {
  if (!readRaw(REG_MOD, 7))
    return false;
  mod = buf[2] << 8 | buf[3];
  ver = buf[4];
  devid  = buf[5] << 24 | buf[6] << 16 | buf[7] << 8 | buf[8];
  return true;
}

bool readAC() {
  if (!readRaw(REG_HUM, 4))
    return false;
  hum = buf[2] << 8 | buf[3];
  tmp = buf[4] << 8 | buf[5];
  return true;
}

unsigned short crc16(unsigned char *ptr, unsigned char len) {
  unsigned short crc = 0xFFFF;
  unsigned char  i   = 0;
  while (len--) {
    crc ^= *ptr++;
    for (i = 0 ; i < 8 ; i++) {
      if (crc & 0x01) {
        crc >>= 1;
        crc  ^= 0xA001;
      }
      else {
        crc >>= 1;
      }
    }
  }
  return crc;
};

Re: No more DHT22 values after deep sleep

PostPosted: Mon Feb 29, 2016 3:02 pm
by hiddenbit
Also with the hints here, I didn't get it working.

For your information, this is my circuit..

current state: first load of sensor data works, but after sleep and wakeup just "NAN".

Re: No more DHT22 values after deep sleep

PostPosted: Mon Feb 29, 2016 5:34 pm
by PuceBaboon
hiddenbit wrote:Also with the hints here, I didn't get it working.

For your information, this is my circuit..

current state: first load of sensor data works, but after sleep and wakeup just "NAN".



This seems to be a general problem with DHT11/22 sensors. The solution which works well for me is
simply to loop until a valid sensor readout is obtained, or a loop limit is reached:-

Code: Select all/*
 * Try to get a valid reading from the DHT11 sensor
 * (not always easy).
 */
void getSensorData() {
    int retry_limit = 50;       // Limit read attempts.

    temperature = dht.readTemperature();
    humidity = dht.readHumidity();
    while ((isnan(temperature) || isnan(humidity))
           || (temperature == 0 && humidity == 0)) {
        if (--retry_limit < 1) {
            temperature = -1;
            humidity = -1;
            Serial.println(F("No DHT sensor detected."));
            return;
        }
#ifdef DEBUG
        RFlash();
        Serial.print(F("!"));
#endif
        Ydelay(500);
        temperature = dht.readTemperature();
        humidity = dht.readHumidity();
    }
#ifdef DEBUG
    Serial.println("");
    RFlash();
#endif
}


Specifically set temperature and humidity to zero before calling getSensorData(). If
the values are both -1 on return then the looped read failed.
YDelay() is just a normal call to delay() with a yield() inserted before the actual delay().
RFlash() is just a very short flash of a red LED to show access to the sensor.