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 -
Which device I have to use to connect with accelerometer before connecting to my pc?
General Discussion • 24 Jan 2023, 17:16 • Yada Kijsathan 16 Mar 2025, 20:17 -
ESP-NOW
General Discussion • 22 Apr 2018, 05:58 • NeverDie 17 Feb 2025, 22:24 -
ESP32 with LoRa
General Discussion • 31 Jan 2023, 11:06 • dhanushmh 24 days ago -
Meet in Malmö, Summer 2016?
General Discussion • 1 Feb 2016, 15:34 • bjacobse 4 Mar 2025, 00:29 -
Code Garage to the rescue.
General Discussion • 11 Jan 2025, 11:25 • skywatch 16 days ago