|
| 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 | + * REVISION HISTORY |
| 22 | + * Version 1.0: Henrik EKblad |
| 23 | + * Version 1.1 - 2016-07-20: Converted to MySensors v2.0 and added various improvements - Torben Woltjen (mozzbozz) |
| 24 | + * |
| 25 | + * DESCRIPTION |
| 26 | + * This sketch provides an example of how to implement a humidity/temperature |
| 27 | + * sensor using a DHT11/DHT-22. |
| 28 | + * |
| 29 | + * For more information, please visit: |
| 30 | + * http://www.mysensors.org/build/humidity |
| 31 | + * |
| 32 | + */ |
| 33 | + |
| 34 | +// Enable debug prints |
| 35 | +#define MY_DEBUG |
| 36 | + |
| 37 | +// Enable and select radio type attached |
| 38 | +#define MY_RADIO_NRF24 |
| 39 | +//#define MY_RADIO_RFM69 |
| 40 | +//#define MY_RS485 |
| 41 | + |
| 42 | +#include <SPI.h> |
| 43 | +#include <MySensors.h> |
| 44 | +#include <DHT.h> |
| 45 | + |
| 46 | +// Set this to the pin you connected the DHT's data pin to |
| 47 | +#define DHT_DATA_PIN 2 |
| 48 | + |
| 49 | +// Set this offset if the sensor has a permanent small offset to the real temperatures |
| 50 | +#define SENSOR_TEMP_OFFSET 0 |
| 51 | + |
| 52 | +// Sleep time between sensor updates (in milliseconds) |
| 53 | +// Must be >1000ms for DHT22 and >2000ms for DHT11 |
| 54 | +static const uint64_t UPDATE_INTERVAL = 60000; |
| 55 | + |
| 56 | +// Force sending an update of the temperature after n sensor reads, so a controller showing the |
| 57 | +// timestamp of the last update doesn't show something like 3 hours in the unlikely case, that |
| 58 | +// the value didn't change since; |
| 59 | +// i.e. the sensor would force sending an update every UPDATE_INTERVAL*FORCE_UPDATE_N_READS [ms] |
| 60 | +static const uint8_t FORCE_UPDATE_N_READS = 10; |
| 61 | + |
| 62 | +#define CHILD_ID_HUM 0 |
| 63 | +#define CHILD_ID_TEMP 1 |
| 64 | + |
| 65 | +float lastTemp; |
| 66 | +float lastHum; |
| 67 | +uint8_t nNoUpdatesTemp; |
| 68 | +uint8_t nNoUpdatesHum; |
| 69 | +boolean metric = true; |
| 70 | + |
| 71 | +MyMessage msgHum(CHILD_ID_HUM, V_HUM); |
| 72 | +MyMessage msgTemp(CHILD_ID_TEMP, V_TEMP); |
| 73 | +DHT dht; |
| 74 | + |
| 75 | + |
| 76 | +void presentation() |
| 77 | +{ |
| 78 | + // Send the sketch version information to the gateway |
| 79 | + sendSketchInfo("TemperatureAndHumidity", "1.1"); |
| 80 | + |
| 81 | + // Register all sensors to gw (they will be created as child devices) |
| 82 | + present(CHILD_ID_HUM, S_HUM); |
| 83 | + present(CHILD_ID_TEMP, S_TEMP); |
| 84 | + |
| 85 | + metric = getConfig().isMetric; |
| 86 | +} |
| 87 | + |
| 88 | + |
| 89 | +void setup() |
| 90 | +{ |
| 91 | + dht.setup(DHT_DATA_PIN); // set data pin of DHT sensor |
| 92 | + if (UPDATE_INTERVAL <= dht.getMinimumSamplingPeriod()) { |
| 93 | + Serial.println("Warning: UPDATE_INTERVAL is smaller than supported by the sensor!"); |
| 94 | + } |
| 95 | + // Sleep for the time of the minimum sampling period to give the sensor time to power up |
| 96 | + // (otherwise, timeout errors might occure for the first reading) |
| 97 | + sleep(dht.getMinimumSamplingPeriod()); |
| 98 | +} |
| 99 | + |
| 100 | + |
| 101 | +void loop() |
| 102 | +{ |
| 103 | + // Force reading sensor, so it works also after sleep() |
| 104 | + dht.readSensor(true); |
| 105 | + |
| 106 | + // Get temperature from DHT library |
| 107 | + float temperature = dht.getTemperature(); |
| 108 | + if (isnan(temperature)) { |
| 109 | + Serial.println("Failed reading temperature from DHT!"); |
| 110 | + } else if (temperature != lastTemp || nNoUpdatesTemp == FORCE_UPDATE_N_READS) { |
| 111 | + // Only send temperature if it changed since the last measurement or if we didn't send an update for n times |
| 112 | + lastTemp = temperature; |
| 113 | + if (!metric) { |
| 114 | + temperature = dht.toFahrenheit(temperature); |
| 115 | + } |
| 116 | + // Reset no updates counter |
| 117 | + nNoUpdatesTemp = 0; |
| 118 | + temperature += SENSOR_TEMP_OFFSET; |
| 119 | + send(msgTemp.set(temperature, 1)); |
| 120 | + |
| 121 | + #ifdef MY_DEBUG |
| 122 | + Serial.print("T: "); |
| 123 | + Serial.println(temperature); |
| 124 | + #endif |
| 125 | + } else { |
| 126 | + // Increase no update counter if the temperature stayed the same |
| 127 | + nNoUpdatesTemp++; |
| 128 | + } |
| 129 | + |
| 130 | + // Get humidity from DHT library |
| 131 | + float humidity = dht.getHumidity(); |
| 132 | + if (isnan(humidity)) { |
| 133 | + Serial.println("Failed reading humidity from DHT"); |
| 134 | + } else if (humidity != lastHum || nNoUpdatesHum == FORCE_UPDATE_N_READS) { |
| 135 | + // Only send humidity if it changed since the last measurement or if we didn't send an update for n times |
| 136 | + lastHum = humidity; |
| 137 | + // Reset no updates counter |
| 138 | + nNoUpdatesHum = 0; |
| 139 | + send(msgHum.set(humidity, 1)); |
| 140 | + |
| 141 | + #ifdef MY_DEBUG |
| 142 | + Serial.print("H: "); |
| 143 | + Serial.println(humidity); |
| 144 | + #endif |
| 145 | + } else { |
| 146 | + // Increase no update counter if the humidity stayed the same |
| 147 | + nNoUpdatesHum++; |
| 148 | + } |
| 149 | + |
| 150 | + // Sleep for a while to save energy |
| 151 | + sleep(UPDATE_INTERVAL); |
| 152 | +} |
0 commit comments