forked from piettetech/PietteTech_DHT
-
Notifications
You must be signed in to change notification settings - Fork 9
/
PietteTech_DHT.h
executable file
·127 lines (110 loc) · 3.53 KB
/
PietteTech_DHT.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
/*
* FILE: PietteTech_DHT.h
* VERSION: 0.3
* PURPOSE: Spark Interrupt driven lib for DHT sensors
* LICENSE: GPL v3 (http://www.gnu.org/licenses/gpl.html)
*
* S Piette (Piette Technologies) scott.piette@gmail.com
* January 2014 Original Spark Port
* October 2014 Added support for DHT21/22 sensors
* Improved timing, moved FP math out of ISR
*
* Based on adaptation by niesteszeck (github/niesteszeck)
* Based on original DHT11 library (http://playgroudn.adruino.cc/Main/DHT11Lib)
*
*
* With this library connect the DHT sensor to the following pins
* D0, D1, D2, D3, D4, A0, A1, A3, A5, A6, A7
* http://docs.spark.io/firmware/#interrupts-attachinterrupt
*
*/
#ifndef __PIETTETECH_DHT_H__
#define __PIETTETECH_DHT_H__
#define DHT_DEBUG_TIMING // Enable this for edge->edge timing collection
#include <Arduino.h>
#include <stdlib.h>
#include <inttypes.h>
#define DHTLIB_VERSION "0.3"
// min valid timing
//#define DHTLIB_MIN_TIMING 60
//#define DHTLIB_MIN_TIMING 50
#define DHTLIB_MIN_TIMING 30
// max valid timing in us
//#define DHTLIB_MAX_TIMING 155
//#define DHTLIB_MAX_TIMING 165
#define DHTLIB_MAX_TIMING 185
#define DHTLIB_ONE_TIMING 110
//#define DHTLIB_ONE_TIMING 105
//#define DHTLIB_ONE_TIMING 109
//#define DHTLIB_ONE_TIMING 107
//#define DHTLIB_RESPONSE_MAX_TIMING 200
#define DHTLIB_RESPONSE_MAX_TIMING 215
// device types
#define DHT11 11
#define DHT21 21
#define AM2301 21
#define DHT22 22
#define AM2302 22
// state codes
#define DHTLIB_OK 0
#define DHTLIB_ACQUIRING 1
#define DHTLIB_ACQUIRED 2
#define DHTLIB_RESPONSE_OK 3
// error codes
#define DHTLIB_ERROR_CHECKSUM -1
#define DHTLIB_ERROR_ISR_TIMEOUT -2
#define DHTLIB_ERROR_RESPONSE_TIMEOUT -3
#define DHTLIB_ERROR_DATA_TIMEOUT -4
#define DHTLIB_ERROR_ACQUIRING -5
#define DHTLIB_ERROR_DELTA -6
#define DHTLIB_ERROR_NOTSTARTED -7
#define DHT_CHECK_STATE \
if(_state == STOPPED) \
return _status; \
else if(_state != ACQUIRED) \
return DHTLIB_ERROR_ACQUIRING; \
if(_convert) convert();
class PietteTech_DHT
{
public:
PietteTech_DHT(uint8_t sigPin, uint8_t dht_type, void (*isrCallback_wrapper)());
void begin(uint8_t sigPin, uint8_t dht_type, void (*isrCallback_wrapper)());
void reset();
void isrCallback();
int acquire();
int acquireAndWait(uint32_t);
float getCelsius();
float getFahrenheit();
float getKelvin();
double getDewPoint();
double getDewPointSlow();
float getHumidity();
bool acquiring();
int getStatus();
float readTemperature();
float readHumidity();
#if defined(DHT_DEBUG_TIMING)
volatile uint8_t _edges[41];
#endif
private:
void (*isrCallback_wrapper)(void);
void convert();
enum states{RESPONSE=0,DATA=1,ACQUIRED=2,STOPPED=3,ACQUIRING=4};
volatile states _state;
volatile int _status;
volatile uint8_t _bits[5];
volatile uint8_t _cnt;
volatile uint8_t _idx;
volatile unsigned long _us;
volatile bool _convert;
#if defined(DHT_DEBUG_TIMING)
volatile uint8_t *_e;
#endif
int _sigPin;
int _type;
unsigned long _lastreadtime;
bool _firstreading;
float _hum;
float _temp;
};
#endif