Adding DHT22/AM2302 sensor libraries (Arduino and PietteTech) but using Arduino for...
[iotcloud.git] / version2 / src / others / PietteTech_DHT / PietteTech_DHT.h
1 /*
2  * FILE:        PietteTech_DHT.h
3  * VERSION:     0.3
4  * PURPOSE:     Spark Interrupt driven lib for DHT sensors
5  * LICENSE:     GPL v3 (http://www.gnu.org/licenses/gpl.html)
6  *
7  * S Piette (Piette Technologies) scott.piette@gmail.com
8  *      January 2014        Original Spark Port
9  *      October 2014        Added support for DHT21/22 sensors
10  *                          Improved timing, moved FP math out of ISR
11  *
12  * Based on adaptation by niesteszeck (github/niesteszeck)
13  * Based on original DHT11 library (http://playgroudn.adruino.cc/Main/DHT11Lib)
14  *
15  *
16  * With this library connect the DHT sensor to the following pins
17  * D0, D1, D2, D3, D4, A0, A1, A3, A5, A6, A7
18  * http://docs.spark.io/firmware/#interrupts-attachinterrupt
19  *
20  */
21
22 #ifndef __PIETTETECH_DHT_H__
23 #define __PIETTETECH_DHT_H__
24
25 // There appears to be a overrun in memory on this class.  For now please leave DHT_DEBUG_TIMING enabled
26 #define DHT_DEBUG_TIMING        // Enable this for edge->edge timing collection
27
28 #include "application.h"
29
30 #define DHTLIB_VERSION "0.3"
31
32 // device types
33 #define DHT11                               11
34 #define DHT21                               21
35 #define AM2301                              21
36 #define DHT22                               22
37 #define AM2302                              22
38
39 // state codes
40 #define DHTLIB_OK                          0
41 #define DHTLIB_ACQUIRING                   1
42 #define DHTLIB_ACQUIRED                    2
43 #define DHTLIB_RESPONSE_OK                 3
44
45 // error codes
46 #define DHTLIB_ERROR_CHECKSUM              -1
47 #define DHTLIB_ERROR_ISR_TIMEOUT           -2
48 #define DHTLIB_ERROR_RESPONSE_TIMEOUT      -3
49 #define DHTLIB_ERROR_DATA_TIMEOUT          -4
50 #define DHTLIB_ERROR_ACQUIRING             -5
51 #define DHTLIB_ERROR_DELTA                 -6
52 #define DHTLIB_ERROR_NOTSTARTED            -7
53
54 #define DHT_CHECK_STATE                         \
55         if(_state == STOPPED)                   \
56             return _status;                     \
57         else if(_state != ACQUIRED)             \
58             return DHTLIB_ERROR_ACQUIRING;      \
59         if(_convert) convert();
60
61 class PietteTech_DHT
62 {
63 public:
64     PietteTech_DHT(uint8_t sigPin, uint8_t dht_type, void (*isrCallback_wrapper)());
65     void begin(uint8_t sigPin, uint8_t dht_type, void (*isrCallback_wrapper)());
66     void isrCallback();
67     int acquire();
68     int acquireAndWait();
69     float getCelsius();
70     float getFahrenheit();
71     float getKelvin();
72     double getDewPoint();
73     double getDewPointSlow();
74     float getHumidity();
75     bool acquiring();
76     int getStatus();
77     float readTemperature();
78     float readHumidity();
79 #if defined(DHT_DEBUG_TIMING)
80     volatile uint8_t _edges[41];
81 #endif
82
83 private:
84     void (*isrCallback_wrapper)(void);
85     void convert();
86
87     enum states{RESPONSE=0,DATA=1,ACQUIRED=2,STOPPED=3,ACQUIRING=4};
88     volatile states _state;
89     volatile int _status;
90     volatile uint8_t _bits[5];
91     volatile uint8_t _cnt;
92     volatile uint8_t _idx;
93     volatile unsigned long _us;
94     volatile bool _convert;
95 #if defined(DHT_DEBUG_TIMING)
96     volatile uint8_t *_e;
97 #endif
98     int _sigPin;
99     int _type;
100     unsigned long _lastreadtime;
101     bool _firstreading;
102     float _hum;
103     float _temp;
104 };
105 #endif