Advertisement
gregwa

FCM120 - DHT22 with Bluetooth.ino

Apr 2nd, 2017
439
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 6.24 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. #include <SoftwareSerial.h>
  16.  
  17.  
  18. #define bluetoothTx       2   // TX-O pin of bluetooth module, Arduino D2
  19. #define bluetoothRx       3   // RX-I pin of bluetooth module, Arduino D3
  20. #define DHTPIN            4   // Pin which is connected to the DHT sensor
  21.  
  22. int led = 13;
  23.  
  24. int buttonPin1 = 7;
  25. int buttonPin2 = 8;
  26. int button1State = 0;
  27. int button2State = 0;
  28.  
  29. int dataFromBt;
  30.  
  31. boolean lightBlink = false;
  32.  
  33. SoftwareSerial bluetooth(bluetoothTx, bluetoothRx);
  34.  
  35.  
  36. // Uncomment the type of sensor in use:
  37. //#define DHTTYPE           DHT11     // DHT 11
  38. #define DHTTYPE           DHT22     // DHT 22 (AM2302)
  39. //#define DHTTYPE           DHT21     // DHT 21 (AM2301)
  40.  
  41. DHT_Unified dht(DHTPIN, DHTTYPE);
  42.  
  43. uint32_t delayMS;
  44.  
  45. void setup() {
  46.   Serial.begin(9600);
  47.   // Initialize device.
  48.   dht.begin();
  49.   Serial.println("DHTxx Unified Sensor Example");
  50.   // Print temperature sensor details.
  51.   sensor_t sensor;
  52.   dht.temperature().getSensor(&sensor);
  53.  
  54.   // Print humidity sensor details.
  55.   dht.humidity().getSensor(&sensor);
  56.   delayMS = sensor.min_delay / 1000;
  57.  
  58.   // Bluetooth Setup
  59.   bluetooth.begin(115200); // The Bluetooth Mate defaults to 115200bps
  60.   bluetooth.print("$"); // Print three times individually
  61.   bluetooth.print("$");
  62.   bluetooth.print("$"); // Enter command mode
  63.   delay(100); // Short delay, wait for the Mate to send back CMD
  64.   bluetooth.println("U,9600,N"); // Temporarily Change the baudrate to 9600, no parity
  65.   // 115200 can be too fast at times for NewSoftSerial to relay the data reliably
  66.   bluetooth.begin(9600); // Start bluetooth serial at 9600
  67.   pinMode(led, OUTPUT);
  68.   pinMode(buttonPin1, INPUT);
  69.   pinMode(buttonPin2, INPUT);
  70. }
  71.  
  72. void loop() {
  73.  
  74.   handleBluetooth();
  75. }
  76.  
  77. void handleBluetooth() {
  78.     if(bluetooth.available()) // If the bluetooth sent any characters
  79.     {
  80.         sensors_event_t event;  
  81.         dataFromBt = bluetooth.read();
  82.         switch (dataFromBt) {
  83.             case 'T':
  84.             case 't':
  85.                 dht.temperature().getEvent(&event);
  86.                 if (isnan(event.temperature)) {
  87.                     bluetooth.println(" Error reading temperature!");
  88.                 } else {
  89.                     bluetooth.print("Temperature: ");
  90.                     bluetooth.print(event.temperature);
  91.                     bluetooth.print(" *C - ");
  92.                     bluetooth.print((event.temperature * 1.8) + 32);
  93.                     bluetooth.println(" *F");
  94.                 }
  95.                 delay(delayMS);
  96.                 break;
  97.             case 'H':
  98.             case 'h':
  99.                 // get humidity
  100.                 dht.humidity().getEvent(&event);
  101.                 if (isnan(event.relative_humidity)) {
  102.                     bluetooth.println("Error reading temperature!");
  103.                 } else {
  104.                     bluetooth.print("Humidity: ");
  105.                     bluetooth.print(event.relative_humidity);
  106.                     bluetooth.println("%");
  107.                 }
  108.                 delay(delayMS);
  109.                 break;
  110.             case 'B':
  111.             case 'b':
  112.                 // Barometric
  113.                 bluetooth.println("Barometric Pressure not yet supported");
  114.                 delay(delayMS);
  115.                 break;
  116.             case 'A':
  117.             case 'a':
  118.                 // All data
  119.                 dht.temperature().getEvent(&event);
  120.                 if (isnan(event.temperature)) {
  121.                     bluetooth.println(" Error reading temperature!");
  122.                 } else {
  123.                     bluetooth.print("Temperature: ");
  124.                     bluetooth.print(event.temperature);
  125.                     bluetooth.print(" *C - ");
  126.                     bluetooth.print((event.temperature * 1.8) + 32);
  127.                     bluetooth.println(" *F");
  128.                 }
  129.                 dht.humidity().getEvent(&event);
  130.                 if (isnan(event.relative_humidity)) {
  131.                     bluetooth.println("Error reading temperature!");
  132.                 } else {
  133.                     bluetooth.print("Humidity: ");
  134.                     bluetooth.print(event.relative_humidity);
  135.                     bluetooth.println("%");
  136.                 }                
  137.                 delay(delayMS);
  138.                 break;
  139.             default:
  140.                 break;
  141.         }
  142.     }
  143. }
  144.  
  145. void handleSerial() {
  146.     while (Serial.available() > 0) {
  147.         char inChar = Serial.read();
  148.         switch (inChar) {
  149.             case 'T':
  150.             case 't':
  151.                 // get temp
  152.                 sensors_event_t event;  
  153.                 dht.temperature().getEvent(&event);
  154.                 if (isnan(event.temperature)) {
  155.                     Serial.println("Error reading temperature!");
  156.                 }
  157.                 else {
  158.                     Serial.print("Temperature: ");
  159.                     Serial.print(event.temperature);
  160.                     Serial.print(" *C - ");
  161.                     Serial.print((event.temperature * 1.8) + 32);
  162.                     Serial.println(" *F");
  163.                 }
  164.                 // Delay between measurements.
  165.                 delay(delayMS);                
  166.                 break;
  167.             case 'H':
  168.             case 'h':
  169.                 // get humidity
  170.                 dht.humidity().getEvent(&event);
  171.                 if (isnan(event.relative_humidity)) {
  172.                     Serial.println("Error reading humidity!");
  173.                 }
  174.                 else {
  175.                     Serial.print("Humidity: ");
  176.                     Serial.print(event.relative_humidity);
  177.                     Serial.println("%");
  178.                 }
  179.                 // Delay between measurements.
  180.                 delay(delayMS);
  181.                 break;
  182.             default:
  183.                 break;
  184.         }
  185.     }
  186. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement