2 * FILE: DHT_example.ino
4 * PURPOSE: Example that uses DHT library with two sensors
5 * LICENSE: GPL v3 (http://www.gnu.org/licenses/gpl.html)
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
11 * Change DHT_SAMPLE_TIME to vary the frequency of samples
13 * Scott Piette (Piette Technologies) scott.piette@gmail.com
14 * January 2014 Original Spark Port
15 * October 2014 Added support for DHT21/22 sensors
16 * Improved timing, moved FP math out of ISR
19 #include "PietteTech_DHT.h"
22 #define DHTTYPE DHT22 // Sensor type DHT11/21/22/AM2301/AM2302
23 #define DHTPIN D5 // Digital pin for communications
24 #define DHT_SAMPLE_INTERVAL 2000 // Sample every two seconds
27 void dht_wrapper(); // must be declared before the lib initialization
30 PietteTech_DHT DHT(DHTPIN, DHTTYPE, dht_wrapper);
33 unsigned int DHTnextSampleTime; // Next time we want to start sample
34 bool bDHTstarted; // flag to indicate we started acquisition
40 while (!Serial.available()) {
41 Serial.println("Press any key to start.");
44 Serial.println("DHT Example program using DHT.acquire and DHT.aquiring");
45 Serial.print("LIB version: ");
46 Serial.println(DHTLIB_VERSION);
47 Serial.println("---------------");
49 DHTnextSampleTime = 0; // Start the first sample immediately
53 // This wrapper is in charge of calling
54 // mus be defined like this for the lib work
61 // Check if we need to start the next sample
62 if (millis() > DHTnextSampleTime) {
63 if (!bDHTstarted) { // start the sample
66 Serial.print(": Retrieving information from sensor: ");
71 int result = DHT.getStatus();
72 if (!DHT.acquiring()) { // has sample completed?
75 int result = DHT.getStatus();
77 Serial.print("Read sensor: ");
82 case DHTLIB_ERROR_CHECKSUM:
83 Serial.println("Error\n\r\tChecksum error");
85 case DHTLIB_ERROR_ISR_TIMEOUT:
86 Serial.println("Error\n\r\tISR time out error");
88 case DHTLIB_ERROR_RESPONSE_TIMEOUT:
89 Serial.println("Error\n\r\tResponse time out error");
91 case DHTLIB_ERROR_DATA_TIMEOUT:
92 Serial.println("Error\n\r\tData time out error");
94 case DHTLIB_ERROR_ACQUIRING:
95 Serial.println("Error\n\r\tAcquiring");
97 case DHTLIB_ERROR_DELTA:
98 Serial.println("Error\n\r\tDelta time to small");
100 case DHTLIB_ERROR_NOTSTARTED:
101 Serial.println("Error\n\r\tNot started");
104 Serial.println("Unknown error");
108 Serial.print("Humidity (%): ");
109 Serial.println(DHT.getHumidity(), 2);
111 Serial.print("Temperature (oC): ");
112 Serial.println(DHT.getCelsius(), 2);
114 Serial.print("Temperature (oF): ");
115 Serial.println(DHT.getFahrenheit(), 2);
117 Serial.print("Temperature (K): ");
118 Serial.println(DHT.getKelvin(), 2);
120 Serial.print("Dew Point (oC): ");
121 Serial.println(DHT.getDewPoint());
123 Serial.print("Dew Point Slow (oC): ");
124 Serial.println(DHT.getDewPointSlow());
126 n++; // increment counter
127 bDHTstarted = false; // reset the sample flag so we can take another
128 DHTnextSampleTime = millis() + DHT_SAMPLE_INTERVAL; // set the time for next sample