Adding .ino files for IoTCloud library tests (periodic write and sleep); adding DHT...
[iotcloud.git] / version2 / src / others / DHT / examples / DHTtester / DHTtester.ino
1 // Example testing sketch for various DHT humidity/temperature sensors
2 // Written by ladyada, public domain
3
4 #include "DHT.h"
5
6 #define DHTPIN 2     // what pin we're connected to
7
8 // Uncomment whatever type you're using!
9 //#define DHTTYPE DHT11   // DHT 11 
10 #define DHTTYPE DHT22   // DHT 22  (AM2302)
11 //#define DHTTYPE DHT21   // DHT 21 (AM2301)
12
13 // Connect pin 1 (on the left) of the sensor to +5V
14 // NOTE: If using a board with 3.3V logic like an Arduino Due connect pin 1
15 // to 3.3V instead of 5V!
16 // Connect pin 2 of the sensor to whatever your DHTPIN is
17 // Connect pin 4 (on the right) of the sensor to GROUND
18 // Connect a 10K resistor from pin 2 (data) to pin 1 (power) of the sensor
19
20 // Initialize DHT sensor for normal 16mhz Arduino
21 DHT dht(DHTPIN, DHTTYPE);
22 // NOTE: For working with a faster chip, like an Arduino Due or Teensy, you
23 // might need to increase the threshold for cycle counts considered a 1 or 0.
24 // You can do this by passing a 3rd parameter for this threshold.  It's a bit
25 // of fiddling to find the right value, but in general the faster the CPU the
26 // higher the value.  The default for a 16mhz AVR is a value of 6.  For an
27 // Arduino Due that runs at 84mhz a value of 30 works.
28 // Example to initialize DHT sensor for Arduino Due:
29 //DHT dht(DHTPIN, DHTTYPE, 30);
30
31 void setup() {
32   Serial.begin(9600); 
33   Serial.println("DHTxx test!");
34  
35   dht.begin();
36 }
37
38 void loop() {
39   // Wait a few seconds between measurements.
40   delay(2000);
41
42   // Reading temperature or humidity takes about 250 milliseconds!
43   // Sensor readings may also be up to 2 seconds 'old' (its a very slow sensor)
44   float h = dht.readHumidity();
45   // Read temperature as Celsius
46   float t = dht.readTemperature();
47   // Read temperature as Fahrenheit
48   float f = dht.readTemperature(true);
49   
50   // Check if any reads failed and exit early (to try again).
51   if (isnan(h) || isnan(t) || isnan(f)) {
52     Serial.println("Failed to read from DHT sensor!");
53     return;
54   }
55
56   // Compute heat index
57   // Must send in temp in Fahrenheit!
58   float hi = dht.computeHeatIndex(f, h);
59
60   Serial.print("Humidity: "); 
61   Serial.print(h);
62   Serial.print(" %\t");
63   Serial.print("Temperature: "); 
64   Serial.print(t);
65   Serial.print(" *C ");
66   Serial.print(f);
67   Serial.print(" *F\t");
68   Serial.print("Heat index: ");
69   Serial.print(hi);
70   Serial.println(" *F");
71 }