Send the average value



  • I have the following code for my sensor and was wondering if it is possilbe to send the average value collected for the
    DHT_interval which is set in the beginning?

    
      unsigned long DHT_Current_Millis = millis();
      if(DHT_Current_Millis - DHT_Millis > DHT_interval)
        {
        DHT_Millis = DHT_Current_Millis; 
        delay(dht.getMinimumSamplingPeriod());
      float temperature = dht.getTemperature();
      if (isnan(temperature)) 
        { Serial.println("Failed reading temperature from DHT"); } 
        else if (temperature != lastTemp) 
          {
          lastTemp = temperature;
          gw.send(msgTemp.set(temperature, 2));
          Serial.print("T: ");
          Serial.println(temperature);
          }
    float humidity = dht.getHumidity();
      if (isnan(humidity)) 
      { Serial.println("Failed reading humidity from DHT"); } 
      else if (humidity != lastHum) 
        {
    	lastHum = humidity;
    	gw.send(msgHum.set(humidity, 2));
    	Serial.print("H: ");
    	Serial.println(humidity);
    	}
    }```


  • @moskovskiy82
    you could use the runningaverage library for this. see here made by Rob Tillaart.

    Then you still need some mechanism to fill the average. This could be done by executing code at an interval which is e.g. 1/10 or 1/20 of the DHT_interval value. This also defines the arraylength myRA(xxx) as in the example below. 1/10: myRA(10); 1/20: myRA(20);

    Good luck with it,

    Boozz

    //
    //    FILE: runningAverageTest.pde
    //  AUTHOR: Rob Tillaart
    //    DATE: 2012-12-30
    //
    // PUPROSE: show working of runningAverage
    //
    
    #include "RunningAverage.h"
    
    RunningAverage myRA(10);
    int samples = 0;
    
    void setup(void) 
    {
      Serial.begin(115200);
      Serial.println("Demo RunningAverage lib");
      Serial.print("Version: ");
      Serial.println(RUNNINGAVERAGE_LIB_VERSION);
      myRA.clear(); // explicitly start clean
    }
    
    void loop(void) 
    {
      long rn = random(0, 1000);
      myRA.addValue(rn * 0.001);
      samples++;
      Serial.print("Running Average: ");
      Serial.println(myRA.getAverage(), 3);
    
      if (samples == 300)
      {
        samples = 0;
        myRA.clear();
      }
      delay(100);
    }
    

  • Mod

    @moskovskiy82 could you describe why you want the average?

    Are you sure the median wouldn't be a better choice? Or letting the controller handle averaging, since it will have access to past events even if the sensor is restarted.


 

464
Online

7.9k
Users

8.8k
Topics

93.7k
Posts