Advertisement
gregwa

FCM120 - DHT22.ino

Apr 2nd, 2017
377
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 4.08 KB | None | 0 0
  1. // DHT Temperature & Humidity Sensor
  2. // Unified Sensor Library Example
  3. // Originally Written by Tony DiCola for Adafruit Industries
  4. // Released under an MIT license.
  5. // Modified by G.D. Walters for Full Circle Magazine
  6. // April, 2017
  7.  
  8. // Depends on the following Arduino libraries:
  9. // - Adafruit Unified Sensor Library: https://github.com/adafruit/Adafruit_Sensor
  10. // - DHT Sensor Library: https://github.com/adafruit/DHT-sensor-library
  11.  
  12. #include <Adafruit_Sensor.h>
  13. #include <DHT.h>
  14. #include <DHT_U.h>
  15.  
  16. #define DHTPIN            4         // Pin which is connected to the DHT sensor.
  17.  
  18. // Uncomment the type of sensor in use:
  19. //#define DHTTYPE           DHT11     // DHT 11
  20. #define DHTTYPE           DHT22     // DHT 22 (AM2302)
  21. //#define DHTTYPE           DHT21     // DHT 21 (AM2301)
  22.  
  23. // See guide for details on sensor wiring and usage:
  24. //   https://learn.adafruit.com/dht/overview
  25.  
  26. DHT_Unified dht(DHTPIN, DHTTYPE);
  27.  
  28. uint32_t delayMS;
  29.  
  30. void setup() {
  31.   Serial.begin(9600);
  32.   // Initialize device.
  33.   dht.begin();
  34.   Serial.println("DHTxx Unified Sensor Example");
  35.   // Print temperature sensor details.
  36.   sensor_t sensor;
  37.   dht.temperature().getSensor(&sensor);
  38.   Serial.println("------------------------------------");
  39.   Serial.println("Temperature");
  40.   Serial.print  ("Sensor:       "); Serial.println(sensor.name);
  41.   Serial.print  ("Driver Ver:   "); Serial.println(sensor.version);
  42.   Serial.print  ("Unique ID:    "); Serial.println(sensor.sensor_id);
  43.   Serial.print  ("Max Value:    "); Serial.print(sensor.max_value); Serial.println(" *C");
  44.   Serial.print  ("Min Value:    "); Serial.print(sensor.min_value); Serial.println(" *C");
  45.   Serial.print  ("Resolution:   "); Serial.print(sensor.resolution); Serial.println(" *C");  
  46.   Serial.println("------------------------------------");
  47.   // Print humidity sensor details.
  48.   dht.humidity().getSensor(&sensor);
  49.   Serial.println("------------------------------------");
  50.   Serial.println("Humidity");
  51.   Serial.print  ("Sensor:       "); Serial.println(sensor.name);
  52.   Serial.print  ("Driver Ver:   "); Serial.println(sensor.version);
  53.   Serial.print  ("Unique ID:    "); Serial.println(sensor.sensor_id);
  54.   Serial.print  ("Max Value:    "); Serial.print(sensor.max_value); Serial.println("%");
  55.   Serial.print  ("Min Value:    "); Serial.print(sensor.min_value); Serial.println("%");
  56.   Serial.print  ("Resolution:   "); Serial.print(sensor.resolution); Serial.println("%");  
  57.   Serial.println("------------------------------------");
  58.   // Set delay between sensor readings based on sensor details.
  59.   delayMS = sensor.min_delay / 1000;
  60. }
  61.  
  62. void loop() {
  63.  
  64.   handleSerial();
  65.  
  66. }
  67.  
  68. void handleSerial() {
  69.     while (Serial.available() > 0) {
  70.         char inChar = Serial.read();
  71.         switch (inChar) {
  72.             case 'T':
  73.             case 't':
  74.                 // get temp
  75.                 sensors_event_t event;  
  76.                 dht.temperature().getEvent(&event);
  77.                 if (isnan(event.temperature)) {
  78.                     Serial.println("Error reading temperature!");
  79.                 }
  80.                 else {
  81.                     Serial.print("Temperature: ");
  82.                     Serial.print(event.temperature);
  83.                     Serial.print(" *C - ");
  84.                     Serial.print((event.temperature * 1.8) + 32);
  85.                     Serial.println(" *F");
  86.                 }
  87.                 // Delay between measurements.
  88.                 delay(delayMS);                
  89.                 break;
  90.             case 'H':
  91.             case 'h':
  92.                 // get humidity
  93.                 dht.humidity().getEvent(&event);
  94.                 if (isnan(event.relative_humidity)) {
  95.                     Serial.println("Error reading humidity!");
  96.                 }
  97.                 else {
  98.                     Serial.print("Humidity: ");
  99.                     Serial.print(event.relative_humidity);
  100.                     Serial.println("%");
  101.                 }
  102.                 // Delay between measurements.
  103.                 delay(delayMS);
  104.                 break;
  105.             default:
  106.                 break;
  107.         }
  108.     }
  109. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement