14
14
#include < EEPROM.h>
15
15
#include < sha204_lib_return_codes.h>
16
16
#include < sha204_library.h>
17
+ #include < RunningAverage.h>
18
+ #include < avr/power.h>
17
19
18
20
// Define a static node address, remove if you want auto address assignment
19
21
// #define NODE_ADDRESS 3
20
22
21
- #define RELEASE " 1.1"
23
+ #define RELEASE " 1.2"
24
+
25
+ #define AVERAGES 2
22
26
23
27
// Child sensor ID's
24
28
#define CHILD_ID_TEMP 1
25
29
#define CHILD_ID_HUM 2
26
- #define CHILD_ID_BATT 199
27
30
28
31
// How many milli seconds between each measurement
29
32
#define MEASURE_INTERVAL 60000
34
37
// When MEASURE_INTERVAL is 60000 and FORCE_TRANSMIT_INTERVAL is 30, we force a transmission every 30 minutes.
35
38
// Between the forced transmissions a tranmission will only occur if the measured value differs from the previous measurement
36
39
37
- // Pin definitions
40
+ // Pin definitions
38
41
#define TEST_PIN A0
39
42
#define LED_PIN A2
40
43
#define ATSHA204_PIN 17 // A3
@@ -50,21 +53,28 @@ MySensor gw;
50
53
// Sensor messages
51
54
MyMessage msgHum (CHILD_ID_HUM, V_HUM);
52
55
MyMessage msgTemp (CHILD_ID_TEMP, V_TEMP);
53
- MyMessage msgBattery (CHILD_ID_BATT, V_VOLTAGE);
54
56
55
57
// Global settings
56
58
int measureCount = 0 ;
59
+ int sendBattery = 0 ;
57
60
boolean isMetric = true ;
61
+ boolean highfreq = true ;
58
62
59
63
// Storage of old measurements
60
64
float lastTemperature = -100 ;
61
65
int lastHumidity = -100 ;
62
66
long lastBattery = -100 ;
63
67
64
- bool highfreq = true ;
68
+ RunningAverage raHum (AVERAGES);
69
+ RunningAverage raTemp (AVERAGES);
65
70
71
+ /* ***************************************************
72
+ *
73
+ * Setup code
74
+ *
75
+ ****************************************************/
66
76
void setup () {
67
-
77
+
68
78
pinMode (LED_PIN, OUTPUT);
69
79
digitalWrite (LED_PIN, LOW);
70
80
@@ -79,6 +89,10 @@ void setup() {
79
89
digitalWrite (TEST_PIN, HIGH); // Enable pullup
80
90
if (!digitalRead (TEST_PIN)) testMode ();
81
91
92
+ // Make sure that ATSHA204 is not floating
93
+ pinMode (ATSHA204_PIN, INPUT);
94
+ digitalWrite (ATSHA204_PIN, HIGH);
95
+
82
96
digitalWrite (TEST_PIN,LOW);
83
97
digitalWrite (LED_PIN, HIGH);
84
98
@@ -99,52 +113,82 @@ void setup() {
99
113
gw.present (CHILD_ID_HUM,S_HUM);
100
114
101
115
isMetric = gw.getConfig ().isMetric ;
102
- Serial.print (" isMetric: " ); Serial.println (isMetric);
103
-
116
+ Serial.print (F (" isMetric: " )); Serial.println (isMetric);
117
+ raHum.clear ();
118
+ raTemp.clear ();
119
+ sendTempHumidityMeasurements (false );
120
+ sendBattLevel (false );
104
121
}
105
122
106
123
107
- // Main loop function
124
+ /* **********************************************
125
+ *
126
+ * Main loop function
127
+ *
128
+ ***********************************************/
108
129
void loop () {
109
130
measureCount ++;
131
+ sendBattery ++;
110
132
bool forceTransmit = false ;
111
-
112
- // When we wake up the 5th time after power on, switch to 1Mhz clock
113
- // This allows us to print debug messages on startup (as serial port is dependend on oscilator settings).
114
- if ((measureCount == 5 ) && highfreq) switchClock (1 <<CLKPS2); // Switch to 1Mhz for the reminder of the sketch, save power.
133
+
134
+ if ((measureCount == 5 ) && highfreq)
135
+ {
136
+ clock_prescale_set (clock_div_8); // Switch to 1Mhz for the reminder of the sketch, save power.
137
+ highfreq = false ;
138
+ }
115
139
116
140
if (measureCount > FORCE_TRANSMIT_INTERVAL) { // force a transmission
117
141
forceTransmit = true ;
118
142
measureCount = 0 ;
119
143
}
120
144
121
145
gw.process ();
122
- sendBattLevel (forceTransmit);
146
+
123
147
sendTempHumidityMeasurements (forceTransmit);
148
+ if (sendBattery > 60 )
149
+ {
150
+ sendBattLevel (forceTransmit); // Not needed to send battery info that often
151
+ sendBattery = 0 ;
152
+ }
124
153
125
154
gw.sleep (MEASURE_INTERVAL);
126
155
}
127
156
128
- /*
157
+
158
+ /* ********************************************
159
+ *
129
160
* Sends temperature and humidity from Si7021 sensor
130
161
*
131
162
* Parameters
132
163
* - force : Forces transmission of a value (even if it's the same as previous measurement)
133
- */
164
+ *
165
+ *********************************************/
134
166
void sendTempHumidityMeasurements (bool force)
135
167
{
136
- if (force) {
137
- lastHumidity = -100 ;
138
- lastTemperature = -100 ;
139
- }
168
+ bool tx = force;
140
169
141
170
si7021_env data = humiditySensor.getHumidityAndTemperature ();
171
+ float oldAvgTemp = raTemp.getAverage ();
172
+ float oldAvgHum = raHum.getAverage ();
142
173
143
- float temperature = (isMetric ? data.celsiusHundredths : data.fahrenheitHundredths ) / 100.0 ;
144
-
145
- int humidity = data.humidityPercent ;
174
+ raTemp.addValue (data.celsiusHundredths / 100 );
175
+ raHum.addValue (data.humidityPercent );
176
+
177
+ float diffTemp = abs (oldAvgTemp - raTemp.getAverage ());
178
+ float diffHum = abs (oldAvgHum - raHum.getAverage ());
179
+
180
+ Serial.println (diffTemp);
181
+ Serial.println (diffHum);
182
+
183
+ if (isnan (diffTemp)) tx = true ;
184
+ if (diffTemp > 0.2 ) tx = true ;
185
+ if (diffHum > 0.5 ) tx = true ;
146
186
147
- if ((lastTemperature != temperature) | (lastHumidity != humidity)) {
187
+ if (tx) {
188
+ measureCount = 0 ;
189
+ float temperature = (isMetric ? data.celsiusHundredths : data.fahrenheitHundredths ) / 100.0 ;
190
+
191
+ int humidity = data.humidityPercent ;
148
192
Serial.print (" T: " );Serial.println (temperature);
149
193
Serial.print (" H: " );Serial.println (humidity);
150
194
@@ -155,12 +199,14 @@ void sendTempHumidityMeasurements(bool force)
155
199
}
156
200
}
157
201
158
- /*
159
- * Sends battery information (both voltage, and battery percentage)
202
+ /* *******************************************
203
+ *
204
+ * Sends battery information (battery percentage)
160
205
*
161
206
* Parameters
162
207
* - force : Forces transmission of a value
163
- */
208
+ *
209
+ *******************************************/
164
210
void sendBattLevel (bool force)
165
211
{
166
212
if (force) lastBattery = -1 ;
@@ -176,6 +222,11 @@ void sendBattLevel(bool force)
176
222
}
177
223
}
178
224
225
+ /* ******************************************
226
+ *
227
+ * Internal battery ADC measuring
228
+ *
229
+ *******************************************/
179
230
long readVcc () {
180
231
// Read 1.1V reference against AVcc
181
232
// set the reference to Vcc and the measurement to the internal 1.1V reference
@@ -202,18 +253,11 @@ long readVcc() {
202
253
return result; // Vcc in millivolts
203
254
}
204
255
205
- void switchClock (unsigned char clk)
206
- {
207
- cli ();
208
-
209
- CLKPR = 1 <<CLKPCE; // Set CLKPCE to enable clk switching
210
- CLKPR = clk;
211
- sei ();
212
- highfreq = false ;
213
- }
214
-
215
-
216
- // Verify all peripherals, and signal via the LED if any problems.
256
+ /* ***************************************************
257
+ *
258
+ * Verify all peripherals, and signal via the LED if any problems.
259
+ *
260
+ ****************************************************/
217
261
void testMode ()
218
262
{
219
263
uint8_t rx_buffer[SHA204_RSP_SIZE_MAX];
0 commit comments