From 6482413db0ef717255bb0a98d38e1468f6db33ff Mon Sep 17 00:00:00 2001 From: Hendrik Langer Date: Tue, 5 Jun 2018 18:00:00 +0200 Subject: [PATCH] implement running average --- platformio.ini | 1 + src/main.cpp | 35 ++++++++++++++++++++++------------- 2 files changed, 23 insertions(+), 13 deletions(-) diff --git a/platformio.ini b/platformio.ini index b11d005..44ac371 100644 --- a/platformio.ini +++ b/platformio.ini @@ -19,3 +19,4 @@ lib_deps = DHT sensor library ; SDS011 sensor Library https://github.com/Zanop/SDS011.git#more-sensor-modes + RunningAverage diff --git a/src/main.cpp b/src/main.cpp index cf4f667..311be45 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -9,6 +9,7 @@ #include extern "C" { #include +#include "user_interface.h" } #include @@ -20,6 +21,8 @@ extern "C" { #include #include +#include + #include const char* server = "ingress.opensensemap.org"; @@ -63,17 +66,18 @@ static constexpr uint8_t SDS_RX = D2; static constexpr uint8_t GEIGER_PIN = D6; static constexpr float CONV_FACTOR = 0.008120; static constexpr float OWN_BACKGROUND_CPS = 0; // documentation says 0.2 (make sure value doesn't get negative if subtracting!) -static constexpr unsigned long logging_period_ms = 60000; ADC_MODE(ADC_VCC); ESP8266WiFiMulti wifiMulti; +os_timer_t Timer1; +RunningAverage geigeraverage(10); + Adafruit_BMP085_Unified bmp = Adafruit_BMP085_Unified(10085); DHT dht(DHT22_PIN, DHTTYPE); SDS011 sds; volatile unsigned long geiger_counts = 0; -unsigned long geiger_previousMillis; unsigned long last_wifi_activity = 0; unsigned long last_dhcp = 0; IPAddress ip, dns, gateway, subnet; @@ -87,7 +91,7 @@ struct __attribute__((packed)) SENSOR_DATA { float temp2; float p10; float p25; - unsigned long cpm; + float cpm; float radioactivity; float voltage; float rssi; @@ -148,16 +152,9 @@ void ICACHE_FLASH_ATTR sendValues() { sd.cpm = 0; sd.radioactivity = -1; - if (millis() - geiger_previousMillis > logging_period_ms) { - sd.cpm = geiger_counts * 60000 / (millis() - geiger_previousMillis); - unsigned long constexpr own_cpm = OWN_BACKGROUND_CPS * 60; - geiger_previousMillis = millis(); - geiger_counts = 0; - sd.radioactivity = (sd.cpm - own_cpm) * CONV_FACTOR; -// Serial.println(cpm); - } else { -// Serial.println("Geiger Counter: no new value"); - } + sd.cpm = geigeraverage.getAverage()*10; + float constexpr own_cpm = OWN_BACKGROUND_CPS * 60; + sd.radioactivity = (sd.cpm - own_cpm) * CONV_FACTOR; Serial.printf("Temperature : %6.2f°C (DHT22)\n", sd.temperature); Serial.printf("Humidity : %6.2f%% (DHT22)\n", sd.humidity); @@ -314,6 +311,13 @@ void ICACHE_RAM_ATTR ISR_geiger_impulse() { geiger_counts++; } +// will be called every 6 seconds +void timerCallback(void *pArg) { + //Serial.printf("running average counts: %d average: %6.2f\n", geiger_counts, geigeraverage.getAverage()); + geigeraverage.addValue(geiger_counts); + geiger_counts = 0; +} + void setup() { Serial.begin(115200); @@ -334,6 +338,11 @@ void setup() { pinMode(GEIGER_PIN, INPUT); attachInterrupt(digitalPinToInterrupt(GEIGER_PIN), ISR_geiger_impulse, FALLING); + geigeraverage.clear(); + geigeraverage.addValue(2); + os_timer_setfn(&Timer1, timerCallback, NULL); + os_timer_arm(&Timer1, 6000, true); + Serial.println("ready."); Serial.flush(); }