Skip to content

Commit 9a0003d

Browse files
committed
Ported example to MySensors 2.0; refactored code and applied small changes to the DHT library
1 parent 3246799 commit 9a0003d

File tree

3 files changed

+152
-105
lines changed

3 files changed

+152
-105
lines changed
Lines changed: 151 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,151 @@
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+
* 2016-07-20: Converted to MySensors v2.0 and added various improvements - Torben Woltjen (mozzbozz)
24+
*
25+
* DESCRIPTION
26+
* This sketch provides an example how to implement a humidity/temperature
27+
* sensor using 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 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+
dht.setup(DHT_DATA_PIN); // set data pin of DHT sensor
91+
if (UPDATE_INTERVAL <= dht.getMinimumSamplingPeriod()) {
92+
Serial.println("Warning: UPDATE_INTERVAL is smaller than supported by the sensor!");
93+
}
94+
// Sleep for the time of the minimum sampling period to give the sensor time to power up
95+
// (otherwise, timeout erros might occure)
96+
sleep(dht.getMinimumSamplingPeriod());
97+
}
98+
99+
100+
void loop()
101+
{
102+
// Force reading sensor, so it works also after sleep()
103+
dht.readSensor(true);
104+
105+
// Get temperature from DHT library
106+
float temperature = dht.getTemperature();
107+
if (isnan(temperature)) {
108+
Serial.println("Failed reading temperature from DHT!");
109+
} else if (temperature != lastTemp || nNoUpdatesTemp == FORCE_UPDATE_N_READS) {
110+
// Only send temperature if it changed since the last measurement or if we didn't send an update for n times
111+
lastTemp = temperature;
112+
if (!metric) {
113+
temperature = dht.toFahrenheit(temperature);
114+
}
115+
// Reset no updates counter
116+
nNoUpdatesTemp = 0;
117+
temperature += SENSOR_TEMP_OFFSET;
118+
send(msgTemp.set(temperature, 1));
119+
120+
#ifdef MY_DEBUG
121+
Serial.print("T: ");
122+
Serial.println(temperature);
123+
#endif
124+
} else {
125+
// Increase no update counter if the temperature stayed the same
126+
nNoUpdatesTemp++;
127+
}
128+
129+
// Get humidity from DHT library
130+
float humidity = dht.getHumidity();
131+
if (isnan(humidity)) {
132+
Serial.println("Failed reading humidity from DHT");
133+
} else if (humidity != lastHum || nNoUpdatesHum == FORCE_UPDATE_N_READS) {
134+
// Only send humidity if it changed since the last measurement or if we didn't send an update for n times
135+
lastHum = humidity;
136+
// Reset no updates counter
137+
nNoUpdatesHum = 0;
138+
send(msgHum.set(humidity, 1));
139+
140+
#ifdef MY_DEBUG
141+
Serial.print("H: ");
142+
Serial.println(humidity);
143+
#endif
144+
} else {
145+
// Increase no update counter if the humidity stayed the same
146+
nNoUpdatesHum++;
147+
}
148+
149+
// Sleep for a while to save energy
150+
sleep(UPDATE_INTERVAL);
151+
}

examples/HumiditySensor/HumiditySensor.ino

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

libraries/DHT/DHT.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ class DHT
6666

6767
DHT_MODEL_t getModel() { return model; }
6868

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

7171
int8_t getNumberOfDecimalsTemperature() { return model == DHT11 ? 0 : 1; };
7272
int8_t getLowerBoundTemperature() { return model == DHT11 ? 0 : -40; };

0 commit comments

Comments
 (0)