Skip to content

Commit 0657b80

Browse files
committedOct 22, 2014
Updated DHT_example
Updated DHT_example to remove while loop Increased version numbers
1 parent b5ab916 commit 0657b80

File tree

6 files changed

+91
-70
lines changed

6 files changed

+91
-70
lines changed
 

‎firmware/PietteTech_DHT.cpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
/*
22
* FILE: PietteTech_DHT.cpp
3-
* VERSION: 0.2
3+
* VERSION: 0.3
44
* PURPOSE: Spark Interrupt driven lib for DHT sensors
55
* LICENSE: GPL v3 (http://www.gnu.org/licenses/gpl.html)
66
*

‎firmware/PietteTech_DHT.h

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
/*
22
* FILE: PietteTech_DHT.h
3-
* VERSION: 0.2
3+
* VERSION: 0.3
44
* PURPOSE: Spark Interrupt driven lib for DHT sensors
55
* LICENSE: GPL v3 (http://www.gnu.org/licenses/gpl.html)
66
*

‎firmware/examples/DHT_2sensor.ino

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
/*
22
* FILE: DHT_2sensor.ino
3-
* VERSION: 0.2
3+
* VERSION: 0.3
44
* PURPOSE: Example that uses DHT library with two sensors
55
* LICENSE: GPL v3 (http://www.gnu.org/licenses/gpl.html)
66
*

‎firmware/examples/DHT_example.ino

+86-65
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,14 @@
11
/*
22
* FILE: DHT_example.ino
3-
* VERSION: 0.2
3+
* VERSION: 0.3
44
* PURPOSE: Example that uses DHT library with two sensors
55
* LICENSE: GPL v3 (http://www.gnu.org/licenses/gpl.html)
66
*
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
912
*
1013
* Scott Piette (Piette Technologies) scott.piette@gmail.com
1114
* January 2014 Original Spark Port
@@ -15,15 +18,21 @@
1518

1619
#include "PietteTech_DHT/PietteTech_DHT.h"
1720

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
2025

2126
//declaration
2227
void dht_wrapper(); // must be declared before the lib initialization
2328

2429
// Lib instantiate
2530
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
2736

2837
void setup()
2938
{
@@ -36,8 +45,11 @@ void setup()
3645
Serial.print("LIB version: ");
3746
Serial.println(DHTLIB_VERSION);
3847
Serial.println("---------------");
48+
49+
DHTnextSampleTime = 0; // Start the first sample immediately
3950
}
4051

52+
4153
// This wrapper is in charge of calling
4254
// mus be defined like this for the lib work
4355
void dht_wrapper() {
@@ -46,64 +58,73 @@ void dht_wrapper() {
4658

4759
void loop()
4860
{
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\tChecksum error");
66-
break;
67-
case DHTLIB_ERROR_ISR_TIMEOUT:
68-
Serial.println("Error\n\r\tISR time out error");
69-
break;
70-
case DHTLIB_ERROR_RESPONSE_TIMEOUT:
71-
Serial.println("Error\n\r\tResponse time out error");
72-
break;
73-
case DHTLIB_ERROR_DATA_TIMEOUT:
74-
Serial.println("Error\n\r\tData time out error");
75-
break;
76-
case DHTLIB_ERROR_ACQUIRING:
77-
Serial.println("Error\n\r\tAcquiring");
78-
break;
79-
case DHTLIB_ERROR_DELTA:
80-
Serial.println("Error\n\r\tDelta time to small");
81-
break;
82-
case DHTLIB_ERROR_NOTSTARTED:
83-
Serial.println("Error\n\r\tNot 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\tChecksum error");
83+
break;
84+
case DHTLIB_ERROR_ISR_TIMEOUT:
85+
Serial.println("Error\n\r\tISR time out error");
86+
break;
87+
case DHTLIB_ERROR_RESPONSE_TIMEOUT:
88+
Serial.println("Error\n\r\tResponse time out error");
89+
break;
90+
case DHTLIB_ERROR_DATA_TIMEOUT:
91+
Serial.println("Error\n\r\tData time out error");
92+
break;
93+
case DHTLIB_ERROR_ACQUIRING:
94+
Serial.println("Error\n\r\tAcquiring");
95+
break;
96+
case DHTLIB_ERROR_DELTA:
97+
Serial.println("Error\n\r\tDelta time to small");
98+
break;
99+
case DHTLIB_ERROR_NOTSTARTED:
100+
Serial.println("Error\n\r\tNot 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+
}
88129
}
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);
109130
}

‎firmware/examples/DHT_simple.ino

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
/*
22
* FILE: DHT_simple.ino
3-
* VERSION: 0.2
3+
* VERSION: 0.3
44
* PURPOSE: Example that uses DHT library with two sensors
55
* LICENSE: GPL v3 (http://www.gnu.org/licenses/gpl.html)
66
*

‎spark.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "PietteTech_DHT",
3-
"version": "0.0.2",
3+
"version": "0.0.3",
44
"author": "Scott Piette scott.piette@gmail.com",
55
"license": "GPL v3 (http://www.gnu.org/licenses/gpl.html)",
66
"description": "Interrupt driven DHT11/21/22 Sensor library"

0 commit comments

Comments
 (0)
Please sign in to comment.