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); }
-
@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.
Suggested Topics
-
Update RF24 library to latest version
Bug Reports • 23 Mar 2014, 23:37 • andriej 24 Mar 2014, 22:52 -
Anybody got one / a few spare minimalist rfm69hw shields for wemos d1 mini?
General Discussion • 20 Sept 2024, 08:45 • kiesel 11 Oct 2024, 15:32 -
Meet in Malmö, Summer 2016?
General Discussion • 1 Feb 2016, 15:34 • bjacobse 22 days ago -
ESP-NOW
General Discussion • 22 Apr 2018, 05:58 • NeverDie 17 Feb 2025, 22:24 -
Running out of nodeId's
General Discussion • 3 Sept 2024, 07:33 • NielBierman 25 Jan 2025, 17:19 -
Combination of networks
General Discussion • 1 Feb 2025, 17:30 • Sniker 4 Feb 2025, 01:02