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
- #define MEASURE_INTERVAL 60000
32
+ #define MEASURE_INTERVAL 1000
30
33
31
34
// FORCE_TRANSMIT_INTERVAL, this number of times of wakeup, the sensor is forced to report all values to the controller
32
35
#define FORCE_TRANSMIT_INTERVAL 30
33
36
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,25 +53,32 @@ 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 ;
58
61
59
62
// Storage of old measurements
60
63
float lastTemperature = -100 ;
61
64
int lastHumidity = -100 ;
62
65
long lastBattery = -100 ;
63
66
64
- bool highfreq = true ;
67
+ RunningAverage raHum (AVERAGES);
68
+ RunningAverage raTemp (AVERAGES);
65
69
70
+ /* ***************************************************
71
+ *
72
+ * Setup code
73
+ *
74
+ ****************************************************/
66
75
void setup () {
67
-
76
+ // clock_prescale_set(clock_div_8); // Switch to 1Mhz right after powerup.
77
+
68
78
pinMode (LED_PIN, OUTPUT);
69
79
digitalWrite (LED_PIN, LOW);
70
80
71
- Serial.begin (115200 );
81
+ Serial.begin (19200 );
72
82
Serial.print (F (" Sensebender Micro FW " ));
73
83
Serial.print (RELEASE);
74
84
Serial.flush ();
@@ -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,75 @@ 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 );
104
120
}
105
121
106
122
107
- // Main loop function
123
+ /* **********************************************
124
+ *
125
+ * Main loop function
126
+ *
127
+ ***********************************************/
108
128
void loop () {
109
129
measureCount ++;
130
+ sendBattery ++;
110
131
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.
115
132
116
133
if (measureCount > FORCE_TRANSMIT_INTERVAL) { // force a transmission
117
134
forceTransmit = true ;
118
135
measureCount = 0 ;
119
136
}
120
137
121
138
gw.process ();
122
- sendBattLevel (forceTransmit);
139
+
123
140
sendTempHumidityMeasurements (forceTransmit);
141
+ if (sendBattery > 60 )
142
+ {
143
+ sendBattLevel (forceTransmit); // Not needed to send battery info that often
144
+ sendBattery = 0 ;
145
+ }
124
146
125
147
gw.sleep (MEASURE_INTERVAL);
126
148
}
127
149
128
- /*
150
+
151
+ /* ********************************************
152
+ *
129
153
* Sends temperature and humidity from Si7021 sensor
130
154
*
131
155
* Parameters
132
156
* - force : Forces transmission of a value (even if it's the same as previous measurement)
133
- */
157
+ *
158
+ *********************************************/
134
159
void sendTempHumidityMeasurements (bool force)
135
160
{
136
- if (force) {
137
- lastHumidity = -100 ;
138
- lastTemperature = -100 ;
139
- }
161
+ bool tx = force;
140
162
141
163
si7021_env data = humiditySensor.getHumidityAndTemperature ();
164
+ float oldAvgTemp = raTemp.getAverage ();
165
+ float oldAvgHum = raHum.getAverage ();
142
166
143
- float temperature = (isMetric ? data.celsiusHundredths : data.fahrenheitHundredths ) / 100.0 ;
144
-
145
- int humidity = data.humidityPercent ;
167
+ raTemp.addValue (data.celsiusHundredths / 100 );
168
+ raHum.addValue (data.humidityPercent );
169
+
170
+ float diffTemp = abs (oldAvgTemp - raTemp.getAverage ());
171
+ float diffHum = abs (oldAvgHum - raHum.getAverage ());
172
+
173
+ Serial.println (diffTemp);
174
+ Serial.println (diffHum);
175
+
176
+ if (isnan (diffTemp)) tx = true ;
177
+ if (diffTemp > 0.2 ) tx = true ;
178
+ if (diffHum > 0.5 ) tx = true ;
146
179
147
- if ((lastTemperature != temperature) | (lastHumidity != humidity)) {
180
+ if (tx) {
181
+ measureCount = 0 ;
182
+ float temperature = (isMetric ? data.celsiusHundredths : data.fahrenheitHundredths ) / 100.0 ;
183
+
184
+ int humidity = data.humidityPercent ;
148
185
Serial.print (" T: " );Serial.println (temperature);
149
186
Serial.print (" H: " );Serial.println (humidity);
150
187
@@ -155,12 +192,14 @@ void sendTempHumidityMeasurements(bool force)
155
192
}
156
193
}
157
194
158
- /*
159
- * Sends battery information (both voltage, and battery percentage)
195
+ /* *******************************************
196
+ *
197
+ * Sends battery information (battery percentage)
160
198
*
161
199
* Parameters
162
200
* - force : Forces transmission of a value
163
- */
201
+ *
202
+ *******************************************/
164
203
void sendBattLevel (bool force)
165
204
{
166
205
if (force) lastBattery = -1 ;
@@ -176,6 +215,11 @@ void sendBattLevel(bool force)
176
215
}
177
216
}
178
217
218
+ /* ******************************************
219
+ *
220
+ * Internal battery ADC measuring
221
+ *
222
+ *******************************************/
179
223
long readVcc () {
180
224
// Read 1.1V reference against AVcc
181
225
// set the reference to Vcc and the measurement to the internal 1.1V reference
@@ -202,18 +246,11 @@ long readVcc() {
202
246
return result; // Vcc in millivolts
203
247
}
204
248
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.
249
+ /* ***************************************************
250
+ *
251
+ * Verify all peripherals, and signal via the LED if any problems.
252
+ *
253
+ ****************************************************/
217
254
void testMode ()
218
255
{
219
256
uint8_t rx_buffer[SHA204_RSP_SIZE_MAX];
0 commit comments