1
1
/*
2
2
* FILE: DHT_example.ino
3
- * VERSION: 0.2
3
+ * VERSION: 0.3
4
4
* PURPOSE: Example that uses DHT library with two sensors
5
5
* LICENSE: GPL v3 (http://www.gnu.org/licenses/gpl.html)
6
6
*
7
- * Calls acquire on one sensor and monitors the results for long term
8
- * analysis. It uses DHT.acquire and DHT.acquiring
7
+ * Example that start acquisition of DHT sensor and allows the
8
+ * loop to continue until the acquisition has completed
9
+ * It uses DHT.acquire and DHT.acquiring
10
+ *
11
+ * Change DHT_SAMPLE_TIME to vary the frequency of samples
9
12
*
10
13
* Scott Piette (Piette Technologies) scott.piette@gmail.com
11
14
* January 2014 Original Spark Port
15
18
16
19
#include " PietteTech_DHT/PietteTech_DHT.h"
17
20
18
- #define DHTTYPE DHT22 // Sensor type DHT11/21/22/AM2301/AM2302
19
- #define DHTPIN 3 // Digital pin for communications
21
+ // system defines
22
+ #define DHTTYPE DHT22 // Sensor type DHT11/21/22/AM2301/AM2302
23
+ #define DHTPIN 3 // Digital pin for communications
24
+ #define DHT_SAMPLE_INTERVAL 2000 // Sample every two seconds
20
25
21
26
// declaration
22
27
void dht_wrapper (); // must be declared before the lib initialization
23
28
24
29
// Lib instantiate
25
30
PietteTech_DHT DHT (DHTPIN, DHTTYPE, dht_wrapper);
26
- int n; // counter
31
+
32
+ // globals
33
+ unsigned int DHTnextSampleTime; // Next time we want to start sample
34
+ bool bDHTstarted; // flag to indicate we started acquisition
35
+ int n; // counter
27
36
28
37
void setup ()
29
38
{
@@ -36,8 +45,11 @@ void setup()
36
45
Serial.print (" LIB version: " );
37
46
Serial.println (DHTLIB_VERSION);
38
47
Serial.println (" ---------------" );
48
+
49
+ DHTnextSampleTime = 0 ; // Start the first sample immediately
39
50
}
40
51
52
+
41
53
// This wrapper is in charge of calling
42
54
// mus be defined like this for the lib work
43
55
void dht_wrapper () {
@@ -46,64 +58,73 @@ void dht_wrapper() {
46
58
47
59
void loop ()
48
60
{
49
- Serial.print (" \n " );
50
- Serial.print (n);
51
- Serial.print (" : Retrieving information from sensor: " );
52
- Serial.print (" Read sensor: " );
53
- // delay(100);
54
-
55
- DHT.acquire ();
56
- while (DHT.acquiring ()) ;
57
-
58
- int result = DHT.getStatus ();
59
-
60
- switch (result) {
61
- case DHTLIB_OK:
62
- Serial.println (" OK" );
63
- break ;
64
- case DHTLIB_ERROR_CHECKSUM:
65
- Serial.println (" Error\n\r\t Checksum error" );
66
- break ;
67
- case DHTLIB_ERROR_ISR_TIMEOUT:
68
- Serial.println (" Error\n\r\t ISR time out error" );
69
- break ;
70
- case DHTLIB_ERROR_RESPONSE_TIMEOUT:
71
- Serial.println (" Error\n\r\t Response time out error" );
72
- break ;
73
- case DHTLIB_ERROR_DATA_TIMEOUT:
74
- Serial.println (" Error\n\r\t Data time out error" );
75
- break ;
76
- case DHTLIB_ERROR_ACQUIRING:
77
- Serial.println (" Error\n\r\t Acquiring" );
78
- break ;
79
- case DHTLIB_ERROR_DELTA:
80
- Serial.println (" Error\n\r\t Delta time to small" );
81
- break ;
82
- case DHTLIB_ERROR_NOTSTARTED:
83
- Serial.println (" Error\n\r\t Not started" );
84
- break ;
85
- default :
86
- Serial.println (" Unknown error" );
87
- break ;
61
+ // Check if we need to start the next sample
62
+ if (millis () > DHTnextSampleTime) {
63
+ if (!bDHTstarted) { // start the sample
64
+ Serial.print (" \n " );
65
+ Serial.print (n);
66
+ Serial.print (" : Retrieving information from sensor: " );
67
+ DHT.acquire ();
68
+ bDHTstarted = true ;
69
+ }
70
+
71
+ if (!DHT.acquiring ()) { // has sample completed?
72
+
73
+ // get DHT status
74
+ int result = DHT.getStatus ();
75
+
76
+ Serial.print (" Read sensor: " );
77
+ switch (result) {
78
+ case DHTLIB_OK:
79
+ Serial.println (" OK" );
80
+ break ;
81
+ case DHTLIB_ERROR_CHECKSUM:
82
+ Serial.println (" Error\n\r\t Checksum error" );
83
+ break ;
84
+ case DHTLIB_ERROR_ISR_TIMEOUT:
85
+ Serial.println (" Error\n\r\t ISR time out error" );
86
+ break ;
87
+ case DHTLIB_ERROR_RESPONSE_TIMEOUT:
88
+ Serial.println (" Error\n\r\t Response time out error" );
89
+ break ;
90
+ case DHTLIB_ERROR_DATA_TIMEOUT:
91
+ Serial.println (" Error\n\r\t Data time out error" );
92
+ break ;
93
+ case DHTLIB_ERROR_ACQUIRING:
94
+ Serial.println (" Error\n\r\t Acquiring" );
95
+ break ;
96
+ case DHTLIB_ERROR_DELTA:
97
+ Serial.println (" Error\n\r\t Delta time to small" );
98
+ break ;
99
+ case DHTLIB_ERROR_NOTSTARTED:
100
+ Serial.println (" Error\n\r\t Not started" );
101
+ break ;
102
+ default :
103
+ Serial.println (" Unknown error" );
104
+ break ;
105
+ }
106
+
107
+ Serial.print (" Humidity (%): " );
108
+ Serial.println (DHT.getHumidity (), 2 );
109
+
110
+ Serial.print (" Temperature (oC): " );
111
+ Serial.println (DHT.getCelsius (), 2 );
112
+
113
+ Serial.print (" Temperature (oF): " );
114
+ Serial.println (DHT.getFahrenheit (), 2 );
115
+
116
+ Serial.print (" Temperature (K): " );
117
+ Serial.println (DHT.getKelvin (), 2 );
118
+
119
+ Serial.print (" Dew Point (oC): " );
120
+ Serial.println (DHT.getDewPoint ());
121
+
122
+ Serial.print (" Dew Point Slow (oC): " );
123
+ Serial.println (DHT.getDewPointSlow ());
124
+
125
+ n++; // increment counter
126
+ bDHTstarted = false ; // reset the sample flag so we can take another
127
+ DHTnextSampleTime = millis () + DHT_SAMPLE_INTERVAL; // set the time for next sample
128
+ }
88
129
}
89
- Serial.print (" Humidity (%): " );
90
- Serial.println (DHT.getHumidity (), 2 );
91
-
92
- Serial.print (" Temperature (oC): " );
93
- Serial.println (DHT.getCelsius (), 2 );
94
-
95
- Serial.print (" Temperature (oF): " );
96
- Serial.println (DHT.getFahrenheit (), 2 );
97
-
98
- Serial.print (" Temperature (K): " );
99
- Serial.println (DHT.getKelvin (), 2 );
100
-
101
- Serial.print (" Dew Point (oC): " );
102
- Serial.println (DHT.getDewPoint ());
103
-
104
- Serial.print (" Dew Point Slow (oC): " );
105
- Serial.println (DHT.getDewPointSlow ());
106
-
107
- n++;
108
- delay (2500 );
109
130
}
0 commit comments