implement RunningAverage of 20 for DHT22
authorDobrica Pavlinusic <dpavlin@rot13.org>
Fri, 24 Nov 2017 19:09:55 +0000 (20:09 +0100)
committerDobrica Pavlinusic <dpavlin@rot13.org>
Fri, 24 Nov 2017 19:09:55 +0000 (20:09 +0100)
This should help this extreamly noisy sensor. We also pull readings
every 2 seconds so we can report running average of last 20 readings
right away.

libraries/RunningAverage/RunningAverage.cpp [new file with mode: 0644]
libraries/RunningAverage/RunningAverage.h [new file with mode: 0644]
rpi_promini/Makefile
rpi_promini/rpi_promini.ino
wlan_si/RunningAverage.cpp [deleted file]
wlan_si/RunningAverage.h [deleted file]

diff --git a/libraries/RunningAverage/RunningAverage.cpp b/libraries/RunningAverage/RunningAverage.cpp
new file mode 100644 (file)
index 0000000..f72d90a
--- /dev/null
@@ -0,0 +1,85 @@
+//
+//    FILE: RunningAverage.cpp
+//  AUTHOR: Rob Tillaart
+// VERSION: 0.2.04
+// PURPOSE: RunningAverage library for Arduino
+//
+// The library stores the last N individual values in a circular buffer,
+// to calculate the running average.
+//
+// HISTORY:
+// 0.1.00 - 2011-01-30 initial version
+// 0.1.01 - 2011-02-28 fixed missing destructor in .h
+// 0.2.00 - 2012-??-?? Yuval Naveh added trimValue (found on web)
+//          http://stromputer.googlecode.com/svn-history/r74/trunk/Arduino/Libraries/RunningAverage/RunningAverage.cpp
+// 0.2.01 - 2012-11-21 refactored
+// 0.2.02 - 2012-12-30 refactored trimValue -> fillValue
+// 0.2.03 - 2013-11-31 getElement
+// 0.2.04 - 2014-07-03 added memory protection
+//
+// Released to the public domain
+//
+
+#include "RunningAverage.h"
+#include <stdlib.h>
+
+RunningAverage::RunningAverage(int n)
+{
+    _size = n;
+    _ar = (float*) malloc(_size * sizeof(float));
+    if (_ar == NULL) _size = 0;
+    clear();
+}
+
+RunningAverage::~RunningAverage()
+{
+    if (_ar != NULL) free(_ar);
+}
+
+// resets all counters
+void RunningAverage::clear()
+{
+    _cnt = 0;
+    _idx = 0;
+    _sum = 0.0;
+    for (int i = 0; i< _size; i++) _ar[i] = 0.0;  // needed to keep addValue simple
+}
+
+// adds a new value to the data-set
+void RunningAverage::addValue(float f)
+{
+    if (_ar == NULL) return;
+    _sum -= _ar[_idx];
+    _ar[_idx] = f;
+    _sum += _ar[_idx];
+    _idx++;
+    if (_idx == _size) _idx = 0;  // faster than %
+    if (_cnt < _size) _cnt++;
+}
+
+// returns the average of the data-set added sofar
+float RunningAverage::getAverage()
+{
+    if (_cnt == 0) return NAN;
+    return _sum / _cnt;
+}
+
+// returns the value of an element if exist, 0 otherwise
+float RunningAverage::getElement(uint8_t idx)
+{
+    if (idx >=_cnt ) return NAN;
+    return _ar[idx];
+}
+
+// fill the average with a value
+// the param number determines how often value is added (weight)
+// number should preferably be between 1 and size
+void RunningAverage::fillValue(float value, int number)
+{
+    clear();
+    for (int i = 0; i < number; i++)
+    {
+        addValue(value);
+    }
+}
+// END OF FILE
diff --git a/libraries/RunningAverage/RunningAverage.h b/libraries/RunningAverage/RunningAverage.h
new file mode 100644 (file)
index 0000000..7a400a4
--- /dev/null
@@ -0,0 +1,48 @@
+#ifndef RunningAverage_h
+#define RunningAverage_h
+//
+//    FILE: RunningAverage.h
+//  AUTHOR: Rob dot Tillaart at gmail dot com
+// PURPOSE: RunningAverage library for Arduino
+//     URL: http://arduino.cc/playground/Main/RunningAverage
+// HISTORY: See RunningAverage.cpp
+//
+// Released to the public domain
+//
+
+// backwards compatibility
+// clr() clear()
+// add(x) addValue(x)
+// avg() getAverage()
+
+#define RUNNINGAVERAGE_LIB_VERSION "0.2.04"
+
+#include "Arduino.h"
+
+class RunningAverage
+{
+public:
+    RunningAverage(void);
+    RunningAverage(int);
+    ~RunningAverage();
+
+    void clear();
+    void addValue(float);
+    void fillValue(float, int);
+
+    float getAverage();
+
+    float getElement(uint8_t idx);
+    uint8_t getSize() { return _size; }
+    uint8_t getCount() { return _cnt; }
+
+protected:
+    uint8_t _size;
+    uint8_t _cnt;
+    uint8_t _idx;
+    float   _sum;
+    float * _ar;
+};
+
+#endif
+// END OF FILE
index 1b11b18..c211581 100644 (file)
@@ -7,8 +7,8 @@ all:
        echo "flash serial"
 
 flash:
-       scp /tmp/build*.tmp/*.hex pi@rpi.tv:/tmp/
-       ssh pi@rpi.tv /home/pi/avrdude.sh
+       scp build-*/*.hex pi@rpi:/tmp/
+       ssh pi@rpi /home/pi/avrdude.sh
 
 serial:
-       ssh pi@rpi.tv microcom -p /dev/ttyAMA0 -s 9600
+       ssh pi@rpi microcom -p /dev/ttyAMA0 -s 9600
index 81964a3..1567fee 100644 (file)
@@ -76,6 +76,11 @@ void send_315(char *code) {
 #include "DHT.h"
 DHT dht;
 
+#include "RunningAverage.h"
+
+RunningAverage temp_avg(20);
+RunningAverage hum_avg(20);
+
 // setup
 
 void help() {
@@ -94,13 +99,28 @@ void setup() {
   // DHT22
   dht.setup(8);
 
+    temp_avg.addValue( dht.getTemperature() );
+    hum_avg.addValue( dht.getHumidity() );
+
 }
 
 int serial_pos = 0;
 char serial_data[2]; // socket (0-9), state (0-1)
 char binary_data[32];
 
+unsigned long time = millis();
+
 void loop() {
+  if ( millis() - time > 2000 ) {
+       float t = dht.getTemperature();
+    if ( dht.getStatus() == 0 )
+      temp_avg.addValue( t );
+       float h = dht.getHumidity();
+    if ( dht.getStatus() == 0 )
+      hum_avg.addValue( h );
+    time = millis();
+  }
+
   if (mySwitch.available()) {
     Serial.print(mySwitch.getReceivedBitlength());
     Serial.print(" bits ");
@@ -147,11 +167,9 @@ void loop() {
      // DHT22
      if (input == 'd') {
        Serial.print("temperature=");
-       Serial.print(dht.getTemperature());
+       Serial.print(temp_avg.getAverage());
        Serial.print(" humidity=");
-       Serial.print(dht.getHumidity());
-       Serial.print(" status=");
-       Serial.println(dht.getStatusString());
+       Serial.println(hum_avg.getAverage());
      }
 
      if ( input >= 0x30 && input <= 0x39 && serial_pos < 2 ) {
diff --git a/wlan_si/RunningAverage.cpp b/wlan_si/RunningAverage.cpp
deleted file mode 100644 (file)
index f72d90a..0000000
+++ /dev/null
@@ -1,85 +0,0 @@
-//
-//    FILE: RunningAverage.cpp
-//  AUTHOR: Rob Tillaart
-// VERSION: 0.2.04
-// PURPOSE: RunningAverage library for Arduino
-//
-// The library stores the last N individual values in a circular buffer,
-// to calculate the running average.
-//
-// HISTORY:
-// 0.1.00 - 2011-01-30 initial version
-// 0.1.01 - 2011-02-28 fixed missing destructor in .h
-// 0.2.00 - 2012-??-?? Yuval Naveh added trimValue (found on web)
-//          http://stromputer.googlecode.com/svn-history/r74/trunk/Arduino/Libraries/RunningAverage/RunningAverage.cpp
-// 0.2.01 - 2012-11-21 refactored
-// 0.2.02 - 2012-12-30 refactored trimValue -> fillValue
-// 0.2.03 - 2013-11-31 getElement
-// 0.2.04 - 2014-07-03 added memory protection
-//
-// Released to the public domain
-//
-
-#include "RunningAverage.h"
-#include <stdlib.h>
-
-RunningAverage::RunningAverage(int n)
-{
-    _size = n;
-    _ar = (float*) malloc(_size * sizeof(float));
-    if (_ar == NULL) _size = 0;
-    clear();
-}
-
-RunningAverage::~RunningAverage()
-{
-    if (_ar != NULL) free(_ar);
-}
-
-// resets all counters
-void RunningAverage::clear()
-{
-    _cnt = 0;
-    _idx = 0;
-    _sum = 0.0;
-    for (int i = 0; i< _size; i++) _ar[i] = 0.0;  // needed to keep addValue simple
-}
-
-// adds a new value to the data-set
-void RunningAverage::addValue(float f)
-{
-    if (_ar == NULL) return;
-    _sum -= _ar[_idx];
-    _ar[_idx] = f;
-    _sum += _ar[_idx];
-    _idx++;
-    if (_idx == _size) _idx = 0;  // faster than %
-    if (_cnt < _size) _cnt++;
-}
-
-// returns the average of the data-set added sofar
-float RunningAverage::getAverage()
-{
-    if (_cnt == 0) return NAN;
-    return _sum / _cnt;
-}
-
-// returns the value of an element if exist, 0 otherwise
-float RunningAverage::getElement(uint8_t idx)
-{
-    if (idx >=_cnt ) return NAN;
-    return _ar[idx];
-}
-
-// fill the average with a value
-// the param number determines how often value is added (weight)
-// number should preferably be between 1 and size
-void RunningAverage::fillValue(float value, int number)
-{
-    clear();
-    for (int i = 0; i < number; i++)
-    {
-        addValue(value);
-    }
-}
-// END OF FILE
diff --git a/wlan_si/RunningAverage.h b/wlan_si/RunningAverage.h
deleted file mode 100644 (file)
index 7a400a4..0000000
+++ /dev/null
@@ -1,48 +0,0 @@
-#ifndef RunningAverage_h
-#define RunningAverage_h
-//
-//    FILE: RunningAverage.h
-//  AUTHOR: Rob dot Tillaart at gmail dot com
-// PURPOSE: RunningAverage library for Arduino
-//     URL: http://arduino.cc/playground/Main/RunningAverage
-// HISTORY: See RunningAverage.cpp
-//
-// Released to the public domain
-//
-
-// backwards compatibility
-// clr() clear()
-// add(x) addValue(x)
-// avg() getAverage()
-
-#define RUNNINGAVERAGE_LIB_VERSION "0.2.04"
-
-#include "Arduino.h"
-
-class RunningAverage
-{
-public:
-    RunningAverage(void);
-    RunningAverage(int);
-    ~RunningAverage();
-
-    void clear();
-    void addValue(float);
-    void fillValue(float, int);
-
-    float getAverage();
-
-    float getElement(uint8_t idx);
-    uint8_t getSize() { return _size; }
-    uint8_t getCount() { return _cnt; }
-
-protected:
-    uint8_t _size;
-    uint8_t _cnt;
-    uint8_t _idx;
-    float   _sum;
-    float * _ar;
-};
-
-#endif
-// END OF FILE