Guest User

MySensors DHT11

a guest
Dec 9th, 2017
243
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.23 KB | None | 0 0
  1. /**
  2. * The MySensors Arduino library handles the wireless radio link and protocol
  3. * between your home built sensors/actuators and HA controller of choice.
  4. * The sensors forms a self healing radio network with optional repeaters. Each
  5. * repeater and gateway builds a routing tables in EEPROM which keeps track of the
  6. * network topology allowing messages to be routed to nodes.
  7. *
  8. * Created by Henrik Ekblad <[email protected]>
  9. * Copyright (C) 2013-2015 Sensnology AB
  10. * Full contributor list: https://github.com/mysensors/Arduino/graphs/contributors
  11. *
  12. * Documentation: http://www.mysensors.org
  13. * Support Forum: http://forum.mysensors.org
  14. *
  15. * This program is free software; you can redistribute it and/or
  16. * modify it under the terms of the GNU General Public License
  17. * version 2 as published by the Free Software Foundation.
  18. *
  19. *******************************
  20. *
  21. * DESCRIPTION
  22. * The ArduinoGateway prints data received from sensors on the serial link.
  23. * The gateway accepts input on seral which will be sent out on radio network.
  24. *
  25. * The GW code is designed for Arduino Nano 328p / 16MHz
  26. *
  27. * Wire connections (OPTIONAL):
  28. * - Inclusion button should be connected between digital pin 3 and GND
  29. * - RX/TX/ERR leds need to be connected between +5V (anode) and digital pin 6/5/4 with resistor 270-330R in a series
  30. *
  31. * LEDs (OPTIONAL):
  32. * - To use the feature, uncomment any of the MY_DEFAULT_xx_LED_PINs
  33. * - RX (green) - blink fast on radio message recieved. In inclusion mode will blink fast only on presentation recieved
  34. * - TX (yellow) - blink fast on radio message transmitted. In inclusion mode will blink slowly
  35. * - ERR (red) - fast blink on error during transmission error or recieve crc error
  36. *
  37. */
  38.  
  39. // Enable debug prints to serial monitor
  40. #define MY_DEBUG
  41.  
  42.  
  43. // Enable and select radio type attached
  44. //#define MY_RADIO_NRF24
  45. //#define MY_RADIO_RFM69
  46.  
  47. // Set LOW transmit power level as default, if you have an amplified NRF-module and
  48. // power your radio separately with a good regulator you can turn up PA level.
  49. //#define MY_RF24_PA_LEVEL RF24_PA_LOW
  50.  
  51. // Enable serial gateway
  52. #define MY_GATEWAY_SERIAL
  53.  
  54. // Define a lower baud rate for Arduino's running on 8 MHz (Arduino Pro Mini 3.3V & SenseBender)
  55. #if F_CPU == 8000000L
  56. #define MY_BAUD_RATE 38400
  57. #endif
  58.  
  59. // Enable inclusion mode
  60. #define MY_INCLUSION_MODE_FEATURE
  61. // Enable Inclusion mode button on gateway
  62. //#define MY_INCLUSION_BUTTON_FEATURE
  63.  
  64. // Inverses behavior of inclusion button (if using external pullup)
  65. //#define MY_INCLUSION_BUTTON_EXTERNAL_PULLUP
  66.  
  67. // Set inclusion mode duration (in seconds)
  68. #define MY_INCLUSION_MODE_DURATION 60
  69. // Digital pin used for inclusion mode button
  70. //#define MY_INCLUSION_MODE_BUTTON_PIN 3
  71.  
  72. // Set blinking period
  73. #define MY_DEFAULT_LED_BLINK_PERIOD 300
  74.  
  75. // Inverses the behavior of leds
  76. //#define MY_WITH_LEDS_BLINKING_INVERSE
  77.  
  78. // Flash leds on rx/tx/err
  79. // Uncomment to override default HW configurations
  80. //#define MY_DEFAULT_ERR_LED_PIN 4 // Error led pin
  81. //#define MY_DEFAULT_RX_LED_PIN 6 // Receive led pin
  82. //#define MY_DEFAULT_TX_LED_PIN 5 // the PCB, on board LED
  83.  
  84. #include <MySensors.h>
  85. #include <Arduino.h>
  86. #include <SPI.h>
  87. #include <DHT.h>
  88.  
  89. #include "DHT.h"
  90.  
  91. #define DHTPIN 5
  92.  
  93. // Set this offset if the sensor has a permanent small offset to the real temperatures
  94. #define SENSOR_TEMP_OFFSET 0
  95.  
  96. static const uint64_t UPDATE_INTERVAL = 60000;
  97.  
  98. static const uint8_t FORCE_UPDATE_N_READS = 10;
  99.  
  100. #define CHILD_ID_HUM 0
  101. #define CHILD_ID_TEMP 1
  102.  
  103. float lastTemp;
  104. float lastHum;
  105. uint8_t nNoUpdatesTemp;
  106. uint8_t nNoUpdatesHum;
  107. bool metric = true;
  108.  
  109.  
  110. MyMessage msgHum(CHILD_ID_HUM, V_HUM);
  111. MyMessage msgTemp(CHILD_ID_TEMP, V_TEMP);
  112.  
  113.  
  114. #define DHTTYPE DHT11
  115.  
  116.  
  117. DHT dht(DHTPIN, DHTTYPE);
  118.  
  119.  
  120. void setup()
  121. {
  122. // Setup locally attached sensors
  123. Serial.begin(115200);
  124. dht.begin();
  125.  
  126. // dht.setup(DHTPIN); // set data pin of DHT sensor
  127. // if (UPDATE_INTERVAL <= dht.getMinimumSamplingPeriod()) {
  128. // Serial.println("Warning: UPDATE_INTERVAL is smaller than supported by the sensor!");
  129. // }
  130. // // Sleep for the time of the minimum sampling period to give the sensor time to power up
  131. // // (otherwise, timeout errors might occure for the first reading)
  132. // sleep(dht.getMinimumSamplingPeriod());
  133. }
  134.  
  135. void presentation()
  136. {
  137. // Present locally attached sensors
  138. // Send the sketch version information to the gateway and Controller
  139. sendSketchInfo("TemperatureAndHumidity", "1.1");
  140.  
  141. // Register all sensors to gw (they will be created as child devices)
  142. present(CHILD_ID_HUM, S_HUM);
  143. present(CHILD_ID_TEMP, S_TEMP);
  144.  
  145. metric = getControllerConfig().isMetric;
  146. }
  147.  
  148. void loop()
  149. {
  150. // Force reading sensor, so it works also after sleep()
  151. dht.begin();
  152.  
  153. // Get temperature from DHT library
  154. float temperature = dht.readTemperature();
  155. if (isnan(temperature)) {
  156. Serial.println("Failed reading temperature from DHT!");
  157. } else if (temperature != lastTemp || nNoUpdatesTemp == FORCE_UPDATE_N_READS) {
  158. // Only send temperature if it changed since the last measurement or if we didn't send an update for n times
  159. lastTemp = temperature;
  160. // if (!metric) {
  161. // temperature = dht.toFahrenheit(temperature);
  162. // }
  163. // Reset no updates counter
  164. nNoUpdatesTemp = 0;
  165. temperature += SENSOR_TEMP_OFFSET;
  166. send(msgTemp.set(temperature, 1));
  167.  
  168. #ifdef MY_DEBUG
  169. Serial.print("T: ");
  170. Serial.println(temperature);
  171. #endif
  172. } else {
  173. // Increase no update counter if the temperature stayed the same
  174. nNoUpdatesTemp++;
  175. }
  176.  
  177. // Get humidity from DHT library
  178. float humidity = dht.readHumidity();
  179. if (isnan(humidity)) {
  180. Serial.println("Failed reading humidity from DHT");
  181. } else if (humidity != lastHum || nNoUpdatesHum == FORCE_UPDATE_N_READS) {
  182. // Only send humidity if it changed since the last measurement or if we didn't send an update for n times
  183. lastHum = humidity;
  184. // Reset no updates counter
  185. nNoUpdatesHum = 0;
  186. send(msgHum.set(humidity, 1));
  187.  
  188. #ifdef MY_DEBUG
  189. Serial.print("H: ");
  190. Serial.println(humidity);
  191. #endif
  192. } else {
  193. // Increase no update counter if the humidity stayed the same
  194. nNoUpdatesHum++;
  195. }
  196.  
  197. // Sleep for a while to save energy
  198. sleep(UPDATE_INTERVAL);
  199. }
Advertisement
Add Comment
Please, Sign In to add comment