Skip to content

Commit 02b8f75

Browse files
committed
Merge pull request arduino#112 from tbowmo/master
Here we go again
2 parents d97dacb + d5becd5 commit 02b8f75

File tree

8 files changed

+436
-39
lines changed

8 files changed

+436
-39
lines changed

libraries/MySensors/examples/SensebenderMicro/SensebenderMicro.ino

+83-39
Original file line numberDiff line numberDiff line change
@@ -14,16 +14,19 @@
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
@@ -34,7 +37,7 @@
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
5154
MyMessage msgHum(CHILD_ID_HUM, V_HUM);
5255
MyMessage msgTemp(CHILD_ID_TEMP, V_TEMP);
53-
MyMessage msgBattery(CHILD_ID_BATT, V_VOLTAGE);
5456

5557
// Global settings
5658
int measureCount = 0;
59+
int sendBattery = 0;
5760
boolean isMetric = true;
61+
boolean highfreq = true;
5862

5963
// Storage of old measurements
6064
float lastTemperature = -100;
6165
int lastHumidity = -100;
6266
long lastBattery = -100;
6367

64-
bool highfreq = true;
68+
RunningAverage raHum(AVERAGES);
69+
RunningAverage raTemp(AVERAGES);
6570

71+
/****************************************************
72+
*
73+
* Setup code
74+
*
75+
****************************************************/
6676
void 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+
***********************************************/
108129
void 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+
*********************************************/
134166
void 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+
*******************************************/
164210
void 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+
*******************************************/
179230
long 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+
****************************************************/
217261
void testMode()
218262
{
219263
uint8_t rx_buffer[SHA204_RSP_SIZE_MAX];
+101
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,101 @@
1+
//
2+
// FILE: RunningAverage.cpp
3+
// AUTHOR: Rob Tillaart
4+
// VERSION: 0.2.08
5+
// DATE: 2015-apr-10
6+
// PURPOSE: RunningAverage library for Arduino
7+
//
8+
// The library stores N individual values in a circular buffer,
9+
// to calculate the running average.
10+
//
11+
// HISTORY:
12+
// 0.1.00 - 2011-01-30 initial version
13+
// 0.1.01 - 2011-02-28 fixed missing destructor in .h
14+
// 0.2.00 - 2012-??-?? Yuval Naveh added trimValue (found on web)
15+
// http://stromputer.googlecode.com/svn-history/r74/trunk/Arduino/Libraries/RunningAverage/RunningAverage.cpp
16+
// 0.2.01 - 2012-11-21 refactored
17+
// 0.2.02 - 2012-12-30 refactored trimValue -> fillValue
18+
// 0.2.03 - 2013-11-31 getElement
19+
// 0.2.04 - 2014-07-03 added memory protection
20+
// 0.2.05 - 2014-12-16 changed float -> double
21+
// 0.2.06 - 2015-03-07 all size uint8_t
22+
// 0.2.07 - 2015-03-16 added getMin() and getMax() functions (Eric Mulder)
23+
// 0.2.08 - 2015-04-10 refactored getMin() and getMax() implementation
24+
//
25+
// Released to the public domain
26+
//
27+
28+
#include "RunningAverage.h"
29+
#include <stdlib.h>
30+
31+
RunningAverage::RunningAverage(uint8_t size)
32+
{
33+
_size = size;
34+
_ar = (double*) malloc(_size * sizeof(double));
35+
if (_ar == NULL) _size = 0;
36+
clear();
37+
}
38+
39+
RunningAverage::~RunningAverage()
40+
{
41+
if (_ar != NULL) free(_ar);
42+
}
43+
44+
// resets all counters
45+
void RunningAverage::clear()
46+
{
47+
_cnt = 0;
48+
_idx = 0;
49+
_sum = 0.0;
50+
_min = NAN;
51+
_max = NAN;
52+
for (uint8_t i = 0; i < _size; i++)
53+
{
54+
_ar[i] = 0.0; // keeps addValue simple
55+
}
56+
}
57+
58+
// adds a new value to the data-set
59+
void RunningAverage::addValue(double value)
60+
{
61+
if (_ar == NULL) return; // allocation error
62+
_sum -= _ar[_idx];
63+
_ar[_idx] = value;
64+
_sum += _ar[_idx];
65+
_idx++;
66+
if (_idx == _size) _idx = 0; // faster than %
67+
// handle min max
68+
if (_cnt == 0) _min = _max = value;
69+
else if (value < _min) _min = value;
70+
else if (value > _max) _max = value;
71+
// update count as last otherwise if( _cnt == 0) above will fail
72+
if (_cnt < _size) _cnt++;
73+
}
74+
75+
// returns the average of the data-set added sofar
76+
double RunningAverage::getAverage()
77+
{
78+
if (_cnt == 0) return NAN;
79+
return _sum / _cnt;
80+
}
81+
82+
// returns the value of an element if exist, NAN otherwise
83+
double RunningAverage::getElement(uint8_t idx)
84+
{
85+
if (idx >=_cnt ) return NAN;
86+
return _ar[idx];
87+
}
88+
89+
// fill the average with a value
90+
// the param number determines how often value is added (weight)
91+
// number should preferably be between 1 and size
92+
void RunningAverage::fillValue(double value, uint8_t number)
93+
{
94+
clear(); // TODO conditional? if (clr) clear();
95+
96+
for (uint8_t i = 0; i < number; i++)
97+
{
98+
addValue(value);
99+
}
100+
}
101+
// END OF FILE
+56
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
//
2+
// FILE: RunningAverage.h
3+
// AUTHOR: Rob dot Tillaart at gmail dot com
4+
// VERSION: 0.2.08
5+
// DATE: 2015-apr-10
6+
// PURPOSE: RunningAverage library for Arduino
7+
// URL: http://arduino.cc/playground/Main/RunningAverage
8+
// HISTORY: See RunningAverage.cpp
9+
//
10+
// Released to the public domain
11+
//
12+
// backwards compatibility
13+
// clr() clear()
14+
// add(x) addValue(x)
15+
// avg() getAverage()
16+
17+
#ifndef RunningAverage_h
18+
#define RunningAverage_h
19+
20+
#define RUNNINGAVERAGE_LIB_VERSION "0.2.08"
21+
22+
#include "Arduino.h"
23+
24+
class RunningAverage
25+
{
26+
public:
27+
RunningAverage(void);
28+
RunningAverage(uint8_t);
29+
~RunningAverage();
30+
31+
void clear();
32+
void addValue(double);
33+
void fillValue(double, uint8_t);
34+
35+
double getAverage();
36+
// returns lowest value added to the data-set since last clear
37+
double getMin() { return _min; };
38+
// returns highest value added to the data-set since last clear
39+
double getMax() { return _max; };
40+
41+
double getElement(uint8_t idx);
42+
uint8_t getSize() { return _size; }
43+
uint8_t getCount() { return _cnt; }
44+
45+
protected:
46+
uint8_t _size;
47+
uint8_t _cnt;
48+
uint8_t _idx;
49+
double _sum;
50+
double * _ar;
51+
double _min;
52+
double _max;
53+
};
54+
55+
#endif
56+
// END OF FILE

0 commit comments

Comments
 (0)