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
29- #define MEASURE_INTERVAL 60000
32+ #define MEASURE_INTERVAL 1000
3033
3134// FORCE_TRANSMIT_INTERVAL, this number of times of wakeup, the sensor is forced to report all values to the controller
3235#define FORCE_TRANSMIT_INTERVAL 30
3336
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,25 +53,32 @@ 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 ;
5861
5962// Storage of old measurements
6063float lastTemperature = -100 ;
6164int lastHumidity = -100 ;
6265long lastBattery = -100 ;
6366
64- bool highfreq = true ;
67+ RunningAverage raHum (AVERAGES);
68+ RunningAverage raTemp (AVERAGES);
6569
70+ /* ***************************************************
71+ *
72+ * Setup code
73+ *
74+ ****************************************************/
6675void setup () {
67-
76+ // clock_prescale_set(clock_div_8); // Switch to 1Mhz right after powerup.
77+
6878 pinMode (LED_PIN, OUTPUT);
6979 digitalWrite (LED_PIN, LOW);
7080
71- Serial.begin (115200 );
81+ Serial.begin (19200 );
7282 Serial.print (F (" Sensebender Micro FW " ));
7383 Serial.print (RELEASE);
7484 Serial.flush ();
@@ -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,75 @@ 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 );
104120}
105121
106122
107- // Main loop function
123+ /* **********************************************
124+ *
125+ * Main loop function
126+ *
127+ ***********************************************/
108128void loop () {
109129 measureCount ++;
130+ sendBattery ++;
110131 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.
115132
116133 if (measureCount > FORCE_TRANSMIT_INTERVAL) { // force a transmission
117134 forceTransmit = true ;
118135 measureCount = 0 ;
119136 }
120137
121138 gw.process ();
122- sendBattLevel (forceTransmit);
139+
123140 sendTempHumidityMeasurements (forceTransmit);
141+ if (sendBattery > 60 )
142+ {
143+ sendBattLevel (forceTransmit); // Not needed to send battery info that often
144+ sendBattery = 0 ;
145+ }
124146
125147 gw.sleep (MEASURE_INTERVAL);
126148}
127149
128- /*
150+
151+ /* ********************************************
152+ *
129153 * Sends temperature and humidity from Si7021 sensor
130154 *
131155 * Parameters
132156 * - force : Forces transmission of a value (even if it's the same as previous measurement)
133- */
157+ *
158+ *********************************************/
134159void sendTempHumidityMeasurements (bool force)
135160{
136- if (force) {
137- lastHumidity = -100 ;
138- lastTemperature = -100 ;
139- }
161+ bool tx = force;
140162
141163 si7021_env data = humiditySensor.getHumidityAndTemperature ();
164+ float oldAvgTemp = raTemp.getAverage ();
165+ float oldAvgHum = raHum.getAverage ();
142166
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 ;
146179
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 ;
148185 Serial.print (" T: " );Serial.println (temperature);
149186 Serial.print (" H: " );Serial.println (humidity);
150187
@@ -155,12 +192,14 @@ void sendTempHumidityMeasurements(bool force)
155192 }
156193}
157194
158- /*
159- * Sends battery information (both voltage, and battery percentage)
195+ /* *******************************************
196+ *
197+ * Sends battery information (battery percentage)
160198 *
161199 * Parameters
162200 * - force : Forces transmission of a value
163- */
201+ *
202+ *******************************************/
164203void sendBattLevel (bool force)
165204{
166205 if (force) lastBattery = -1 ;
@@ -176,6 +215,11 @@ void sendBattLevel(bool force)
176215 }
177216}
178217
218+ /* ******************************************
219+ *
220+ * Internal battery ADC measuring
221+ *
222+ *******************************************/
179223long readVcc () {
180224 // Read 1.1V reference against AVcc
181225 // set the reference to Vcc and the measurement to the internal 1.1V reference
@@ -202,18 +246,11 @@ long readVcc() {
202246 return result; // Vcc in millivolts
203247}
204248
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+ ****************************************************/
217254void testMode ()
218255{
219256 uint8_t rx_buffer[SHA204_RSP_SIZE_MAX];
0 commit comments