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); } }``` -
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); } -
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); } }```
Hello! It looks like you're interested in this conversation, but you don't have an account yet.
Getting fed up of having to scroll through the same posts each visit? When you register for an account, you'll always come back to exactly where you were before, and choose to be notified of new replies (either via email, or push notification). You'll also be able to save bookmarks and upvote posts to show your appreciation to other community members.
With your input, this post could be even better 💗
Register Login