added runningAverage for pressure
authorDobrica Pavlinusic <dpavlin@rot13.org>
Thu, 23 Nov 2017 11:18:10 +0000 (12:18 +0100)
committerDobrica Pavlinusic <dpavlin@rot13.org>
Thu, 23 Nov 2017 11:18:20 +0000 (12:18 +0100)
wlan_si/Makefile
wlan_si/wlan_si.ino

index b1b4134..f4a7822 100644 (file)
@@ -1,10 +1,13 @@
-all:
-       echo "flash serial"
+#ARDUINO_DIR=/opt/arduino
+ARDUINO_SKETCHBOOK = ../
+
+BOARD_TAG    = pro5v328
+include /usr/share/arduino/Arduino.mk
 
 pi=10.60.1.201
 
 flash:
-       scp `ls -t /tmp/build*.tmp/*.hex | head -1` pi@${pi}:/tmp/arduino.hex
+       scp `ls -t build-*/*.hex | head -1` pi@${pi}:/tmp/arduino.hex
        ssh pi@${pi} sudo avrdude -c linuxgpio -p atmega328p -U flash:w:/tmp/arduino.hex
 
 serial:
index b3b43a6..2c12d93 100644 (file)
@@ -9,6 +9,24 @@ int pir = 6; // PIR on d6
 // hardware based on https://dev.wlan-si.net/wiki/Telemetry/sensgw
 // original software https://github.com/SloMusti/sensgw
 
+float runningAverage(float M) {
+  #define LM_SIZE 100
+  static float LM[LM_SIZE];      // LastMeasurements
+  static byte index = 0;
+  static float sum = 0;
+  static byte count = 0;
+
+  // keep sum updated to improve speed.
+  sum -= LM[index];
+  LM[index] = M;
+  sum += LM[index];
+  index++;
+  index = index % LM_SIZE;
+  if (count < LM_SIZE) count++;
+
+  return sum / count;
+}
+
 void setup(void) 
 {
   Serial.begin(9600);
@@ -21,6 +39,8 @@ void setup(void)
   pinMode(pir, INPUT);
 }
 
+int count = 0;
+
 void loop(void) 
 {
   digitalWrite(led, HIGH);
@@ -28,7 +48,9 @@ void loop(void)
   float pressureKPA = 0, temperatureC = 0;    
 
   mpl115a2.getPT(&pressureKPA,&temperatureC);
-  Serial.print("Pressure="); Serial.print(pressureKPA, 4); Serial.print(" kPa ");
+
+  if ( count++ % 10 == 0 ) {
+  Serial.print("Pressure="); Serial.print(runningAverage(pressureKPA), 4); Serial.print(" kPa ");
   Serial.print("Temp="); Serial.print(temperatureC, 1); Serial.print(" C");
 
   Serial.print(" PIR="); Serial.print( digitalRead(pir) );
@@ -37,7 +59,11 @@ void loop(void)
 
   Serial.println();
 
+  } else {
+       runningAverage(pressureKPA);
+  }
+
   digitalWrite(led, LOW);
   
-  delay(1000);
+  delay(100);
 }