1414#include < EEPROM.h>
1515#include < sha204_lib_return_codes.h>
1616#include < sha204_library.h>
17+ #include < RunningAverage.h>
18+ #include < avr/power.h>
1719
1820// Define a static node address, remove if you want auto address assignment
1921// #define NODE_ADDRESS 3
2022
21- #define RELEASE " 1.1"
23+ #define RELEASE " 1.2"
24+
25+ #define AVERAGES 2
2226
2327// Child sensor ID's
2428#define CHILD_ID_TEMP 1
2529#define CHILD_ID_HUM 2
26- #define CHILD_ID_BATT 199
2730
2831// How many milli seconds between each measurement
2932#define MEASURE_INTERVAL 60000
3437// When MEASURE_INTERVAL is 60000 and FORCE_TRANSMIT_INTERVAL is 30, we force a transmission every 30 minutes.
3538// Between the forced transmissions a tranmission will only occur if the measured value differs from the previous measurement
3639
37- // Pin definitions
40+ // Pin definitions
3841#define TEST_PIN A0
3942#define LED_PIN A2
4043#define ATSHA204_PIN 17 // A3
@@ -50,21 +53,28 @@ MySensor gw;
5053// Sensor messages
5154MyMessage msgHum (CHILD_ID_HUM, V_HUM);
5255MyMessage msgTemp (CHILD_ID_TEMP, V_TEMP);
53- MyMessage msgBattery (CHILD_ID_BATT, V_VOLTAGE);
5456
5557// Global settings
5658int measureCount = 0 ;
59+ int sendBattery = 0 ;
5760boolean isMetric = true ;
61+ boolean highfreq = true ;
5862
5963// Storage of old measurements
6064float lastTemperature = -100 ;
6165int lastHumidity = -100 ;
6266long lastBattery = -100 ;
6367
64- bool highfreq = true ;
68+ RunningAverage raHum (AVERAGES);
69+ RunningAverage raTemp (AVERAGES);
6570
71+ /* ***************************************************
72+ *
73+ * Setup code
74+ *
75+ ****************************************************/
6676void setup () {
67-
77+
6878 pinMode (LED_PIN, OUTPUT);
6979 digitalWrite (LED_PIN, LOW);
7080
@@ -79,6 +89,10 @@ void setup() {
7989 digitalWrite (TEST_PIN, HIGH); // Enable pullup
8090 if (!digitalRead (TEST_PIN)) testMode ();
8191
92+ // Make sure that ATSHA204 is not floating
93+ pinMode (ATSHA204_PIN, INPUT);
94+ digitalWrite (ATSHA204_PIN, HIGH);
95+
8296 digitalWrite (TEST_PIN,LOW);
8397 digitalWrite (LED_PIN, HIGH);
8498
@@ -99,52 +113,82 @@ void setup() {
99113 gw.present (CHILD_ID_HUM,S_HUM);
100114
101115 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 );
104121}
105122
106123
107- // Main loop function
124+ /* **********************************************
125+ *
126+ * Main loop function
127+ *
128+ ***********************************************/
108129void loop () {
109130 measureCount ++;
131+ sendBattery ++;
110132 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+ }
115139
116140 if (measureCount > FORCE_TRANSMIT_INTERVAL) { // force a transmission
117141 forceTransmit = true ;
118142 measureCount = 0 ;
119143 }
120144
121145 gw.process ();
122- sendBattLevel (forceTransmit);
146+
123147 sendTempHumidityMeasurements (forceTransmit);
148+ if (sendBattery > 60 )
149+ {
150+ sendBattLevel (forceTransmit); // Not needed to send battery info that often
151+ sendBattery = 0 ;
152+ }
124153
125154 gw.sleep (MEASURE_INTERVAL);
126155}
127156
128- /*
157+
158+ /* ********************************************
159+ *
129160 * Sends temperature and humidity from Si7021 sensor
130161 *
131162 * Parameters
132163 * - force : Forces transmission of a value (even if it's the same as previous measurement)
133- */
164+ *
165+ *********************************************/
134166void sendTempHumidityMeasurements (bool force)
135167{
136- if (force) {
137- lastHumidity = -100 ;
138- lastTemperature = -100 ;
139- }
168+ bool tx = force;
140169
141170 si7021_env data = humiditySensor.getHumidityAndTemperature ();
171+ float oldAvgTemp = raTemp.getAverage ();
172+ float oldAvgHum = raHum.getAverage ();
142173
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 ;
146186
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 ;
148192 Serial.print (" T: " );Serial.println (temperature);
149193 Serial.print (" H: " );Serial.println (humidity);
150194
@@ -155,12 +199,14 @@ void sendTempHumidityMeasurements(bool force)
155199 }
156200}
157201
158- /*
159- * Sends battery information (both voltage, and battery percentage)
202+ /* *******************************************
203+ *
204+ * Sends battery information (battery percentage)
160205 *
161206 * Parameters
162207 * - force : Forces transmission of a value
163- */
208+ *
209+ *******************************************/
164210void sendBattLevel (bool force)
165211{
166212 if (force) lastBattery = -1 ;
@@ -176,6 +222,11 @@ void sendBattLevel(bool force)
176222 }
177223}
178224
225+ /* ******************************************
226+ *
227+ * Internal battery ADC measuring
228+ *
229+ *******************************************/
179230long readVcc () {
180231 // Read 1.1V reference against AVcc
181232 // set the reference to Vcc and the measurement to the internal 1.1V reference
@@ -202,18 +253,11 @@ long readVcc() {
202253 return result; // Vcc in millivolts
203254}
204255
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+ ****************************************************/
217261void testMode ()
218262{
219263 uint8_t rx_buffer[SHA204_RSP_SIZE_MAX];
0 commit comments