Adding DHT22/AM2302 sensor libraries (Arduino and PietteTech) but using Arduino for...
[iotcloud.git] / version2 / src / others / PietteTech_DHT / DHT22.ino
1 /*
2  * FILE:        DHT_example.ino
3  * VERSION:     0.3
4  * PURPOSE:     Example that uses DHT library with two sensors
5  * LICENSE:     GPL v3 (http://www.gnu.org/licenses/gpl.html)
6  *
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
12  *
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
17  */
18  
19 #include "PietteTech_DHT.h"
20  
21 // system defines
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
25  
26 //declaration
27 void dht_wrapper(); // must be declared before the lib initialization
28  
29 // Lib instantiate
30 PietteTech_DHT DHT(DHTPIN, DHTTYPE, dht_wrapper);
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
36  
37 void setup()
38 {
39     Serial.begin(9600);
40     while (!Serial.available()) {
41         Serial.println("Press any key to start.");
42         delay (1000);
43     }
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("---------------");
48  
49     DHTnextSampleTime = 0;  // Start the first sample immediately
50 }
51  
52  
53 // This wrapper is in charge of calling
54 // mus be defined like this for the lib work
55 void dht_wrapper() {
56     DHT.isrCallback();
57 }
58  
59 void loop()
60 {
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     int result = DHT.getStatus();
72     if (!DHT.acquiring()) {     // has sample completed?
73  
74         // get DHT status
75         int result = DHT.getStatus();
76  
77         Serial.print("Read sensor: ");
78         switch (result) {
79         case DHTLIB_OK:
80             Serial.println("OK");
81             break;
82         case DHTLIB_ERROR_CHECKSUM:
83             Serial.println("Error\n\r\tChecksum error");
84             break;
85         case DHTLIB_ERROR_ISR_TIMEOUT:
86             Serial.println("Error\n\r\tISR time out error");
87             break;
88         case DHTLIB_ERROR_RESPONSE_TIMEOUT:
89             Serial.println("Error\n\r\tResponse time out error");
90             break;
91         case DHTLIB_ERROR_DATA_TIMEOUT:
92             Serial.println("Error\n\r\tData time out error");
93             break;
94         case DHTLIB_ERROR_ACQUIRING:
95             Serial.println("Error\n\r\tAcquiring");
96             break;
97         case DHTLIB_ERROR_DELTA:
98             Serial.println("Error\n\r\tDelta time to small");
99             break;
100         case DHTLIB_ERROR_NOTSTARTED:
101             Serial.println("Error\n\r\tNot started");
102             break;
103         default:
104             Serial.println("Unknown error");
105             break;
106         }
107  
108         Serial.print("Humidity (%): ");
109         Serial.println(DHT.getHumidity(), 2);
110  
111         Serial.print("Temperature (oC): ");
112         Serial.println(DHT.getCelsius(), 2);
113  
114         Serial.print("Temperature (oF): ");
115         Serial.println(DHT.getFahrenheit(), 2);
116  
117         Serial.print("Temperature (K): ");
118         Serial.println(DHT.getKelvin(), 2);
119  
120         Serial.print("Dew Point (oC): ");
121         Serial.println(DHT.getDewPoint());
122  
123         Serial.print("Dew Point Slow (oC): ");
124         Serial.println(DHT.getDewPointSlow());
125  
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
129     }
130     }
131 }