OMG - DHT22 and deepsleep

Hello,
I was looking through the documentation but haven’t been able to get to my answer.

I have 3 working OMG 0.9betas on Wemos D1 mini. One of them has an RF receiver and RF transmitter + DHT22 and the other two facilitate just DHT22. For those two that I use only for hum. and temp. I have noticed the temp. readings aren’t accurate. I discovered this is due to the way they are mounted on top of the wemos board.

As I have been researching the web I found out that people utilize deep sleep so the wemos board gets time to cool down between the readings. Someone even got to a conclusion minimum of 3 minutes is the least amount of deep sleep the wemos should get, to not affect the dht readings.

So as I have already searched through docs and couldn’t find this info I would like to ask if OMG utilizes deep sleep or is there any code rewriting necessary to get it working with it?

Thanks for the answer,
Regards, Jure

Hello,

Currently there is no deep sleep function as in most of the case the boards needs to stay on so as to listen for commands but as i m concerned about power consumption and quite interested on deep sleep functions we can work on that together.

Yes I thought that was the case. It is quite logical, I wouldn’t want my doorbell reciever to go to sleep :slight_smile: It would make the postman wait a long time for me to respond :smiley:

I’m not sure I would be of enough help as I’m not yet confident with my programing skills :slight_smile: But this is what I could gather in my research until now.

To enable deep sleep on esp8266 boards you need to connect the D0 pin to RST, which enables deep sleep.
Everytime D0 sends a LOW to RST, ESP8266 restarts, running the whole script until it hits this peace of code ESP.deepSleep(); in the brackets you define for how long it goes to deep sleep until it reboots again.

I’m not really savvy with programing yet, but the logic should work just fine:

void MeasureTempAndHum(){
 if defined DeepSleep {
  timedht = millis();
    static float persistedh;
    static float persistedt;
    float h = dht.readHumidity();
    // Read temperature as Celsius (the default)
    float t = dht.readTemperature(); 
    // Check if any reads failed and exit early (to try again).
    if (isnan(h) || isnan(t)) {
      trc(F("Failed to read from DHT sensor!"));
    }else{
      trc(F("Creating DHT buffer"));
      StaticJsonBuffer<JSON_MSG_BUFFER> jsonBuffer;
      JsonObject& DHTdata = jsonBuffer.createObject();
      if(h != persistedh || dht_always){
        DHTdata.set("hum", (float)h);
       }else{
        trc(F("Same hum don't send it"));
       }
      if(t != persistedt || dht_always){
        DHTdata.set("temp", (float)t);
      }else{
        trc(F("Same temp don't send it"));
      }
      if(DHTdata.size()>0) pub(DHTTOPIC,DHTdata);
    }
    persistedh = h;
    persistedt = t;
    ESP.deepsleep(DeepSleep)
  }
  else (millis() > (timedht + TimeBetweenReadingDHT)) {//retriving value of temperature and humidity of the box from DHT every xUL
    timedht = millis();
    static float persistedh;
    static float persistedt;
    float h = dht.readHumidity();
    // Read temperature as Celsius (the default)
    float t = dht.readTemperature(); 
    // Check if any reads failed and exit early (to try again).
    if (isnan(h) || isnan(t)) {
      trc(F("Failed to read from DHT sensor!"));
    }else{
      trc(F("Creating DHT buffer"));
      StaticJsonBuffer<JSON_MSG_BUFFER> jsonBuffer;
      JsonObject& DHTdata = jsonBuffer.createObject();
      if(h != persistedh || dht_always){
        DHTdata.set("hum", (float)h);
       }else{
        trc(F("Same hum don't send it"));
       }
      if(t != persistedt || dht_always){
        DHTdata.set("temp", (float)t);
      }else{
        trc(F("Same temp don't send it"));
      }
      if(DHTdata.size()>0) pub(DHTTOPIC,DHTdata);
    }
    persistedh = h;
    persistedt = t;
  }
 }
}
#endif

What do you think, would this work or did I hit in the dark?

The configDHT.h should than include a #define DeepSleep 30
and instead of 30 the right value of seconds for the DeepSleep to last (this one could be configurable)

In all this logic there is only one problem I see, it works great when you have just the DHT enabled, but would as you have said yourself, render RF reciever and all others pretty much useless.

Again a disclamer this is purely theoretical up here, haven’t tested any of it yet. Let me know if you think this could work or am I missing something?

Regards, Jure

Hello,

Thanks for the infos, I think deepsleep play the role of a timer, in this case we don’t need to use millis (our current non blocking timer). You have to replace the millis functions when this low power mode is used.
Maybe by using some ifdef / else macros.

Hi, yeah a valid point. Didn’t think of that…I’ll be trying to tweak the code a little bit more and try testing it next week.
I’ve ordered some more wemos and already have a bunch of unused dhts, so I’ll report back with what I find out.

Cheers :slight_smile:

1 Like