Guest User

Untitled

a guest
Nov 11th, 2016
78
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.88 KB | None | 0 0
  1. #define MY_DEBUG
  2. #define MY_GATEWAY_SERIAL
  3.  
  4. #include <SPI.h>
  5. #include <Ethernet.h>
  6. #include <MySensors.h>
  7. #include <DHT.h>
  8. #include <Bounce2.h>
  9.  
  10.  
  11. #define DHT_DATA_PIN 8
  12. #define SENSOR_TEMP_OFFSET 0
  13.  
  14. // Sleep time between sensor updates (in milliseconds)
  15. // Must be >1000ms for DHT22 and >2000ms for DHT11
  16. static const uint64_t UPDATE_INTERVAL = 1000;
  17.  
  18. static const uint8_t FORCE_UPDATE_N_READS = 10;
  19.  
  20. #define CHILD_ID_HUM 0
  21. #define CHILD_ID_TEMP 1
  22. #define CHILD_ID 3
  23.  
  24. float lastTemp;
  25. float lastHum;
  26. uint8_t nNoUpdatesTemp;
  27. uint8_t nNoUpdatesHum;
  28. bool metric = true;
  29.  
  30. MyMessage msgHum(CHILD_ID_HUM, V_HUM);
  31. MyMessage msgTemp(CHILD_ID_TEMP, V_TEMP);
  32. DHT dht;
  33.  
  34. // Change to V_LIGHT if you use S_LIGHT in presentation below
  35. MyMessage msg(CHILD_ID,V_TRIPPED);
  36.  
  37. void before() {
  38.  
  39. }
  40. void presentation()
  41. {
  42. // Send the sketch version information to the gateway
  43. sendSketchInfo("S-tmp", "1.1");
  44.  
  45. // Register all sensors to gw (they will be created as child devices)
  46. present(CHILD_ID_HUM, S_HUM);
  47. present(CHILD_ID_TEMP, S_TEMP);
  48.  
  49. }
  50.  
  51.  
  52.  
  53. void setup()
  54. {
  55. dht.setup(DHT_DATA_PIN); // set data pin of DHT sensor
  56. if (UPDATE_INTERVAL <= dht.getMinimumSamplingPeriod()) {
  57. Serial.println("Warning: UPDATE_INTERVAL is smaller than supported by the sensor!");
  58. }
  59.  
  60. }
  61.  
  62.  
  63. void loop()
  64. {
  65.  
  66.  
  67. dht.readSensor(true);
  68.  
  69. // Get temperature from DHT library
  70. float temperature = dht.getTemperature();
  71. if (isnan(temperature)) {
  72. Serial.println("Failed reading temperature from DHT!");
  73. } else if (temperature != lastTemp || nNoUpdatesTemp == FORCE_UPDATE_N_READS) {
  74. // Only send temperature if it changed since the last measurement or if we didn't send an update for n times
  75. lastTemp = temperature;
  76. if (!metric) {
  77. temperature = dht.toFahrenheit(temperature);
  78. }
  79. // Reset no updates counter
  80. nNoUpdatesTemp = 0;
  81. temperature += SENSOR_TEMP_OFFSET;
  82. send(msgTemp.set(temperature, 1));
  83.  
  84. #ifdef MY_DEBUG
  85. Serial.print("T: ");
  86. Serial.println(temperature);
  87. #endif
  88. } else {
  89. // Increase no update counter if the temperature stayed the same
  90. nNoUpdatesTemp++;
  91. }
  92.  
  93. // Get humidity from DHT library
  94. float humidity = dht.getHumidity();
  95. if (isnan(humidity)) {
  96. Serial.println("Failed reading humidity from DHT");
  97. } else if (humidity != lastHum || nNoUpdatesHum == FORCE_UPDATE_N_READS) {
  98. // Only send humidity if it changed since the last measurement or if we didn't send an update for n times
  99. lastHum = humidity;
  100. // Reset no updates counter
  101. nNoUpdatesHum = 0;
  102. send(msgHum.set(humidity, 1));
  103.  
  104. #ifdef MY_DEBUG
  105. Serial.print("H: ");
  106. Serial.println(humidity);
  107. #endif
  108. } else {
  109. // Increase no update counter if the humidity stayed the same
  110. nNoUpdatesHum++;
  111. }
  112.  
  113. // Sleep for a while to save energy
  114. sleep(UPDATE_INTERVAL);
  115. }
Add Comment
Please, Sign In to add comment