ASensor
 All Classes Functions Pages
DHTTemperatureSensor.h
1 #pragma once
2 /*
3  ASensor - Sensor library. Can be used standalone or in conjunction with AWind library
4  Copyright (C)2015 Andrei Degtiarev. All right reserved
5 
6  You can always find the latest version of the library at
7  https://github.com/AndreiDegtiarev/ASensor
8 
9  This library is free software; you can redistribute it and/or
10  modify it under the terms of the MIT license.
11  Please see the included documents for further information.
12 */
14 class DHTTemperatureSensor : public ISensor
15 {
16 public:
17  enum SensorModel
18  {
19  AUTO_DETECT,
20  DHT11,
21  DHT22
22  };
23 private:
24 #ifndef DEMO_SENSORS
25  DHT *_dht;
26 #endif
27  //float _last_temperature;
28  float _last_humidity;
29  bool _isOK;
30  int _pin;
31 public:
32  DHTTemperatureSensor(int port,SensorModel sensorModel=AUTO_DETECT)
33  {
34  _pin=port;
35 #ifndef DEMO_SENSORS
36  _dht=new DHT();
37  DHT::DHT_MODEL_t dhtModel=DHT::AUTO_DETECT;
38  if(sensorModel == DHT11)
39  dhtModel=DHT::DHT11;
40  else
41  dhtModel=DHT::DHT22;
42  _dht->setup(port,dhtModel);
43 #endif
44  _last_humidity=0;
45  _isOK=false;
46  }
47 
48 public:
49  virtual const __FlashStringHelper* Name()
50  {
51  return F("DHT Temperature");
52  }
53  virtual float LowMeasurementLimit()
54  {
55  return -50;
56  }
57  virtual float HighMeasurementLimit()
58  {
59  return 50;
60  }
61  virtual int Precission()
62  {
63  return 1;
64  }
65  float LastHumidity()
66  {
67  return _last_humidity;
68  }
69  bool IsOK()
70  {
71  return _isOK;
72  }
73  virtual bool Measure(float &data)
74  {
75  _isOK=false;
76 #ifdef DEMO_SENSORS
77  data =(float)rand()/RAND_MAX*5+20;
78  _last_humidity = (float)rand()/RAND_MAX*10+60;
79  _isOK=true;
80 #else
81  DHT::DHT_ERROR_t status= DHT::ERROR_NONE;
82  if(_dht!=NULL)
83  {
84  status=_dht->getStatus();
85  if(status == DHT::ERROR_NONE || status ==DHT::ERROR_CHECKSUM) //test for check sum does not work stably
86  {
87  data = _dht->getTemperature();
88  _last_humidity = _dht->getHumidity();
89  }
90  else
91  {
92  out<<F("DHT Status: ")<<_dht->getStatusString()<<endln;
93  _dht->setup(_pin,_dht->getModel());
94  //delay(_dht->getMinimumSamplingPeriod()*2);
95  }
96  }
97 #ifdef DEBUG_AWIND
98  out<<F("Status: ")<<_dht->getStatusString()<<F(" Tempr: ")<<data<<F(" Humidity: ")<<_last_humidity<<endln;
99 #endif
100  _isOK=status == DHT::ERROR_NONE && !isnan(data) && !isnan(_last_humidity);
101  //delay(_dht->getMinimumSamplingPeriod());
102 #endif
103  return _isOK;
104  }
105 };
Wrapper for DHT temperature and humidity sensor. To use this sensor https://github.com/markruys/arduino-DHT library has to be installed. Details to member functions see ISensor class documentation.
Definition: DHTTemperatureSensor.h:14