AskSin++
Dht.h
1 //- -----------------------------------------------------------------------------------------------------------------------
2 // AskSin++
3 // 2018-04-03 papa Creative Commons - http://creativecommons.org/licenses/by-nc-sa/3.0/de/
4 //- -----------------------------------------------------------------------------------------------------------------------
5 
6 #ifndef __SENSORS_DHT11_h__
7 #define __SENSORS_DHT11_h__
8 
9 #include <Sensors.h>
10 #include <DHT.h>
11 
12 namespace as {
13 
14 // https://github.com/adafruit/DHT-sensor-library
15 template <int DATAPIN,int TYPE=DHT11,uint8_t MAXMEASURE=1>
16 class Dht : public Temperature, public Humidity {
17  DHT _dht;
18 public:
19  Dht () : _dht(DATAPIN,TYPE) {}
20 
21  void init () {
22  _dht.begin();
23  _present = true;
24  }
25  bool measure (__attribute__((unused)) bool async=false) {
26  bool success = false;
27  if( present() == true ) {
28  uint8_t measure=MAXMEASURE;
29  while( success == false && measure > 0 ) {
30  --measure;
31  float t = _dht.readTemperature(false,true);
32  float h = _dht.readHumidity();
33  if( isnan(t) == false && isnan(h) == false ) {
34  _temperature = t * 10;
35  _humidity = h;
36  success = true;
37  }
38  else if( measure > 0 ) {
39  _delay_ms(500);
40  }
41  }
42  }
43  return success;
44  }
45 };
46 
47 }
48 
49 #endif
as::Humidity
Definition: Sensors.h:39
as::Temperature
Definition: Sensors.h:30
as::Dht
Definition: Dht.h:16