Guest User

Untitled

a guest
May 9th, 2017
76
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. // Enable and select radio type attached
  2. #define MY_RADIO_NRF24
  3.  
  4. // Enable debugging
  5. #define MY_DEBUG
  6.  
  7. //#define MY_NODE_ID 1
  8.  
  9. #include <DHT.h>
  10. #include <DHT_U.h>
  11. #include <MySensors.h>
  12.  
  13. // Set this to the pin you connected the DHT's data pin to
  14. #define DHT_DATA_PIN 3
  15.  
  16. // We're using a DHT22
  17. #define DHTTYPE DHT22
  18.  
  19. // Set a child sensor id
  20. #define CHILD_ID_HUM 0
  21. #define CHILD_ID_TEMP 1
  22.  
  23. // Set this offset if the sensor has a permanent small offset to the real temperatures
  24. #define SENSOR_TEMP_OFFSET 0
  25.  
  26.  
  27. // Sleep time between sensor updates (in milliseconds)
  28. // Must be >1000ms for DHT22 and >2000ms for DHT11
  29. static const uint64_t UPDATE_INTERVAL = 1001;
  30.  
  31. DHT_Unified dht(DHT_DATA_PIN, DHTTYPE);
  32.  
  33. uint32_t delayMS;
  34. float humidity;
  35. float temperature;
  36.  
  37. MyMessage msgHum(CHILD_ID_HUM, V_HUM);
  38. MyMessage msgTemp(CHILD_ID_TEMP, V_TEMP);
  39.  
  40. bool initialValueSent = false;
  41.  
  42.  
  43. void setup()
  44. {
  45. dht.begin();
  46. sensor_t sensor;
  47. }
  48.  
  49. void presentation()
  50. {
  51. sendSketchInfo("DHT22 Sensor", "0.1");
  52. present(CHILD_ID_TEMP, S_TEMP);
  53. present(CHILD_ID_HUM, S_HUM);
  54.  
  55. }
  56.  
  57.  
  58. void loop()
  59. {
  60. // Read stuff from the DHT sensor
  61. sensors_event_t event;
  62. dht.temperature().getEvent(&event);
  63. if (isnan(event.temperature)) {
  64. Serial.println("Error reading temperature.");
  65. temperature = 0;
  66. }
  67. else {
  68. temperature = event.temperature;
  69.  
  70. }
  71. // Get humidity event and print its value.
  72. dht.humidity().getEvent(&event);
  73. if (isnan(event.relative_humidity)) {
  74. Serial.println("Error reading humidity!");
  75. humidity = 0;
  76. }
  77. else {
  78. humidity = event.relative_humidity;
  79. }
  80. send(msgHum.set(temperature, 1));
  81. #ifdef My_DEBUG
  82. Serial.print("T: ")
  83. Serial.println(temperature);
  84. #endif
  85. send(msgTemp.set(humidity, 1));
  86. #ifdef My_DEBUG
  87. Serial.print("H: ")
  88. Serial.println(humidity);
  89. #endif
  90.  
  91. // Sleep for a while to save energy
  92. sleep(UPDATE_INTERVAL);
  93. }
RAW Paste Data