Skip to content

Commit 846bbb3

Browse files
authored
Merge pull request mysensors#1 from mozzbozz/master
Ported humidity (DHT) example to MySensors v2.0 and added some improvements
2 parents 3246799 + 7a4f75d commit 846bbb3

File tree

4 files changed

+160
-109
lines changed

4 files changed

+160
-109
lines changed
Lines changed: 152 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,152 @@
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+
}

examples/HumiditySensor/HumiditySensor.ino

Lines changed: 0 additions & 104 deletions
This file was deleted.

libraries/DHT/DHT.cpp

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
2013-06-10: Initial version
2525
2013-06-12: Refactored code
2626
2013-07-01: Add a resetTimer method
27+
2016-07-20: Add force parameter - Torben Woltjen (mozzbozz)
2728
******************************************************************/
2829

2930
#include "DHT.h"
@@ -108,13 +109,15 @@ const char *DHT::getStatusString() {
108109

109110
#endif
110111

111-
void DHT::readSensor()
112+
void DHT::readSensor(bool force)
112113
{
113114
// Make sure we don't poll the sensor too often
114115
// - Max sample rate DHT11 is 1 Hz (duty cicle 1000 ms)
115116
// - Max sample rate DHT22 is 0.5 Hz (duty cicle 2000 ms)
117+
// If 'force' is true, the user has to take care of this -> this way, the
118+
// microcontroller can be set to sleep where it doesn't increase millis().
116119
unsigned long startTime = millis();
117-
if ( (unsigned long)(startTime - lastReadTime) < (model == DHT11 ? 999L : 1999L) ) {
120+
if ( !force && (unsigned long)(startTime - lastReadTime) < (model == DHT11 ? 999L : 1999L) ) {
118121
return;
119122
}
120123
lastReadTime = startTime;

libraries/DHT/DHT.h

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
2013-06-10: Initial version
2525
2013-06-12: Refactored code
2626
2013-07-01: Add a resetTimer method
27+
2016-07-20: Add force parameter - Torben Woltjen (mozzbozz)
2728
******************************************************************/
2829

2930
#ifndef dht_h
@@ -58,6 +59,7 @@ class DHT
5859
void setup(uint8_t pin, DHT_MODEL_t model=AUTO_DETECT);
5960
void resetTimer();
6061

62+
void readSensor(bool force=false);
6163
float getTemperature();
6264
float getHumidity();
6365

@@ -66,7 +68,7 @@ class DHT
6668

6769
DHT_MODEL_t getModel() { return model; }
6870

69-
int getMinimumSamplingPeriod() { return model == DHT11 ? 1000 : 2000; }
71+
unsigned int getMinimumSamplingPeriod() { return model == DHT11 ? 1000 : 2000; }
7072

7173
int8_t getNumberOfDecimalsTemperature() { return model == DHT11 ? 0 : 1; };
7274
int8_t getLowerBoundTemperature() { return model == DHT11 ? 0 : -40; };
@@ -80,8 +82,6 @@ class DHT
8082
static float toCelsius(float fromFahrenheit) { return (fromFahrenheit - 32.0) / 1.8; };
8183

8284
protected:
83-
void readSensor();
84-
8585
float temperature;
8686
float humidity;
8787

0 commit comments

Comments
 (0)